diff --git a/Correction/MiniJeuxFinal/Games/IGame.cs b/Correction/MiniJeuxFinal/Games/IGame.cs index a6b2a3b..3a8464f 100644 --- a/Correction/MiniJeuxFinal/Games/IGame.cs +++ b/Correction/MiniJeuxFinal/Games/IGame.cs @@ -1,6 +1,6 @@ namespace MiniJeuxFinal.Games { - internal interface IGame + public interface IGame { event EventHandler<(IPlayer winner, IPlayer[] players)>? GameEnded; diff --git a/Correction/MiniJeuxFinal/Games/IPlayer.cs b/Correction/MiniJeuxFinal/Games/IPlayer.cs index 782b62e..85211d5 100644 --- a/Correction/MiniJeuxFinal/Games/IPlayer.cs +++ b/Correction/MiniJeuxFinal/Games/IPlayer.cs @@ -1,6 +1,6 @@ namespace MiniJeuxFinal.Games { - internal interface IPlayer + public interface IPlayer { string Name { get; } diff --git a/Correction/MiniJeuxFinal/Games/PierrePapierCiseaux/PierrePapierCiseauxGame.cs b/Correction/MiniJeuxFinal/Games/PierrePapierCiseaux/PierrePapierCiseauxGame.cs index 3e8c6de..83d9b7d 100644 --- a/Correction/MiniJeuxFinal/Games/PierrePapierCiseaux/PierrePapierCiseauxGame.cs +++ b/Correction/MiniJeuxFinal/Games/PierrePapierCiseaux/PierrePapierCiseauxGame.cs @@ -1,58 +1,63 @@ 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 class PierrePapierCiseauxGame : IGame { - public string Name => "Jeu du Pierre, Papier, Ciseaux 🧱 📄 ✂️"; - private readonly int _totalRounds; - private readonly string _playerName; + private readonly IPlayer _player; + private readonly IPlayer _computer; + 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) - { + + + public PierrePapierCiseauxGame(int totalRound, IPlayer player, IPlayer computer) + { _totalRounds = totalRound; - _playerName = playerName; + _player = player; + _computer = computer; } + public string Name => "Jeu du Pierre, Papier, Ciseaux 🧱 📄 ✂️"; + + + 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) + 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])); + var result = Play(); + if (result != null) + result.IncrementScore(); + + TurnEnded?.Invoke(this, (result, [_player, _computer])); } - GameEnded?.Invoke(this, (player.Score > computer.Score ? player : computer, [player, computer])); + var winner = _player.Score > _computer.Score ? _player : _computer; + GameEnded?.Invoke(this, (winner, [_player, _computer])); + } + + + + private IPlayer? Play() + { + var actionPLayer = (IActionPpc)_player.Play(); + var actionComputer = (IActionPpc)_computer.Play(); + TurnStarted?.Invoke(this, (actionPLayer.Name, actionComputer.Name)); + + if (actionPLayer.ToWin(actionComputer) && actionComputer.ToWin(actionPLayer)) + return null; + + if (actionPLayer.ToWin(actionComputer)) + return _player; + + return _computer; } } } diff --git a/Correction/MiniJeuxFinal/Program.cs b/Correction/MiniJeuxFinal/Program.cs index 907ddae..6a8eb35 100644 --- a/Correction/MiniJeuxFinal/Program.cs +++ b/Correction/MiniJeuxFinal/Program.cs @@ -1,10 +1,22 @@ -using MiniJeuxFinal.Games.PierrePapierCiseaux; +using MiniJeuxFinal.Games; +using MiniJeuxFinal.Games.PierrePapierCiseaux; +using MiniJeuxFinal.Games.PierrePapierCiseaux.Actions; +using MiniJeuxFinal.Games.PierrePapierCiseaux.Factories; +using MiniJeuxFinal.Games.PierrePapierCiseaux.Players; +using MiniJeuxFinal.Wrappers; class Program() { public static void Main() { - var game = new PierrePapierCiseauxGame(3, "loic"); + Console.WriteLine("Entrer votre nom de joueur:"); + var userName = Console.ReadLine(); + var player = new PlayerPpc(userName, new InputActionFactory(new ConsoleService())); + var computer = new PlayerPpc("Ordi", new RandomActionFactory()); + IAction[] lstActions = [new StoneAction(), new PaperAction(), new ScissorsAction()]; + player.AddAction(lstActions); + computer.AddAction(lstActions); + var game = new PierrePapierCiseauxGame(3, player, computer); game.TurnStarted += (sender, tuple) => { Console.WriteLine($"Tu joues : {tuple.playerChoice}");