LINQ optimizations + fix exception for the ShowBoard in Program.cs
continuous-integration/drone/push Build is passing Details

test_old_branch
Jules LASCRET 11 months ago
parent c99a0fec4e
commit 0b237dacd2

@ -46,14 +46,15 @@ namespace QwirkleClassLibrary.Boards
public bool AddTileInCell(int x, int y, Tile tile) public bool AddTileInCell(int x, int y, Tile tile)
{ {
for (int i = 0; i < cells.Count; i++) foreach (var t in cells)
{ {
if (cells[i].GetX != x || cells[i].GetY != y) continue; if (t.GetX != x || t.GetY != y) continue;
if (cells[i].IsFree) if (t.IsFree)
{ {
return cells[i].SetTile(tile); return t.SetTile(tile);
} }
} }
return false; return false;
} }
@ -69,13 +70,14 @@ namespace QwirkleClassLibrary.Boards
public Cell? GetCell(int x, int y) public Cell? GetCell(int x, int y)
{ {
for (int i = 0; i < cells.Count; i++) foreach (var t in cells)
{ {
if (cells[i].GetX == x && cells[i].GetY == y) if (t.GetX == x && t.GetY == y)
{ {
return cells[i]; return t;
} }
} }
return null; return null;
} }
} }

@ -107,7 +107,7 @@ namespace QwirkleClassLibrary.Games
public Board CreateBoard() public Board CreateBoard()
{ {
board = new Board(8, 8); board = new Board(15, 12);
return board; return board;
} }
@ -117,11 +117,10 @@ namespace QwirkleClassLibrary.Games
return bag; return bag;
} }
public bool StartGame() public void StartGame()
{ {
if (players.Count < 2 || players.Count >= 5) return false; if (players.Count < 2 || players.Count >= 5) return;
GameRunning = true; GameRunning = true;
return true;
} }
public void AddCellUsed(Cell? c) public void AddCellUsed(Cell? c)
@ -431,10 +430,7 @@ namespace QwirkleClassLibrary.Games
} }
} }
foreach (var cell in cellsPlayed) score += cellsPlayed.Sum(cell => CalculateAdjacentScore(cell, b, cellsPlayed, cellsX, cellsY));
{
score += CalculateAdjacentScore(cell, b, cellsPlayed, cellsX, cellsY);
}
if (!scoreBoard.TryAdd(player, score)) if (!scoreBoard.TryAdd(player, score))

@ -226,12 +226,14 @@ static void ShowBoard(Game game)
{ {
for (int y = 0; y < board.Columns; y++) for (int y = 0; y < board.Columns; y++)
{ {
if (board.GetCell(y, i)!.IsFree == false) Cell? cell = board.GetCell(y, i);
if (cell != null && cell.IsFree == false)
{ {
Tile? tile = board.GetCell(y, i)?.GetTile; Tile? tile = cell.GetTile;
if (tile != null) if (tile != null)
{ {
Console.Write("| " + tile.GetShape.ToString()[0] + tile.GetShape.ToString()[1] + tile.GetColor.ToString()[0] + " |"); Console.Write("| " + tile.GetShape.ToString()[0] + tile.GetShape.ToString()[1] +
tile.GetColor.ToString()[0] + " |");
} }
} }
else else
@ -239,6 +241,7 @@ static void ShowBoard(Game game)
Console.Write("| |"); Console.Write("| |");
} }
} }
Console.WriteLine(); Console.WriteLine();
} }
} }

Loading…
Cancel
Save