Refacto pour test ppcGame

This commit is contained in:
Grizouille
2025-12-12 16:30:45 +01:00
parent 7cec5696dc
commit 2bbd8a35df
4 changed files with 55 additions and 38 deletions

View File

@@ -1,6 +1,6 @@
namespace MiniJeuxFinal.Games namespace MiniJeuxFinal.Games
{ {
internal interface IGame public interface IGame
{ {
event EventHandler<(IPlayer winner, IPlayer[] players)>? GameEnded; event EventHandler<(IPlayer winner, IPlayer[] players)>? GameEnded;

View File

@@ -1,6 +1,6 @@
namespace MiniJeuxFinal.Games namespace MiniJeuxFinal.Games
{ {
internal interface IPlayer public interface IPlayer
{ {
string Name { get; } string Name { get; }

View File

@@ -1,58 +1,63 @@
using MiniJeuxFinal.Games.PierrePapierCiseaux.Actions; using MiniJeuxFinal.Games.PierrePapierCiseaux.Actions;
using MiniJeuxFinal.Games.PierrePapierCiseaux.Factories;
using MiniJeuxFinal.Games.PierrePapierCiseaux.Players;
using MiniJeuxFinal.Wrappers;
namespace MiniJeuxFinal.Games.PierrePapierCiseaux 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 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)>? GameEnded;
public event EventHandler<(IPlayer? winner, IPlayer[] players)>? TurnEnded; public event EventHandler<(IPlayer? winner, IPlayer[] players)>? TurnEnded;
public event EventHandler<(string playerChoice, string computerChoice)>? TurnStarted; public event EventHandler<(string playerChoice, string computerChoice)>? TurnStarted;
public PierrePapierCiseauxGame(int totalRound, string playerName)
{
public PierrePapierCiseauxGame(int totalRound, IPlayer player, IPlayer computer)
{
_totalRounds = totalRound; _totalRounds = totalRound;
_playerName = playerName; _player = player;
_computer = computer;
} }
public string Name => "Jeu du Pierre, Papier, Ciseaux 🧱 📄 ✂️";
public void Start() public void Start()
{ {
IAction[] lstActions = [new StoneAction(), new PaperAction(), new ScissorsAction()]; while (_player.Score < _totalRounds && _computer.Score < _totalRounds)
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 result = Play();
var actionComputer = (IActionPpc)computer.Play(); if (result != null)
TurnStarted?.Invoke(this, (actionPLayer.Name, actionComputer.Name)); result.IncrementScore();
if (actionPLayer.ToWin(actionComputer)) TurnEnded?.Invoke(this, (result, [_player, _computer]));
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])); 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;
} }
} }
} }

View File

@@ -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() class Program()
{ {
public static void Main() 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) => game.TurnStarted += (sender, tuple) =>
{ {
Console.WriteLine($"Tu joues : {tuple.playerChoice}"); Console.WriteLine($"Tu joues : {tuple.playerChoice}");