diff --git a/Qwirkle/QwirkleClassLibrary/Game.cs b/Qwirkle/QwirkleClassLibrary/Game.cs index e8a51bc..2e780d7 100644 --- a/Qwirkle/QwirkleClassLibrary/Game.cs +++ b/Qwirkle/QwirkleClassLibrary/Game.cs @@ -35,6 +35,11 @@ namespace QwirkleClassLibrary protected virtual void OnNextPlayer(NextPlayerNotifiedEventArgs args) => NextPlayerNotified?.Invoke(this, args); + public event EventHandler? PlaceTileNotified; + + protected virtual void OnPlaceTile(PlaceTileNotifiedEventArgs args) + => PlaceTileNotified?.Invoke(this, args); + public Game() { bag = CreateTileBag(3); @@ -264,11 +269,13 @@ namespace QwirkleClassLibrary if (extendedCell.GetTile.GetColor != tile.GetColor && extendedCell.GetTile.GetShape != tile.GetShape) { + OnPlaceTile(new PlaceTileNotifiedEventArgs(tile, "Les couleurs/formes adjacentes ne correspondes pas")); return false; } if (i == 6) { + OnPlaceTile(new PlaceTileNotifiedEventArgs(tile, "La ligne ou colonne fait deja 6 tuiles !")); return false; } } @@ -304,7 +311,12 @@ namespace QwirkleClassLibrary { if (!b.HasOccupiedCase()) { - return true; + if (this.PlaceTile(this.GetPlayingPlayer(), t, x, y) == true) + { + this.AddCellUsed(this.GetBoard().GetCell(x, y)); + OnPlaceTile(new PlaceTileNotifiedEventArgs(t, "Tile correctement placé")); + return true; + } } var surroundingCells = new List @@ -324,6 +336,7 @@ namespace QwirkleClassLibrary if (cell.GetTile.GetColor != t.GetColor && cell.GetTile.GetShape != t.GetShape) { + OnPlaceTile(new PlaceTileNotifiedEventArgs(t, "Tile ne correspond pas au bonne couleur des tiles a coté")); return false; } @@ -336,7 +349,18 @@ namespace QwirkleClassLibrary } } - return CheckTilesInLine(this.cellUsed, b, x, y) && surroundingCells.Any(cell => cell?.GetTile != null); + if (CheckTilesInLine(this.cellUsed, b, x, y) && surroundingCells.Any(cell => cell?.GetTile != null)) + { + if(this.PlaceTile(this.GetPlayingPlayer(), t, x, y) == true) + { + this.AddCellUsed(this.GetBoard().GetCell(x, y)); + OnPlaceTile(new PlaceTileNotifiedEventArgs(t, "Tile correctement placé")); + return true; + } + OnPlaceTile(new PlaceTileNotifiedEventArgs(t, "Cell Already use")); + } + OnPlaceTile(new PlaceTileNotifiedEventArgs(t, "La tuile n'a pu etre placé")); + return false; } public int GetPlayerScore(Player player, List cellsPlayed, Board board) diff --git a/Qwirkle/QwirkleClassLibrary/NextPlayerNotifiedEventArgs.cs b/Qwirkle/QwirkleClassLibrary/NextPlayerNotifiedEventArgs.cs index 6c0f6aa..d406f4b 100644 --- a/Qwirkle/QwirkleClassLibrary/NextPlayerNotifiedEventArgs.cs +++ b/Qwirkle/QwirkleClassLibrary/NextPlayerNotifiedEventArgs.cs @@ -2,7 +2,7 @@ { public class NextPlayerNotifiedEventArgs : EventArgs { - public Player player { get; set; } + public Player player { get; private set; } public NextPlayerNotifiedEventArgs(Player player) { diff --git a/Qwirkle/QwirkleClassLibrary/PlaceTileNotifiedEventArgs.cs b/Qwirkle/QwirkleClassLibrary/PlaceTileNotifiedEventArgs.cs new file mode 100644 index 0000000..2abbd8a --- /dev/null +++ b/Qwirkle/QwirkleClassLibrary/PlaceTileNotifiedEventArgs.cs @@ -0,0 +1,15 @@ +namespace QwirkleClassLibrary +{ + public class PlaceTileNotifiedEventArgs : EventArgs + { + public Tile tile { get; private set; } + + public string reason { get; private set; } + + public PlaceTileNotifiedEventArgs(Tile tile, string reason) + { + this.tile = tile; + this.reason = reason; + } + } +} \ No newline at end of file diff --git a/Qwirkle/QwirkleConsoleApp/NotificationClass.cs b/Qwirkle/QwirkleConsoleApp/NotificationClass.cs index 953e9d6..138479d 100644 --- a/Qwirkle/QwirkleConsoleApp/NotificationClass.cs +++ b/Qwirkle/QwirkleConsoleApp/NotificationClass.cs @@ -18,5 +18,10 @@ namespace QwirkleConsoleApp { Console.WriteLine(args.player.NameTag + "'s turn"); } + + public void NotificationAddTile(object? sender, PlaceTileNotifiedEventArgs args) + { + Console.WriteLine("The tile [" + args.tile.ToString() + "] " + args.reason); + } } } diff --git a/Qwirkle/QwirkleConsoleApp/Program.cs b/Qwirkle/QwirkleConsoleApp/Program.cs index 6eb7a4f..fc739b3 100644 --- a/Qwirkle/QwirkleConsoleApp/Program.cs +++ b/Qwirkle/QwirkleConsoleApp/Program.cs @@ -53,6 +53,11 @@ static void ShowTiles(Game game) static void AddTile(Game game) { + + NotificationClass nc = new NotificationClass(); + + game.PlaceTileNotified += nc.NotificationAddTile; + Tile? tile = null; Write("Enter the number of the tile you want to place : "); int no = Convert.ToInt32(ReadLine()); @@ -70,22 +75,16 @@ static void AddTile(Game game) Write("Enter the y of the cell : "); int y = Convert.ToInt32(ReadLine()); - if (game.IsMoveCorrect(tile, x, y, game.GetBoard()) == true) +/* if (game.IsMoveCorrect(tile, x, y, game.GetBoard()) == true) { if (game.PlaceTile(game.GetPlayingPlayer(), tile, x, y) == true) { WriteLine("ok ! your tile is placed"); - game.AddCellUsed(game.GetBoard().GetCell(x, y)); - } - else - { - WriteLine("ERROR : Cell already used"); - } - } - else - { - WriteLine("ERROR : Move not correct"); - } + + }*/ + + game.IsMoveCorrect(tile, x, y, game.GetBoard()); + game.PlaceTileNotified -= nc.NotificationAddTile; } static void SwapTile(Game game)