diff --git a/Qwirkle/QwirkleClassLibrary/Board.cs b/Qwirkle/QwirkleClassLibrary/Board.cs index b4c97d5..52a0719 100644 --- a/Qwirkle/QwirkleClassLibrary/Board.cs +++ b/Qwirkle/QwirkleClassLibrary/Board.cs @@ -12,20 +12,37 @@ namespace QwirkleClassLibrary { public ReadOnlyCollection ReadCells => cells.AsReadOnly(); private readonly List cells = new(); + + private int Rows { get; } + private int Columns { get; } - public Board() + public Board(int rows, int cols) { - - for (int i = 0; i < 12; i++) + Rows = rows; + Columns = cols; + + for (int a = 0; a < Rows; a++) { - for (int j = 0; j < 12; j++) + for (int b = 0; b < Columns; b++) { - Cell localcell = new(i, j); + Cell localcell = new(a, b); cells.Add(localcell); } } } + public bool HasOccupiedCase() + { + foreach (var cell in cells) + { + if (cell.IsFree == false) + { + return true; + } + } + return false; + } + public bool AddTileInCell(int x, int y, Tile tile) { for (int i = 0; i < cells.Count; i++) @@ -49,5 +66,17 @@ namespace QwirkleClassLibrary { return ReadCells; } + + public Cell? GetCell(int x, int y) + { + for (int i = 0; i < cells.Count; i++) + { + if (this.cells[i].GetX == x && this.cells[i].GetY == y) + { + return cells[i]; + } + } + return null; + } } } diff --git a/Qwirkle/QwirkleClassLibrary/Game.cs b/Qwirkle/QwirkleClassLibrary/Game.cs index 68360de..d87d83f 100644 --- a/Qwirkle/QwirkleClassLibrary/Game.cs +++ b/Qwirkle/QwirkleClassLibrary/Game.cs @@ -11,7 +11,7 @@ namespace QwirkleClassLibrary { public class Game : IPlayer, IRules { - private readonly TileBag bag; + private TileBag bag; public bool GameRunning { get; private set; } private Board board; @@ -23,7 +23,7 @@ namespace QwirkleClassLibrary public Game() { - bag = new TileBag(3); + bag = CreateTileBag(3); board = CreateBoard(); } @@ -61,18 +61,21 @@ namespace QwirkleClassLibrary public Board CreateBoard() { - board = new Board(); + board = new Board(12, 12); return board; } + + public TileBag CreateTileBag(int nbSet) + { + bag = new TileBag(nbSet); + return bag; + } public bool StartGame() { - if(players.Count>= 2 && players.Count < 5) - { - this.GameRunning = true; - return true; - } - return false; + if (players.Count < 2 || players.Count >= 5) return false; + this.GameRunning = true; + return true; } public Player GetPlayingPlayer() @@ -202,14 +205,39 @@ namespace QwirkleClassLibrary return true; } - public bool IsMoveCorrect(Tile t, Board b) + public bool IsMoveCorrect(Tile t, int x, int y, Board b) { - return false; + if (b.HasOccupiedCase() == false) + { + return true; + } + + var surroundingTiles = new List(); + + surroundingTiles.Add(b.GetCell(x + 1, y)?.GetTile); + surroundingTiles.Add(b.GetCell(x - 1, y)?.GetTile); + surroundingTiles.Add(b.GetCell(x, y + 1)?.GetTile); + surroundingTiles.Add(b.GetCell(x, y - 1)?.GetTile); + + foreach (var tile in surroundingTiles) + { + if (tile == null) + { + continue; + } + + if (tile.GetColor != t.GetColor && tile.GetShape != t.GetShape) + { + return false; + } + } + + return true; } public bool IsGameOver() { - throw new NotImplementedException(); + return false; } } } \ No newline at end of file