diff --git a/Qwirkle/QwirkleClassLibrary/Games/Game.cs b/Qwirkle/QwirkleClassLibrary/Games/Game.cs index fdb3e4f..5d001cc 100644 --- a/Qwirkle/QwirkleClassLibrary/Games/Game.cs +++ b/Qwirkle/QwirkleClassLibrary/Games/Game.cs @@ -200,7 +200,7 @@ namespace QwirkleClassLibrary.Games players[i].IsPlaying = false; players[(i + 1) % players.Count].IsPlaying = true; - OnNextPlayer(new NextPlayerNotifiedEventArgs(players[i])); + OnNextPlayer(new NextPlayerNotifiedEventArgs(players[(i + 1) % players.Count])); return players[GetPlayingPlayerPosition()].NameTag; } @@ -369,7 +369,7 @@ namespace QwirkleClassLibrary.Games if (cell.GetTile.GetColor == t.GetColor && cell.GetTile.GetShape == t.GetShape) { - OnPlaceTile(new PlaceTileNotifiedEventArgs(t, " : Tile already placed on the same line / column !")); + OnPlaceTile(new PlaceTileNotifiedEventArgs(t, " is already placed on the same line / column !")); return false; } @@ -382,6 +382,7 @@ namespace QwirkleClassLibrary.Games return false; } } + if (!CheckTilesInLine(cellUsed, b, x, y)) { @@ -389,18 +390,36 @@ namespace QwirkleClassLibrary.Games return false; } - return surroundingCells.Any(cell => cell?.GetTile != null); + if (!surroundingCells.Any(cell => cell?.GetTile != null)) + { + OnPlaceTile(new PlaceTileNotifiedEventArgs(t, " : You can't place a tile that isn't adjacent to another one !")); + return false; + } + + return true; } - public int GetPlayerScore(Player player, ReadOnlyCollection cellsPlayed, Board b) +public int GetPlayerScore(Player player, ReadOnlyCollection cellsPlayed, Board b) { int score = cellsPlayed.Count; + + int cellsX = cellsPlayed[0].GetX; + int cellsY = cellsPlayed[0].GetY; foreach (var cell in cellsPlayed) { - score += CalculateAdjacentScore(cell, b, cellsPlayed); + if (cellsX != cell.GetX && cellsX != -1) + { + cellsX = -1; + } + else if (cellsY != cell.GetY && cellsY != -1) + { + cellsY = -1; + } + + score += CalculateAdjacentScore(cell, b, cellsPlayed, cellsX, cellsY); } @@ -412,7 +431,7 @@ namespace QwirkleClassLibrary.Games return score; } - private static int CalculateAdjacentScore(Cell cell, Board b, ReadOnlyCollection cellsPlayed) + public int CalculateAdjacentScore(Cell cell, Board b, ReadOnlyCollection cellsPlayed, int cellsX, int cellsY) { int score = 0; @@ -433,23 +452,14 @@ namespace QwirkleClassLibrary.Games int dx = adjacentCell.GetX - cell.GetX; int dy = adjacentCell.GetY - cell.GetY; - - if (adjacentCell.GetX == cell.GetX) - { - score += CalculateLineScore(cell, 0, dy, b); - - } - else if (adjacentCell.GetY == cell.GetY) - { - score += CalculateLineScore(cell, dx, 0, b); - - } + + score += CalculateLineScore(cell, dx, dy, b, cellsX, cellsY); } return score; } - private static int CalculateLineScore(Cell cell, int dx, int dy, Board b) + public int CalculateLineScore(Cell cell, int dx, int dy, Board b, int cellsX, int cellsY) { int score = 0; @@ -465,12 +475,18 @@ namespace QwirkleClassLibrary.Games score++; } + if (dx == 0 && cellsX == -1 || dy == 0 && cellsY == -1) + { + score += 1; + } + return score; } + public List CheckTilesBag() { - List PlayerTilesBagPos = []; + List playerTilesBagPos = []; if (bag.TilesBag.Count == 0) { @@ -478,27 +494,27 @@ namespace QwirkleClassLibrary.Games { if (players[i].Tiles.Count != 0) { - PlayerTilesBagPos.Add(i); + playerTilesBagPos.Add(i); } } } - return PlayerTilesBagPos; + return playerTilesBagPos; } - public bool CheckBoardTile(List PlayerTilesBagPos) + public bool CheckBoardTile(List playerTilesBagPos) { - for (int i = 0; i < PlayerTilesBagPos.Count; i++) + for (int i = 0; i < playerTilesBagPos.Count; i++) { - for (int j = 0; j < players[PlayerTilesBagPos[i]].Tiles.Count; j++) + for (int j = 0; j < players[playerTilesBagPos[i]].Tiles.Count; j++) { for (int b = 0; b < board.ReadCells.Count; b++) { int x = board.ReadCells[b].GetX; int y = board.ReadCells[b].GetY; - if (IsMoveCorrect(players[PlayerTilesBagPos[i]].Tiles[j], x, y, board)) + if (IsMoveCorrect(players[playerTilesBagPos[i]].Tiles[j], x, y, board)) { return true; } @@ -512,9 +528,9 @@ namespace QwirkleClassLibrary.Games public bool CheckGameOver(Player player) { - List PlayerTilesBagPos = CheckTilesBag(); + List playerTilesBagPos = CheckTilesBag(); - if (PlayerTilesBagPos.Count != 0 && !CheckBoardTile(PlayerTilesBagPos)) + if (playerTilesBagPos.Count != 0 && !CheckBoardTile(playerTilesBagPos)) { OnEndOfGame(new EndOfGameNotifiedEventArgs(player)); GameRunning = false; diff --git a/Qwirkle/QwirkleClassLibrary/Games/IPlayer.cs b/Qwirkle/QwirkleClassLibrary/Games/IPlayer.cs index 3135127..c2fe694 100644 --- a/Qwirkle/QwirkleClassLibrary/Games/IPlayer.cs +++ b/Qwirkle/QwirkleClassLibrary/Games/IPlayer.cs @@ -20,4 +20,8 @@ public interface IPlayer public bool SwapTiles(Player player, List tilesToSwap); public int GetPlayerScore(Player player, ReadOnlyCollection cellsPlayed, Board b); + + int CalculateAdjacentScore(Cell cell, Board b, ReadOnlyCollection cellsPlayed, int cellsX, int cellsY); + + int CalculateLineScore(Cell cell, int dx, int dy, Board b, int cellsX, int cellsY); } \ No newline at end of file