From 8724cc88edd7ffa300cf1ec584f4f27b0cfbb770 Mon Sep 17 00:00:00 2001 From: "jeremy.mouyon" Date: Thu, 16 May 2024 09:59:20 +0200 Subject: [PATCH] no test, just back of script by jules --- Qwirkle/QwirkleClassLibrary/Game.cs | 95 ++++++++++++++++++++++------ Qwirkle/QwirkleConsoleApp/Program.cs | 19 ++++-- 2 files changed, 91 insertions(+), 23 deletions(-) diff --git a/Qwirkle/QwirkleClassLibrary/Game.cs b/Qwirkle/QwirkleClassLibrary/Game.cs index 3c29e84..97813b2 100644 --- a/Qwirkle/QwirkleClassLibrary/Game.cs +++ b/Qwirkle/QwirkleClassLibrary/Game.cs @@ -255,13 +255,46 @@ namespace QwirkleClassLibrary return true; } - + + /* public bool CheckExtendedSurroundingCells(Tile tile, int x, int y, int dx, int dy, Board b) + { + for (int i = 1; i < 7; i++) + { + var extendedCell = b.GetCell(x + i * dx, y + i * dy); + + if (extendedCell?.GetTile == null) + { + break; + } + + if (extendedCell.GetTile.GetColor != tile.GetColor && extendedCell.GetTile.GetShape != tile.GetShape) + { + OnPlaceTile(new PlaceTileNotifiedEventArgs(tile, "Color / Shape does not match with the surrounding tiles !")); + return false; + } + + if (i == 6) + { + OnPlaceTile(new PlaceTileNotifiedEventArgs(tile, "Row/Column already 6 tiles long !")); + return false; + } + } + + if (this.PlaceTile(this.GetPlayingPlayer(), tile, x, y)) + { + this.AddCellUsed(this.GetBoard().GetCell(x, y)); + OnPlaceTile(new PlaceTileNotifiedEventArgs(tile, "Tile correctement placé")); + return true; + } + return false; + }*/ + public bool CheckExtendedSurroundingCells(Tile tile, int x, int y, int dx, int dy, Board b) { for (int i = 1; i < 7; i++) { var extendedCell = b.GetCell(x + i * dx, y + i * dy); - + if (extendedCell?.GetTile == null) { break; @@ -269,24 +302,16 @@ namespace QwirkleClassLibrary if (extendedCell.GetTile.GetColor != tile.GetColor && extendedCell.GetTile.GetShape != tile.GetShape) { - OnPlaceTile(new PlaceTileNotifiedEventArgs(tile, "Color / Shape does not match with the surrounding tiles !")); return false; } if (i == 6) { - OnPlaceTile(new PlaceTileNotifiedEventArgs(tile, "Row/Column already 6 tiles long !")); return false; } } - if (this.PlaceTile(this.GetPlayingPlayer(), tile, x, y)) - { - this.AddCellUsed(this.GetBoard().GetCell(x, y)); - OnPlaceTile(new PlaceTileNotifiedEventArgs(tile, "Tile correctement placé")); - return true; - } - return false; + return true; } public bool CheckTilesInLine(List cells, Board b, int x, int y) @@ -313,16 +338,11 @@ namespace QwirkleClassLibrary return false; } - public bool IsMoveCorrect(Tile t, int x, int y, Board b) + /*public bool IsMoveCorrect(Tile t, int x, int y, Board b) { if (!b.HasOccupiedCase()) { - if (this.PlaceTile(this.GetPlayingPlayer(), t, x, y)) - { - this.AddCellUsed(this.GetBoard().GetCell(x, y)); - OnPlaceTile(new PlaceTileNotifiedEventArgs(t, "Tile correctly placed")); return true; - } } var surroundingCells = new List @@ -364,7 +384,46 @@ namespace QwirkleClassLibrary } OnPlaceTile(new PlaceTileNotifiedEventArgs(t, "La tuile n'a pu etre placé")); return false; - } + } */ + + public bool IsMoveCorrect(Tile t, int x, int y, Board b) + { + if (!b.HasOccupiedCase()) + { + return true; + } + + var surroundingCells = new List + { + b.GetCell(x + 1, y), + b.GetCell(x - 1, y), + b.GetCell(x, y + 1), + b.GetCell(x, y - 1) + }; + + foreach (var cell in surroundingCells) + { + if (cell?.GetTile == null) + { + continue; + } + + if (cell.GetTile.GetColor != t.GetColor && cell.GetTile.GetShape != t.GetShape) + { + return false; + } + + var dx = cell.GetX - x; + var dy = cell.GetY - y; + + if (CheckExtendedSurroundingCells(t, x, y, dx, dy, b) == false) + { + return false; + } + } + + return CheckTilesInLine(this.cellUsed, b, x, y) && surroundingCells.Any(cell => cell?.GetTile != null); + } public int GetPlayerScore(Player player, ReadOnlyCollection cellsPlayed, Board b) { diff --git a/Qwirkle/QwirkleConsoleApp/Program.cs b/Qwirkle/QwirkleConsoleApp/Program.cs index 099c12b..25511dd 100644 --- a/Qwirkle/QwirkleConsoleApp/Program.cs +++ b/Qwirkle/QwirkleConsoleApp/Program.cs @@ -75,16 +75,25 @@ 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; + /* game.IsMoveCorrect(tile, x, y, game.GetBoard()); + game.PlaceTileNotified -= nc.NotificationAddTile;*/ } static void SwapTile(Game game)