?
continuous-integration/drone/push Build is failing
Details
continuous-integration/drone/push Build is failing
Details
commit
3d6a3a9503
@ -0,0 +1,11 @@
|
||||
{
|
||||
"profiles": {
|
||||
"QwirkleConsoleApp": {
|
||||
"commandName": "Project"
|
||||
},
|
||||
"WSL": {
|
||||
"commandName": "WSL2",
|
||||
"distributionName": ""
|
||||
}
|
||||
}
|
||||
}
|
@ -0,0 +1,127 @@
|
||||
using QwirkleClassLibrary;
|
||||
namespace TestBase;
|
||||
|
||||
public class TestGame
|
||||
{
|
||||
|
||||
|
||||
[Theory]
|
||||
[InlineData(true, false, "testt")]
|
||||
[InlineData(false, false, "testt")]
|
||||
public void Test_GameAddPlayerIngame(bool result, bool gamestate, string p)
|
||||
{
|
||||
Game game = new Game();
|
||||
|
||||
if (!result) {
|
||||
game.AddPlayerInGame(p);
|
||||
Assert.False(game.AddPlayerInGame(p));
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert.True(game.AddPlayerInGame(p));
|
||||
}
|
||||
|
||||
if (gamestate)
|
||||
{
|
||||
Assert.False(game.AddPlayerInGame(p));
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(false, null)]
|
||||
[InlineData(true, "test")]
|
||||
public void Test_GameAddPlayerIngame2(bool result, string p)
|
||||
{
|
||||
Game game = new Game();
|
||||
|
||||
if (!result)
|
||||
{
|
||||
Assert.False(game.AddPlayerInGame(p));
|
||||
}
|
||||
|
||||
for (int i = 0; i < 4; i++)
|
||||
{
|
||||
string name = p + i;
|
||||
game.AddPlayerInGame(name);
|
||||
}
|
||||
Assert.False(game.AddPlayerInGame(p));
|
||||
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData("test")]
|
||||
public void Test_GameCreatePlayers(string p)
|
||||
{
|
||||
Game game = new Game();
|
||||
Player player = new Player(p);
|
||||
Assert.Equal(game.CreatePlayer(p).NameTag, player.NameTag);
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(true, "test1", "test2")]
|
||||
[InlineData(false, "test1", "test2")]
|
||||
public void Test_GameState(bool result, string p1, string p2)
|
||||
{
|
||||
Game game = new Game();
|
||||
|
||||
if (!result)
|
||||
{
|
||||
game.StartGame();
|
||||
Assert.False(game.GameRunning);
|
||||
}
|
||||
game.AddPlayerInGame(p1);
|
||||
game.AddPlayerInGame(p2);
|
||||
game.StartGame();
|
||||
|
||||
Assert.True(game.GameRunning);
|
||||
}
|
||||
|
||||
|
||||
[Theory]
|
||||
[InlineData(true, "test1", "test2", "test3")]
|
||||
[InlineData(false, "test1", "test2", "test3")]
|
||||
public void Test_GameGetPlayingPlayerPosition(bool result, string p1, string p2, string p3)
|
||||
{
|
||||
Game game = new Game();
|
||||
game.AddPlayerInGame(p1);
|
||||
game.AddPlayerInGame(p2);
|
||||
game.AddPlayerInGame(p3);
|
||||
|
||||
if (!result)
|
||||
{
|
||||
Assert.Equal(-1, game.GetPlayingPlayerPosition());
|
||||
return;
|
||||
}
|
||||
game.StartGame();
|
||||
game.SetFirstPlayer();
|
||||
Assert.Equal(0, game.GetPlayingPlayerPosition());
|
||||
}
|
||||
|
||||
[Theory]
|
||||
[InlineData(true)]
|
||||
[InlineData(false)]
|
||||
public void Test_GameGetPlaylingPlayer(bool result)
|
||||
{
|
||||
Game game = new Game();
|
||||
game.AddPlayerInGame("patrick");
|
||||
game.AddPlayerInGame("jean");
|
||||
if (!result)
|
||||
{
|
||||
Assert.Throws<ArgumentException>(() => game.GetPlayingPlayer());
|
||||
}
|
||||
game.StartGame();
|
||||
game.SetFirstPlayer();
|
||||
|
||||
Assert.Equal(game.PlayerList[0], game.GetPlayingPlayer());
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,30 @@
|
||||
using QwirkleClassLibrary;
|
||||
namespace TestBase;
|
||||
|
||||
public class TestScore
|
||||
{
|
||||
[Theory]
|
||||
[InlineData(false)]
|
||||
//[InlineData(true)]
|
||||
public void Test_CellScoreConstructor(bool except)
|
||||
{
|
||||
/*if (!except)
|
||||
{
|
||||
Assert.Throws<ArgumentNullException>(() => new Score(null));
|
||||
return;
|
||||
}*/
|
||||
Player p = new Player("test");
|
||||
Score score = new Score(p);
|
||||
Assert.True(true);
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
@ -0,0 +1,35 @@
|
||||
public interface IDisplayer
|
||||
{
|
||||
void OnGameStart(Board board);
|
||||
void OnPlayerNotified(Player player);
|
||||
void OnMoveChosen(Player player, int row, int col);
|
||||
void OnValidMove(Player player, Board board, int row, int col, bool isValid);
|
||||
void OnBoardChanged(Board board);
|
||||
}
|
||||
|
||||
public class ConsoleDisplayer : IDisplayer
|
||||
{
|
||||
public void OnGameStarted(Board board)
|
||||
{
|
||||
Console.WriteLine("Game has started !");
|
||||
DisplayBoard(board);
|
||||
}
|
||||
|
||||
public void DisplayBoard(Board board)
|
||||
{
|
||||
for(int row = 0; row < board.NbRows; row++)
|
||||
{
|
||||
for(int col = 0; col < board.NbCols; col++)
|
||||
{
|
||||
var playerSymbol = board[row, col] ?.Player == 1 ? "X" : "O";
|
||||
Console.Write($"{playerSymbol} ")
|
||||
}
|
||||
Console.WriteLine();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// program.cs
|
||||
|
||||
OnGameStarted += _ => Console.WriteLine("Game has started !"); // j'ai pas compris mais ça à l'air complètement fumé
|
@ -0,0 +1,128 @@
|
||||
// Cours sur les évènements
|
||||
|
||||
/*
|
||||
Pour la faire courte, y a 3 possibilités pour les affichages :
|
||||
- les Console.WriteLine qui sont absolument interdits dans les classes de logique (Game, Board, Rules, Player)
|
||||
- l'implémentations d'une interface IDisplayer qui permet de définir des méthodes qui seront appelées par les classes de logique
|
||||
- Définition du type délégué (delegate) => ça prend un board, ça rend rien
|
||||
-
|
||||
|
||||
*/
|
||||
|
||||
|
||||
public class Game // Game.cs file
|
||||
{
|
||||
public /*delegate*/ event void GameStarted(); // c'est pas une méthode, c'est un type de méthode !!!
|
||||
// le event est une propriété qui permet de s'abonner à un évènement : elle empêche de faire =
|
||||
|
||||
|
||||
public event EventHandler<GameStarted> GameStarted;
|
||||
protected virtual void OnGameStarted()
|
||||
{
|
||||
GameStarted?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
private GameStarted onGameStarted;
|
||||
|
||||
private Board board { get; init; } // je sais pas ce que fait le init
|
||||
|
||||
private IRules Rules { get; init; }
|
||||
|
||||
private Player Player1 { get; init; }
|
||||
private Player Player2 { get; init; }
|
||||
|
||||
private IDisplayer displayer
|
||||
|
||||
public Game(Irules rules, Player player1, Player player2)
|
||||
{
|
||||
Player1 = player1;
|
||||
Player2 = player2;
|
||||
Rules = rules;
|
||||
Board = rules.InitBoard();
|
||||
}
|
||||
|
||||
{
|
||||
|
||||
}
|
||||
|
||||
public void start()
|
||||
{
|
||||
displayer.OnGameStart(Board);
|
||||
|
||||
Console.WriteLine("Game started !");
|
||||
while(true)
|
||||
{
|
||||
// Get next player
|
||||
Player nextPlayer = Rules.GetNextPlayer(Board) == 1 ? Player1 : Player2;
|
||||
// displayer.OnPlayerNotified(nextPlayer);
|
||||
onGameStarted(board);
|
||||
|
||||
// Notify player to make a move
|
||||
(int chosenRow, int chosenCol) = nextPlayer.ChooseMove(Board, Rules);
|
||||
displayer.OnMoveChosen(nextPlayer, chosenRow, chosenCol);
|
||||
|
||||
// If move is valid (check with rules)
|
||||
if(Rules.IsValidMove(Board, chosenRow, chosenCol, nextPlayer.Id))
|
||||
{
|
||||
// true => modify board
|
||||
Board.InsertPiece(chosenRow, chosenCol, nextPlayer.Id);
|
||||
displayer.OnValidMove(nextPlayer, Board, chosenRow, chosenCol, true);
|
||||
|
||||
displayer.OnGameOver(Board, nextPlayer, Rules);
|
||||
|
||||
// is game over? (check with rules)
|
||||
if(Rules.IsGameOver(Board, chosenRow, chosenCol, out int winner, out Case[] winningPlaces))
|
||||
{
|
||||
Console.WriteLine($"Player {winner} won the game !");
|
||||
|
||||
for(int row = 0; row < Board.NbRows; row++)
|
||||
{
|
||||
for(int col = 0; col < Board.NbCols; col++)
|
||||
{
|
||||
Console.Write($"{playerSymbol} ")
|
||||
}
|
||||
Console.WriteLine();
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
}
|
||||
// gets the winner, winning places, return
|
||||
// false => nothing
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
public abstract class Player // Player.cs file
|
||||
{
|
||||
public string Name { get; private set; }
|
||||
|
||||
public int Id { get; private init; }
|
||||
|
||||
public Player(string name, int id)
|
||||
{
|
||||
Id = id;
|
||||
Name = name;
|
||||
}
|
||||
|
||||
public abstract (int row, int col) ChooseMove(Board board, IRules rules);
|
||||
}
|
||||
|
||||
public class RandomPlayer : Player // RandomPlayer.cs file
|
||||
{
|
||||
public RandomPlayer(int id) : base(id, "Jérôme")
|
||||
{
|
||||
}
|
||||
|
||||
public static Random random = new Random();
|
||||
|
||||
public override (int row, int col) ChooseMove(Board board, IRules rules)
|
||||
{
|
||||
var moves = rules.GetPossibleMoves(board, Id);
|
||||
var chosenMoveId = random.Next(0, moves.Count());
|
||||
Case chosenCase = moves.ElementAt(chosenMoveId);
|
||||
|
||||
return (chosenCase.Row, chosenCase.Col);
|
||||
}
|
||||
}
|
Loading…
Reference in new issue