diff --git a/Qwirkle/QwirkleClassLibrary/Games/Game.cs b/Qwirkle/QwirkleClassLibrary/Games/Game.cs index 9e5bf92..b4d8f61 100644 --- a/Qwirkle/QwirkleClassLibrary/Games/Game.cs +++ b/Qwirkle/QwirkleClassLibrary/Games/Game.cs @@ -164,7 +164,7 @@ namespace QwirkleClassLibrary.Games /// Returns the Board of the game /// /// Board - public Board? GetBoard() + public Board GetBoard() { return board; } @@ -362,8 +362,8 @@ namespace QwirkleClassLibrary.Games return false; } - if (!IsMoveCorrect(tile, x, y, board!)) return false; - if (board!.AddTileInCell(x, y, tile)) + if (!IsMoveCorrect(tile, x, y, board)) return false; + if (board.AddTileInCell(x, y, tile)) { AddCellUsed(board.GetCell(x, y)); return player.RemoveTileToPlayer(tile); @@ -479,7 +479,7 @@ namespace QwirkleClassLibrary.Games previousTilesFound = true; } - if (extendedCell?.Tile == null) + if (extendedCell?.Tile! == null!) { break; } @@ -552,9 +552,16 @@ namespace QwirkleClassLibrary.Games return false; } - public static bool CheckTileInCompletedLines(Tile? t1, ref int nbTiles, ref List checkdoubles) + /// + /// Check that there isn't any same tile on a said line when a tile is forming a line + /// + /// + /// + /// + /// + private static bool CheckTileInCompletedLines(Tile? t1, ref int nbTiles, ref List checkdoubles) { - if (t1 != null) + if (t1! != null!) { nbTiles++; @@ -569,7 +576,18 @@ namespace QwirkleClassLibrary.Games return true; } - public bool CheckWrongCompletedLines(Tile tile, int x, int y, int dx, int dy, Board b, ref List checkdoubles) + /// + /// Check if the line is completed with the tile placed + /// + /// + /// + /// + /// + /// + /// + /// + /// + private static bool CheckWrongCompletedLines(int x, int y, int dx, int dy, Board b, ref List checkdoubles) { int nbTiles = 1; @@ -578,7 +596,7 @@ namespace QwirkleClassLibrary.Games var extendedCell = b.GetCell(x + i * dx, y + i * dy); var extendedCell2 = b.GetCell(x - i * dx, y - i * dy); - if (extendedCell?.Tile == null && extendedCell2?.Tile == null) + if (extendedCell?.Tile! == null! && extendedCell2?.Tile! == null!) { break; } @@ -591,30 +609,17 @@ namespace QwirkleClassLibrary.Games return nbTiles <= 6; } - - - /// - /// Main method to check if the move the player is trying to make is correct - /// - /// - /// - /// - /// - /// bool public bool IsMoveCorrect(Tile t, int x, int y, Board b) { - bool previousTilesFound = false; - - var checkDoubles = new List(); - if (!b.HasOccupiedCase()) { return true; } - if (b.GetCell(x, y)!.Tile != null) + if (b.GetCell(x, y)!.Tile! != null!) { OnPlaceTile(new PlaceTileNotifiedEventArgs(t, " : Cell already used !")); + return false; } var surroundingCells = new List @@ -624,10 +629,26 @@ namespace QwirkleClassLibrary.Games b.GetCell(x, y + 1), b.GetCell(x, y - 1) }; + + if (surroundingCells.All(cell => cell?.Tile! == null!)) + { + OnPlaceTile(new PlaceTileNotifiedEventArgs(t, + " : You can't place a tile that isn't adjacent to another one !")); + return false; + } + + return IsTilePlacementCorrect(t, x, y, b, surroundingCells); + } + + public bool IsTilePlacementCorrect(Tile t, int x, int y, Board b, List surroundingCells) + { + bool previousTilesFound = false; + + var checkDoubles = new List(); foreach (var cell in surroundingCells) { - if (cell?.Tile == null) + if (cell?.Tile! == null!) { continue; } @@ -642,7 +663,6 @@ namespace QwirkleClassLibrary.Games if (cell.Tile.GetColor == t.GetColor && cell.Tile.GetShape == t.GetShape) { OnPlaceTile(new PlaceTileNotifiedEventArgs(t, " is already placed on the same line / column !")); - return false; } @@ -654,12 +674,12 @@ namespace QwirkleClassLibrary.Games return false; } - if (CheckWrongCompletedLines(t, x, y, dx, dy, b, ref checkDoubles)) continue; - OnPlaceTile(new PlaceTileNotifiedEventArgs(t, " : You can't complete this line ! (More than 6 tiles / same tiles on the line)")); + if (CheckWrongCompletedLines(x, y, dx, dy, b, ref checkDoubles)) continue; + OnPlaceTile(new PlaceTileNotifiedEventArgs(t, + " : You can't complete this line ! (More than 6 tiles / same tiles on the line)")); return false; } - if (!CheckTilesInLine(cellUsed, b, x, y)) { OnPlaceTile(new PlaceTileNotifiedEventArgs(t, @@ -667,18 +687,10 @@ namespace QwirkleClassLibrary.Games return false; } - if (surroundingCells.All(cell => cell?.Tile == null)) - { - OnPlaceTile(new PlaceTileNotifiedEventArgs(t, - " : You can't place a tile that isn't adjacent to another one !")); - return false; - } - if (previousTilesFound) return true; OnPlaceTile(new PlaceTileNotifiedEventArgs(t, " : You must place your tile next / on the same line as the ones previously placed !")); return false; - } @@ -771,7 +783,7 @@ namespace QwirkleClassLibrary.Games foreach (var adjacentCell in surroundingCells) { - if (adjacentCell?.Tile == null || cellsPlayed.Contains(adjacentCell) || + if (adjacentCell?.Tile! == null! || cellsPlayed.Contains(adjacentCell) || checkedSurroundingCells.Contains(adjacentCell)) { continue; @@ -808,7 +820,7 @@ namespace QwirkleClassLibrary.Games { var extendedCell = b.GetCell(cell.GetX + i * direction.Item1, cell.GetY + i * direction.Item2); - if (extendedCell?.Tile == null || cellsPlayed.Contains(extendedCell)) + if (extendedCell?.Tile! == null! || cellsPlayed.Contains(extendedCell)) { break; } @@ -864,7 +876,7 @@ namespace QwirkleClassLibrary.Games { foreach (var t in players[t1].Tiles) { - for (int b = 0; b < board!.ReadCells.Count; b++) + for (int b = 0; b < board.ReadCells.Count; b++) { int x = board.ReadCells[b].GetX; int y = board.ReadCells[b].GetY; diff --git a/Qwirkle/QwirkleClassLibrary/Tiles/Tile.cs b/Qwirkle/QwirkleClassLibrary/Tiles/Tile.cs index de68ec4..4752c67 100644 --- a/Qwirkle/QwirkleClassLibrary/Tiles/Tile.cs +++ b/Qwirkle/QwirkleClassLibrary/Tiles/Tile.cs @@ -41,19 +41,13 @@ namespace QwirkleClassLibrary.Tiles /// A getter for the shape of the Tile. /// /// The shape attribute of the Tile. - public Shape GetShape - { - get { return shape; } - } + public Shape GetShape => shape; /// /// A getter for the color of the Tile. /// /// The color attribute of the Tile. - public Color GetColor - { - get { return color; } - } + public Color GetColor => color; /// /// This method is used to override the ToString() method. It is simply a tool to facilitate the development. @@ -66,20 +60,66 @@ namespace QwirkleClassLibrary.Tiles public int CompareTo(object? obj) { - if (obj == null) return 1; + if (obj == null) + { + throw new NullReferenceException(); + } var otherTile = obj as Tile; - if (otherTile != null) + + if (color == otherTile!.color) { - if (color == otherTile.color) - { - return shape.CompareTo(otherTile.shape); - } + return shape.CompareTo(otherTile.shape); + } + + return color.CompareTo(otherTile.color); + + } - return color.CompareTo(otherTile.color); + public override bool Equals(object? obj) + { + if (obj == null || GetType() != obj.GetType()) + { + return false; } - throw new ArgumentException("Object is not a Tile"); + var otherTile = obj as Tile; + return color == otherTile!.color && shape == otherTile.shape; + } + + public override int GetHashCode() + { + return HashCode.Combine(color, shape); + } + + public static bool operator ==(Tile tile1, Tile tile2) + { + return EqualityComparer.Default.Equals(tile1, tile2); + } + + public static bool operator !=(Tile tile1, Tile tile2) + { + return !(tile1 == tile2); + } + + public static bool operator <(Tile tile1, Tile tile2) + { + return tile1.CompareTo(tile2) < 0; + } + + public static bool operator >(Tile tile1, Tile tile2) + { + return tile1.CompareTo(tile2) > 0; + } + + public static bool operator <=(Tile tile1, Tile tile2) + { + return tile1.CompareTo(tile2) <= 0; + } + + public static bool operator >=(Tile tile1, Tile tile2) + { + return tile1.CompareTo(tile2) >= 0; } } } \ No newline at end of file