From 9e1b504451d840bfc7912b41318bd4d6398f5528 Mon Sep 17 00:00:00 2001 From: "jules.lascret" Date: Wed, 15 May 2024 09:49:13 +0200 Subject: [PATCH] Optimized the score method, fixed one major issue on it. Still not working as it should --- Qwirkle/QwirkleClassLibrary/Game.cs | 131 ++++++++++++++++++------- Qwirkle/QwirkleClassLibrary/IPlayer.cs | 4 +- Qwirkle/QwirkleConsoleApp/Program.cs | 3 +- 3 files changed, 103 insertions(+), 35 deletions(-) diff --git a/Qwirkle/QwirkleClassLibrary/Game.cs b/Qwirkle/QwirkleClassLibrary/Game.cs index 088cb03..a29a8f8 100644 --- a/Qwirkle/QwirkleClassLibrary/Game.cs +++ b/Qwirkle/QwirkleClassLibrary/Game.cs @@ -318,54 +318,119 @@ namespace QwirkleClassLibrary return CheckTilesInLine(this.cellUsed, b, x, y) && surroundingCells.Any(cell => cell?.GetTile != null); } - public int GetPlayerScore(Player player, List cellsPlayed, Board board) + // public int GetPlayerScore(Player player, List cellsPlayed, Board b) + // { + // // Compte le nombre de tuiles et ajoute ce nombre au score + // int score = cellsPlayed.Count; + // + // // Check les lignes / colonnes adjacentes aux tuiles placées + // for(int i= 0; i < cellsPlayed.Count; i++) + // { + // // A chaque tour, crée la liste des 4 cellules adjacentes à la cellule sélectionnée + // var surroundingCells = new[] + // { + // b.GetCell(cellsPlayed[i].GetX + 1, cellsPlayed[i].GetY), + // b.GetCell(cellsPlayed[i].GetX - 1, cellsPlayed[i].GetY), + // b.GetCell(cellsPlayed[i].GetX, cellsPlayed[i].GetY + 1), + // b.GetCell(cellsPlayed[i].GetX, cellsPlayed[i].GetY - 1) + // }; + // + // // Parcourt le tableau dans la direction par rapport à ces cellules adjacentes + // foreach (var cell in surroundingCells) + // { + // // Si la cellule adjacente ne contient pas de tuile, on passe à la cellule adjacente suivante + // if (cell?.GetTile == null) break; + // + // if (cellsPlayed.Contains(cell) != true) + // { + // + // var dx = cell.GetX - cellsPlayed[i].GetX; + // var dy = cell.GetY - cellsPlayed[i].GetY; + // + // for (int j = 1; j < b.Rows; j++) + // { + // var extendedCell = b.GetCell(cellsPlayed[i].GetX + j * dx, cellsPlayed[i].GetY + j * dy); + // + // if (extendedCell?.GetTile == null) + // { + // break; + // } + // + // score += 1; + // + // } + // } + // } + // + // } + // + // ScoreBoard.Add(player, score); + // + // return score; + // } + + public int GetPlayerScore(Player player, ReadOnlyCollection cellsPlayed, Board b) { - // Compte le nombre de tuiles et ajoute ce nombre au score int score = cellsPlayed.Count; - // Check les lignes / colonnes adjacentes aux tuiles placées - for(int i= 0; i < cellsPlayed.Count; i++) + foreach (var cell in cellsPlayed) { - // A chaque tour, crée la liste des 4 cellules adjacentes à la cellule sélectionnée - var surroundingCells = new[] - { - board.GetCell(cellsPlayed[i].GetX + 1, cellsPlayed[i].GetY), - board.GetCell(cellsPlayed[i].GetX - 1, cellsPlayed[i].GetY), - board.GetCell(cellsPlayed[i].GetX, cellsPlayed[i].GetY + 1), - board.GetCell(cellsPlayed[i].GetX, cellsPlayed[i].GetY - 1) - }; - - // Parcourt le tableau dans la direction par rapport à ces cellules adjacentes - foreach (var cell in surroundingCells) + score += CalculateAdjacentScore(cell, b, cellsPlayed); + } + + + if(ScoreBoard.TryAdd(player, score) == false) + { + ScoreBoard[player] += score; + } + + return score; + } + + private int CalculateAdjacentScore(Cell cell, Board b, ReadOnlyCollection cellsPlayed) + { + int score = 0; + + var surroundingCells = new[] + { + b.GetCell(cell.GetX + 1, cell.GetY), + b.GetCell(cell.GetX - 1, cell.GetY), + b.GetCell(cell.GetX, cell.GetY + 1), + b.GetCell(cell.GetX, cell.GetY - 1) + }; + + foreach (var adjacentCell in surroundingCells) + { + if (adjacentCell?.GetTile == null || cellsPlayed.Contains(adjacentCell)) { - // Si la cellule adjacente ne contient pas de tuile, on passe à la cellule adjacente suivante - if (cell?.GetTile == null) break; + continue; + } - else if (cellsPlayed.Contains(cell) != true) - { + int dx = adjacentCell.GetX - cell.GetX; + int dy = adjacentCell.GetY - cell.GetY; - var dx = cell.GetX - cellsPlayed[i].GetX; - var dy = cell.GetY - cellsPlayed[i].GetY; + score += CalculateLineScore(cell, dx, dy, b); + } - for (int j = 1; j < board.Rows; j++) - { - var extendedCell = board.GetCell(cellsPlayed[i].GetX + j * dx, cellsPlayed[i].GetY + j * dy); + return score; + } - if (extendedCell?.GetTile == null) - { - break; - } + private int CalculateLineScore(Cell cell, int dx, int dy, Board b) + { + int score = 0; - score += 1; + for (int i = 1; i < b.Rows; i++) + { + var extendedCell = b.GetCell(cell.GetX + i * dx, cell.GetY + i * dy); - } - } + if (extendedCell?.GetTile == null) + { + break; } + score++; } - ScoreBoard.Add(player, score); - return score; } diff --git a/Qwirkle/QwirkleClassLibrary/IPlayer.cs b/Qwirkle/QwirkleClassLibrary/IPlayer.cs index 3bc87ce..cc7e3c6 100644 --- a/Qwirkle/QwirkleClassLibrary/IPlayer.cs +++ b/Qwirkle/QwirkleClassLibrary/IPlayer.cs @@ -1,3 +1,5 @@ +using System.Collections.ObjectModel; + namespace QwirkleClassLibrary; public interface IPlayer @@ -12,5 +14,5 @@ public interface IPlayer public bool SwapTiles(Player player, List tilesToSwap); - public int GetPlayerScore(Player player, List cellsPlayed, Board board); + public int GetPlayerScore(Player player, ReadOnlyCollection cellsPlayed, Board b); } \ No newline at end of file diff --git a/Qwirkle/QwirkleConsoleApp/Program.cs b/Qwirkle/QwirkleConsoleApp/Program.cs index e76c236..e51e200 100644 --- a/Qwirkle/QwirkleConsoleApp/Program.cs +++ b/Qwirkle/QwirkleConsoleApp/Program.cs @@ -136,7 +136,7 @@ static void MenuSwitch(Game game) WriteLine("[1] Place your tiles"); WriteLine("[2] Swap your tiles"); - WriteLine("[3] Skip your turn"); + WriteLine("[3] End your turn / Skip your turn"); enter = Convert.ToInt32(ReadLine()); @@ -150,6 +150,7 @@ static void MenuSwitch(Game game) enter = 3; break; case 3: + WriteLine("Your score on this turn : " + game.GetPlayerScore(game.GetPlayingPlayer(), game.CellsUsed, game.GetBoard())); game.EmptyCellUsed(); return; }