Ajout d'une correction possible
This commit is contained in:
@@ -0,0 +1,7 @@
|
||||
namespace MiniJeuxFinal.Games.PierrePapierCiseaux.Actions
|
||||
{
|
||||
public interface IActionPpc : IAction
|
||||
{
|
||||
bool ToWin(IActionPpc other);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace MiniJeuxFinal.Games.PierrePapierCiseaux.Actions
|
||||
{
|
||||
public class PaperAction : IActionPpc
|
||||
{
|
||||
public string Name => "Papier";
|
||||
|
||||
public bool ToWin(IActionPpc other)
|
||||
{
|
||||
return other is not ScissorsAction;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace MiniJeuxFinal.Games.PierrePapierCiseaux.Actions
|
||||
{
|
||||
public class ScissorsAction : IActionPpc
|
||||
{
|
||||
public string Name => "Ciseaux";
|
||||
|
||||
public bool ToWin(IActionPpc other)
|
||||
{
|
||||
return other is not StoneAction;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
namespace MiniJeuxFinal.Games.PierrePapierCiseaux.Actions
|
||||
{
|
||||
public class StoneAction : IActionPpc
|
||||
{
|
||||
public string Name => "Pierre";
|
||||
|
||||
public bool ToWin(IActionPpc other)
|
||||
{
|
||||
return other is not PaperAction;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,9 @@
|
||||
using MiniJeuxFinal.Games.PierrePapierCiseaux.Actions;
|
||||
|
||||
namespace MiniJeuxFinal.Games.PierrePapierCiseaux.Factories
|
||||
{
|
||||
public interface IActionPpcFactory
|
||||
{
|
||||
IActionPpc GetAction(IList<IActionPpc> action);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
using MiniJeuxFinal.Games.PierrePapierCiseaux.Actions;
|
||||
using MiniJeuxFinal.Wrappers;
|
||||
|
||||
namespace MiniJeuxFinal.Games.PierrePapierCiseaux.Factories
|
||||
{
|
||||
public class InputActionFactory : IActionPpcFactory
|
||||
{
|
||||
private readonly IConsole _console;
|
||||
|
||||
public InputActionFactory(IConsole console)
|
||||
{
|
||||
_console = console;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public IActionPpc GetAction(IList<IActionPpc> action)
|
||||
{
|
||||
_console.WriteLine("Choisis (pierre, papier, ciseaux) : ");
|
||||
for (int i = 0; i < action.Count; i++)
|
||||
Console.WriteLine($"{i}: {action[i].Name}");
|
||||
|
||||
var result = _console.ReadLine();
|
||||
if (!int.TryParse(result, out var shootId) || shootId >= action.Count)
|
||||
{
|
||||
_console.WriteLine("❌ Choix invalide !");
|
||||
return GetAction(action);
|
||||
}
|
||||
|
||||
return action[shootId];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using MiniJeuxFinal.Games.PierrePapierCiseaux.Actions;
|
||||
|
||||
namespace MiniJeuxFinal.Games.PierrePapierCiseaux.Factories
|
||||
{
|
||||
public class RandomActionFactory : IActionPpcFactory
|
||||
{
|
||||
private static Random rnd = new Random();
|
||||
|
||||
|
||||
|
||||
public IActionPpc GetAction(IList<IActionPpc> action)
|
||||
{
|
||||
int n = rnd.Next(action.Count);
|
||||
return action[n];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,58 @@
|
||||
using MiniJeuxFinal.Games.PierrePapierCiseaux.Actions;
|
||||
using MiniJeuxFinal.Games.PierrePapierCiseaux.Factories;
|
||||
using MiniJeuxFinal.Games.PierrePapierCiseaux.Players;
|
||||
using MiniJeuxFinal.Wrappers;
|
||||
|
||||
namespace MiniJeuxFinal.Games.PierrePapierCiseaux
|
||||
{
|
||||
internal class PierrePapierCiseauxGame : IGame
|
||||
{
|
||||
public string Name => "Jeu du Pierre, Papier, Ciseaux 🧱 📄 ✂️";
|
||||
|
||||
private readonly int _totalRounds;
|
||||
private readonly string _playerName;
|
||||
|
||||
public event EventHandler<(IPlayer winner, IPlayer[] players)>? GameEnded;
|
||||
|
||||
public event EventHandler<(IPlayer? winner, IPlayer[] players)>? TurnEnded;
|
||||
public event EventHandler<(string playerChoice, string computerChoice)>? TurnStarted;
|
||||
|
||||
public PierrePapierCiseauxGame(int totalRound, string playerName)
|
||||
{
|
||||
_totalRounds = totalRound;
|
||||
_playerName = playerName;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void Start()
|
||||
{
|
||||
IAction[] lstActions = [new StoneAction(), new PaperAction(), new ScissorsAction()];
|
||||
var player = new PlayerPpc(_playerName, new InputActionFactory(new ConsoleService()));
|
||||
player.AddAction(lstActions);
|
||||
var computer = new PlayerPpc("Ordi", new RandomActionFactory());
|
||||
computer.AddAction(lstActions);
|
||||
|
||||
while (player.Score < _totalRounds && computer.Score < _totalRounds)
|
||||
{
|
||||
var actionPLayer = (IActionPpc)player.Play();
|
||||
var actionComputer = (IActionPpc)computer.Play();
|
||||
TurnStarted?.Invoke(this, (actionPLayer.Name, actionComputer.Name));
|
||||
|
||||
if (actionPLayer.ToWin(actionComputer))
|
||||
player.IncrementScore();
|
||||
else if (actionComputer.ToWin(actionPLayer))
|
||||
computer.IncrementScore();
|
||||
else
|
||||
{
|
||||
TurnEnded?.Invoke(this, (null, [player, computer]));
|
||||
continue;
|
||||
}
|
||||
|
||||
TurnEnded?.Invoke(this, (actionPLayer.ToWin(actionComputer) ? player : computer, [player, computer]));
|
||||
}
|
||||
|
||||
GameEnded?.Invoke(this, (player.Score > computer.Score ? player : computer, [player, computer]));
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,50 @@
|
||||
using MiniJeuxFinal.Games.PierrePapierCiseaux.Actions;
|
||||
using MiniJeuxFinal.Games.PierrePapierCiseaux.Factories;
|
||||
|
||||
namespace MiniJeuxFinal.Games.PierrePapierCiseaux.Players
|
||||
{
|
||||
public class PlayerPpc : IPlayer
|
||||
{
|
||||
private readonly List<IActionPpc> _actions = [];
|
||||
private readonly IActionPpcFactory _actionPpcFactory;
|
||||
|
||||
|
||||
|
||||
public PlayerPpc(string name, IActionPpcFactory actionPpcFactory)
|
||||
{
|
||||
Name = name;
|
||||
_actionPpcFactory = actionPpcFactory;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public string Name { get; }
|
||||
|
||||
public int Score { get; private set; }
|
||||
|
||||
|
||||
public void AddAction(params IAction[] action)
|
||||
{
|
||||
_actions.AddRange(action.Cast<IActionPpc>());
|
||||
}
|
||||
|
||||
public void IncrementScore()
|
||||
{
|
||||
Score++;
|
||||
}
|
||||
|
||||
public IAction Play()
|
||||
{
|
||||
if (!_actions.Any())
|
||||
throw new InvalidOperationException("You don't have a action");
|
||||
|
||||
return _actionPpcFactory.GetAction(_actions);
|
||||
}
|
||||
|
||||
|
||||
public override string ToString()
|
||||
{
|
||||
return $"{Name}: {Score}";
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user