From 48d41e04961b7ba645f69773f8bc9c649c225e8d Mon Sep 17 00:00:00 2001 From: "jules.lascret" Date: Tue, 14 May 2024 17:50:37 +0200 Subject: [PATCH 1/3] just for sonar --- Qwirkle/QwirkleClassLibrary/Game.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/Qwirkle/QwirkleClassLibrary/Game.cs b/Qwirkle/QwirkleClassLibrary/Game.cs index cf9f723..3101e4c 100644 --- a/Qwirkle/QwirkleClassLibrary/Game.cs +++ b/Qwirkle/QwirkleClassLibrary/Game.cs @@ -161,6 +161,12 @@ namespace QwirkleClassLibrary } } + + /// + /// + /// + /// + /// public bool DrawTiles(Player player) { while(player.Tiles.Count < 6) From ef49b7ea53faefd9df5ebf5fe75be523a4bec027 Mon Sep 17 00:00:00 2001 From: "jules.lascret" Date: Tue, 14 May 2024 18:00:25 +0200 Subject: [PATCH 2/3] Splitting methods to optimize --- Qwirkle/QwirkleClassLibrary/Game.cs | 48 ++++++++++++++++----------- Qwirkle/QwirkleClassLibrary/IRules.cs | 2 ++ Qwirkle/QwirkleConsoleApp/Program.cs | 2 +- 3 files changed, 32 insertions(+), 20 deletions(-) diff --git a/Qwirkle/QwirkleClassLibrary/Game.cs b/Qwirkle/QwirkleClassLibrary/Game.cs index 3101e4c..902b2dd 100644 --- a/Qwirkle/QwirkleClassLibrary/Game.cs +++ b/Qwirkle/QwirkleClassLibrary/Game.cs @@ -212,6 +212,31 @@ 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) + { + return false; + } + + if (i == 6) + { + return false; + } + } + + return true; + } public bool IsMoveCorrect(Tile t, int x, int y, Board b) { @@ -220,7 +245,7 @@ namespace QwirkleClassLibrary return true; } - var surroundingCells = new[] + var surroundingCells = new List { b.GetCell(x + 1, y), b.GetCell(x - 1, y), @@ -243,24 +268,9 @@ namespace QwirkleClassLibrary var dx = cell.GetX - x; var dy = cell.GetY - y; - for (int i = 1; i < 7; i++) + if (CheckExtendedSurroundingCells(t, x, y, dx, dy, b) == false) { - var extendedCell = b.GetCell(x + i * dx, y + i * dy); - - if (extendedCell?.GetTile == null) - { - break; - } - - if (extendedCell.GetTile.GetColor != t.GetColor && extendedCell.GetTile.GetShape != t.GetShape) - { - return false; - } - - if (i == 6) - { - return false; - } + return false; } } @@ -270,7 +280,7 @@ namespace QwirkleClassLibrary public bool IsGameOver() { - return false; + return true; } } } \ No newline at end of file diff --git a/Qwirkle/QwirkleClassLibrary/IRules.cs b/Qwirkle/QwirkleClassLibrary/IRules.cs index 75d47a4..40fe258 100644 --- a/Qwirkle/QwirkleClassLibrary/IRules.cs +++ b/Qwirkle/QwirkleClassLibrary/IRules.cs @@ -14,6 +14,8 @@ namespace QwirkleClassLibrary bool IsMoveCorrect(Tile t, int x, int y, Board b); + bool CheckExtendedSurroundingCells(Tile tile, int x, int y, int dx, int dy, Board b); + bool IsGameOver(); } } diff --git a/Qwirkle/QwirkleConsoleApp/Program.cs b/Qwirkle/QwirkleConsoleApp/Program.cs index ef2f2a0..c4bfd54 100644 --- a/Qwirkle/QwirkleConsoleApp/Program.cs +++ b/Qwirkle/QwirkleConsoleApp/Program.cs @@ -195,7 +195,7 @@ static void MainMenu(Game game) game.DrawTiles(game.GetPlayingPlayer()); MenuSwitch(game); - } while (true); //while (game.GetPlayingPlayerPosition() != game.PlayerList.Count - 1); + } while (game.IsGameOver()); //while (game.GetPlayingPlayerPosition() != game.PlayerList.Count - 1); } static void MainGame() From a86a39cbb1d1b7f6c3e1b9bed0f6169e4fc24a9c Mon Sep 17 00:00:00 2001 From: "jules.lascret" Date: Tue, 14 May 2024 19:01:46 +0200 Subject: [PATCH 3/3] I could be a fucking genius --- Qwirkle/QwirkleClassLibrary/Board.cs | 1 - Qwirkle/QwirkleClassLibrary/Game.cs | 43 +++++++++++++++++++++++++-- Qwirkle/QwirkleClassLibrary/IRules.cs | 2 ++ Qwirkle/QwirkleConsoleApp/Program.cs | 2 ++ 4 files changed, 44 insertions(+), 4 deletions(-) diff --git a/Qwirkle/QwirkleClassLibrary/Board.cs b/Qwirkle/QwirkleClassLibrary/Board.cs index 4614b52..81ba4e0 100644 --- a/Qwirkle/QwirkleClassLibrary/Board.cs +++ b/Qwirkle/QwirkleClassLibrary/Board.cs @@ -51,7 +51,6 @@ namespace QwirkleClassLibrary if (cells[i].IsFree == true) { return cells[i].SetTile(tile); - } } return false; diff --git a/Qwirkle/QwirkleClassLibrary/Game.cs b/Qwirkle/QwirkleClassLibrary/Game.cs index 902b2dd..bbf5fa7 100644 --- a/Qwirkle/QwirkleClassLibrary/Game.cs +++ b/Qwirkle/QwirkleClassLibrary/Game.cs @@ -20,6 +20,9 @@ namespace QwirkleClassLibrary public ReadOnlyCollection ScoreList => scores.AsReadOnly(); private readonly List scores = new(); + + public ReadOnlyCollection CellsUsed => cellUsed.AsReadOnly(); + private readonly List cellUsed = new(); public Game() { @@ -80,6 +83,16 @@ namespace QwirkleClassLibrary return true; } + public void AddCellUsed(Cell? c) + { + if (c != null) cellUsed.Add(c); + } + + public void EmptyCellUsed() + { + cellUsed.Clear(); + } + public Player GetPlayingPlayer() { if(GetPlayingPlayerPosition() == -1) @@ -163,7 +176,7 @@ namespace QwirkleClassLibrary /// - /// + /// Allows a player to draw tiles from the bag as soon as he has less than 6 tiles /// /// /// @@ -238,6 +251,30 @@ namespace QwirkleClassLibrary return true; } + public bool CheckTilesInLine(List cells, Board b, int x, int y) + { + if(cells.Count < 2) + { + return true; + } + + var x1 = cells[0].GetX; + var y1 = cells[0].GetY; + var x2 = cells[1].GetX; + var y2 = cells[1].GetY; + + if (x1 == x2) + { + return x == x1; + } + if (y1 == y2) + { + return y == y1; + } + + return false; + } + public bool IsMoveCorrect(Tile t, int x, int y, Board b) { if (!b.HasOccupiedCase()) @@ -274,8 +311,8 @@ namespace QwirkleClassLibrary } } - return surroundingCells.Any(cell => cell?.GetTile != null); - } + return CheckTilesInLine(this.cellUsed, b, x, y) && surroundingCells.Any(cell => cell?.GetTile != null); + } public bool IsGameOver() diff --git a/Qwirkle/QwirkleClassLibrary/IRules.cs b/Qwirkle/QwirkleClassLibrary/IRules.cs index 40fe258..5041b74 100644 --- a/Qwirkle/QwirkleClassLibrary/IRules.cs +++ b/Qwirkle/QwirkleClassLibrary/IRules.cs @@ -16,6 +16,8 @@ namespace QwirkleClassLibrary bool CheckExtendedSurroundingCells(Tile tile, int x, int y, int dx, int dy, Board b); + bool CheckTilesInLine(List cells, Board b, int x, int y); + bool IsGameOver(); } } diff --git a/Qwirkle/QwirkleConsoleApp/Program.cs b/Qwirkle/QwirkleConsoleApp/Program.cs index c4bfd54..e76c236 100644 --- a/Qwirkle/QwirkleConsoleApp/Program.cs +++ b/Qwirkle/QwirkleConsoleApp/Program.cs @@ -76,6 +76,7 @@ static void AddTile(Game game) if (game.PlaceTile(game.GetPlayingPlayer(), tile, x, y) == true) { WriteLine("ok ! your tile is placed"); + game.AddCellUsed(game.GetBoard().GetCell(x, y)); } else { @@ -149,6 +150,7 @@ static void MenuSwitch(Game game) enter = 3; break; case 3: + game.EmptyCellUsed(); return; } }