From 69f9b3d404bafa5c8a5a62385c66a64d1046f077 Mon Sep 17 00:00:00 2001 From: "jules.lascret" Date: Wed, 15 May 2024 10:33:07 +0200 Subject: [PATCH] Fixed a lot of code smelt --- Qwirkle/QwirkleClassLibrary/Board.cs | 2 +- Qwirkle/QwirkleClassLibrary/Game.cs | 88 ++++++---------------------- Qwirkle/QwirkleClassLibrary/Score.cs | 13 ++-- Qwirkle/QwirkleClassLibrary/Tile.cs | 2 +- Qwirkle/QwirkleConsoleApp/Program.cs | 12 ++-- Qwirkle/TestBase/TestScore.cs | 2 +- Qwirkle/TestBase/TestTileBag.cs | 8 +-- 7 files changed, 37 insertions(+), 90 deletions(-) diff --git a/Qwirkle/QwirkleClassLibrary/Board.cs b/Qwirkle/QwirkleClassLibrary/Board.cs index 81ba4e0..8638e00 100644 --- a/Qwirkle/QwirkleClassLibrary/Board.cs +++ b/Qwirkle/QwirkleClassLibrary/Board.cs @@ -48,7 +48,7 @@ namespace QwirkleClassLibrary for (int i = 0; i < cells.Count; i++) { if (this.cells[i].GetX != x || this.cells[i].GetY != y) continue; - if (cells[i].IsFree == true) + if (cells[i].IsFree) { return cells[i].SetTile(tile); } diff --git a/Qwirkle/QwirkleClassLibrary/Game.cs b/Qwirkle/QwirkleClassLibrary/Game.cs index 3ae76e4..30dca81 100644 --- a/Qwirkle/QwirkleClassLibrary/Game.cs +++ b/Qwirkle/QwirkleClassLibrary/Game.cs @@ -13,8 +13,8 @@ namespace QwirkleClassLibrary public class Game : IPlayer, IRules { - - public Dictionary ScoreBoard = new(); + private Dictionary scoreBoard = new(); + private TileBag bag; public bool GameRunning { get; private set; } private Board board; @@ -49,13 +49,13 @@ namespace QwirkleClassLibrary public bool AddPlayerInGame(string? playerTag) { - if (string.IsNullOrWhiteSpace(playerTag) == true) + if (string.IsNullOrWhiteSpace(playerTag)) { OnPlayerNotified(new AddPlayerNotifiedEventArgs("ERROR : The name is null or white space.")); return false; } - if(this.GameRunning == true) + if(this.GameRunning) { OnPlayerNotified(new AddPlayerNotifiedEventArgs("ERROR : The game is running.")); return false; @@ -134,7 +134,7 @@ namespace QwirkleClassLibrary { for (int i = 0; i < players.Count; i++) { - if (players[i].IsPlaying == true) + if (players[i].IsPlaying) { return i; } @@ -163,7 +163,7 @@ namespace QwirkleClassLibrary public string SetFirstPlayer() { - if (GameRunning == true) + if (GameRunning) { players[0].IsPlaying = true; //OnNextPlayer(new NextPlayerNotifiedEventArgs(players[0])); @@ -194,9 +194,9 @@ namespace QwirkleClassLibrary public bool PlaceTile(Player player, Tile tile, int x, int y) { - if (board.AddTileInCell(x, y, tile) == true) + if (board.AddTileInCell(x, y, tile)) { - return player.RemoveTileToPlayer(tile) == true; + return player.RemoveTileToPlayer(tile); } else { @@ -269,13 +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")); + OnPlaceTile(new PlaceTileNotifiedEventArgs(tile, "Color / Shape does not match with the surrounding tiles !")); return false; } if (i == 6) { - OnPlaceTile(new PlaceTileNotifiedEventArgs(tile, "La ligne ou colonne fait deja 6 tuiles !")); + OnPlaceTile(new PlaceTileNotifiedEventArgs(tile, "Row/Column already 6 tiles long !")); return false; } } @@ -311,10 +311,10 @@ namespace QwirkleClassLibrary { if (!b.HasOccupiedCase()) { - if (this.PlaceTile(this.GetPlayingPlayer(), t, x, y) == true) + if (this.PlaceTile(this.GetPlayingPlayer(), t, x, y)) { this.AddCellUsed(this.GetBoard().GetCell(x, y)); - OnPlaceTile(new PlaceTileNotifiedEventArgs(t, "Tile correctement placé")); + OnPlaceTile(new PlaceTileNotifiedEventArgs(t, "Tile correctly placed")); return true; } } @@ -342,16 +342,13 @@ namespace QwirkleClassLibrary var dx = cell.GetX - x; var dy = cell.GetY - y; - - if (CheckExtendedSurroundingCells(t, x, y, dx, dy, b) == false) - { - return false; - } + + return CheckExtendedSurroundingCells(t, x, y, dx, dy, b); } if (CheckTilesInLine(this.cellUsed, b, x, y) && surroundingCells.Any(cell => cell?.GetTile != null)) { - if(this.PlaceTile(this.GetPlayingPlayer(), t, x, y) == true) + if(this.PlaceTile(this.GetPlayingPlayer(), t, x, y)) { this.AddCellUsed(this.GetBoard().GetCell(x, y)); OnPlaceTile(new PlaceTileNotifiedEventArgs(t, "Tile correctement placé")); @@ -363,57 +360,6 @@ namespace QwirkleClassLibrary return false; } - // 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) { int score = cellsPlayed.Count; @@ -424,9 +370,9 @@ namespace QwirkleClassLibrary } - if(ScoreBoard.TryAdd(player, score) == false) + if(scoreBoard.TryAdd(player, score) == false) { - ScoreBoard[player] += score; + scoreBoard[player] += score; } return score; diff --git a/Qwirkle/QwirkleClassLibrary/Score.cs b/Qwirkle/QwirkleClassLibrary/Score.cs index 50822f2..4bb3253 100644 --- a/Qwirkle/QwirkleClassLibrary/Score.cs +++ b/Qwirkle/QwirkleClassLibrary/Score.cs @@ -8,19 +8,18 @@ namespace QwirkleClassLibrary { public struct Score { - private int score { get; set; } - public string playerTag { get; } + private int PlayerScore { get; set; } + public string PlayerTag { get; } - public Score(Player p) + public Score(Player? p) { if (p == null) { - throw new ArgumentException(); + throw new NullReferenceException(); } else - { - score = 0; - playerTag = p.NameTag; + { PlayerScore = 0; + PlayerTag = p.NameTag; } } } diff --git a/Qwirkle/QwirkleClassLibrary/Tile.cs b/Qwirkle/QwirkleClassLibrary/Tile.cs index f6adb00..76a509d 100644 --- a/Qwirkle/QwirkleClassLibrary/Tile.cs +++ b/Qwirkle/QwirkleClassLibrary/Tile.cs @@ -12,11 +12,11 @@ namespace QwirkleClassLibrary { private readonly Shape shape; private readonly Color color; + public Tile(Shape sh, Color co) { shape = sh; color = co; - //Console.WriteLine("A tile of shape " + shape + " and color " + color + " has been created."); } public string NameColorTile() diff --git a/Qwirkle/QwirkleConsoleApp/Program.cs b/Qwirkle/QwirkleConsoleApp/Program.cs index eb6ec76..099c12b 100644 --- a/Qwirkle/QwirkleConsoleApp/Program.cs +++ b/Qwirkle/QwirkleConsoleApp/Program.cs @@ -42,7 +42,7 @@ static void ShowTiles(Game game) var stringBuilder = new StringBuilder(); - for (int i = 0; i < currentPlayer.Tiles.Count(); i++) + for (int i = 0; i < currentPlayer.Tiles.Count; i++) { stringBuilder.Append("[" + (i + 1) + "] "); stringBuilder.AppendLine(currentPlayer.Tiles[i].ToString()); @@ -94,7 +94,7 @@ static void SwapTile(Game game) ShowTiles(game); - while (continueSwap == true) + while (continueSwap) { Write("Enter the number of the tile you want to swap : "); int no = Convert.ToInt32(ReadLine()); @@ -165,7 +165,6 @@ static void MenuSwitch(Game game) static void ShowBoard(Game game) { Board board = game.GetBoard(); - List cells = board.GetCells(); for(int i = 0; i(() => new Score(null)); return; }*/ - Player p = new Player("test"); + Player? p = new Player("test"); Score score = new Score(p); Assert.True(true); } diff --git a/Qwirkle/TestBase/TestTileBag.cs b/Qwirkle/TestBase/TestTileBag.cs index 350f0f6..e6556bf 100644 --- a/Qwirkle/TestBase/TestTileBag.cs +++ b/Qwirkle/TestBase/TestTileBag.cs @@ -22,11 +22,11 @@ public class TestTileBag [Fact] public void Test_AddTileInBag() { - Tile t = null; + Tile? t = null; Tile tok = new(Shape.Club, Color.Green); TileBag bag = new TileBag(2); - if (bag.AddTileInBag(t) == false) + if (t != null && bag.AddTileInBag(t) == false) { Assert.True(bag.AddTileInBag(tok)); @@ -37,12 +37,12 @@ public class TestTileBag [Fact] public void Test_RemoveTileInBag() { - Tile t = null; + Tile? t = null; Tile tok = new(Shape.Club, Color.Green); TileBag bag = new TileBag(2); bag.AddTileInBag(tok); - if (bag.RemoveTileInBag(t) == false) + if (t != null && bag.RemoveTileInBag(t) == false) { Assert.True(bag.RemoveTileInBag(tok));