From f60eca5a729ff84195f58b087ca8221436ecb055 Mon Sep 17 00:00:00 2001 From: rportet Date: Tue, 14 May 2024 19:19:40 +0200 Subject: [PATCH] adding player score counting in game, adding class in IPlayer :D --- Qwirkle/QwirkleClassLibrary/Game.cs | 57 +++++++++++++++++++++++++- Qwirkle/QwirkleClassLibrary/IPlayer.cs | 2 + 2 files changed, 58 insertions(+), 1 deletion(-) diff --git a/Qwirkle/QwirkleClassLibrary/Game.cs b/Qwirkle/QwirkleClassLibrary/Game.cs index cf9f723..da0769d 100644 --- a/Qwirkle/QwirkleClassLibrary/Game.cs +++ b/Qwirkle/QwirkleClassLibrary/Game.cs @@ -6,11 +6,15 @@ using System.Text; using System.Threading.Tasks; using System.Xml.Linq; using System.Security.Cryptography; +using System.Collections; namespace QwirkleClassLibrary { + public class Game : IPlayer, IRules { + + public Dictionary ScoreBoard = new(); private TileBag bag; public bool GameRunning { get; private set; } private Board board; @@ -261,7 +265,58 @@ namespace QwirkleClassLibrary return surroundingCells.Any(cell => cell?.GetTile != null); } - + public int GetPlayerScore(Player player, List cellsPlayed, Board board) + { + // 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[] + { + 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) + { + // Si la cellule adjacente ne contient pas de tuile, on passe à la cellule adjacente suivante + if (cell?.GetTile == null) break; + + else if (cellsPlayed.Contains(cell) != true) + { + + var dx = cell.GetX - cellsPlayed[i].GetX; + var dy = cell.GetY - cellsPlayed[i].GetY; + + for (int j = 1; j < board.Rows; j++) + { + var extendedCell = board.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 bool IsGameOver() { return false; diff --git a/Qwirkle/QwirkleClassLibrary/IPlayer.cs b/Qwirkle/QwirkleClassLibrary/IPlayer.cs index 264ea7c..3bc87ce 100644 --- a/Qwirkle/QwirkleClassLibrary/IPlayer.cs +++ b/Qwirkle/QwirkleClassLibrary/IPlayer.cs @@ -11,4 +11,6 @@ public interface IPlayer public bool DrawTiles(Player player); public bool SwapTiles(Player player, List tilesToSwap); + + public int GetPlayerScore(Player player, List cellsPlayed, Board board); } \ No newline at end of file