Refacto pour test ppcGame
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
namespace MiniJeuxFinal.Games
|
||||
{
|
||||
internal interface IGame
|
||||
public interface IGame
|
||||
{
|
||||
event EventHandler<(IPlayer winner, IPlayer[] players)>? GameEnded;
|
||||
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
namespace MiniJeuxFinal.Games
|
||||
{
|
||||
internal interface IPlayer
|
||||
public interface IPlayer
|
||||
{
|
||||
string Name { get; }
|
||||
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user