Score counting at 95% done + fix swap tiles issue + optimized score counting methods
continuous-integration/drone/push Build is failing Details

test_old_branch
Jules LASCRET 11 months ago
parent a7ecb591d3
commit 960af83a9f

@ -359,6 +359,11 @@ namespace QwirkleClassLibrary.Games
return false; return false;
} }
if (bag!.TilesBag.Count < tilesToSwap.Count)
{
return false;
}
foreach (var t in tilesToSwap) foreach (var t in tilesToSwap)
{ {
if (!player.RemoveTileToPlayer(t)) if (!player.RemoveTileToPlayer(t))
@ -617,9 +622,11 @@ namespace QwirkleClassLibrary.Games
b.GetCell(cell.GetX, cell.GetY - 1) b.GetCell(cell.GetX, cell.GetY - 1)
}; };
var checkedSurroundingCells = new List<Cell>();
foreach (var adjacentCell in surroundingCells) foreach (var adjacentCell in surroundingCells)
{ {
if (adjacentCell?.GetTile == null || cellsPlayed.Contains(adjacentCell)) if (adjacentCell?.GetTile == null || cellsPlayed.Contains(adjacentCell) || checkedSurroundingCells.Contains(adjacentCell))
{ {
continue; continue;
} }
@ -627,7 +634,9 @@ namespace QwirkleClassLibrary.Games
int dx = adjacentCell.GetX - cell.GetX; int dx = adjacentCell.GetX - cell.GetX;
int dy = adjacentCell.GetY - cell.GetY; int dy = adjacentCell.GetY - cell.GetY;
score += CalculateLineScore(cellsPlayed, cell, dx, dy, b, cellsX, cellsY, ref nbCellsInLine); score += CalculateLineScore(cellsPlayed, cell, new Tuple<int, int>(dx, dy), b, new Tuple<int, int>(cellsX, cellsY), ref nbCellsInLine);
checkedSurroundingCells.Add(adjacentCell);
} }
return score; return score;
@ -638,27 +647,25 @@ namespace QwirkleClassLibrary.Games
/// </summary> /// </summary>
/// <param name="cellsPlayed"></param> /// <param name="cellsPlayed"></param>
/// <param name="cell"></param> /// <param name="cell"></param>
/// <param name="dx"></param> /// <param name="direction"></param>
/// <param name="dy"></param>
/// <param name="b"></param> /// <param name="b"></param>
/// <param name="cellsX"></param> /// <param name="orientation"></param>
/// <param name="cellsY"></param>
/// <param name="nbCellsInLine"></param> /// <param name="nbCellsInLine"></param>
/// <returns>int</returns> /// <returns>int</returns>
public int CalculateLineScore(ReadOnlyCollection<Cell> cellsPlayed, Cell cell, int dx, int dy, Board b, int cellsX, int cellsY, ref int nbCellsInLine) public int CalculateLineScore(ReadOnlyCollection<Cell> cellsPlayed, Cell cell, Tuple<int, int> direction, Board b, Tuple<int, int> orientation, ref int nbCellsInLine)
{ {
int score = 0; int score = 0;
for (int i = 1; i < 6; i++) for (int i = 1; i < 6; i++)
{ {
var extendedCell = b.GetCell(cell.GetX + i * dx, cell.GetY + i * dy); var extendedCell = b.GetCell(cell.GetX + i * direction.Item1, cell.GetY + i * direction.Item2);
if (extendedCell?.GetTile == null || cellsPlayed.Contains(extendedCell)) if (extendedCell?.GetTile == null || cellsPlayed.Contains(extendedCell))
{ {
break; break;
} }
if (dx != 0 && cellsX == -1 || dy != 0 && cellsY == -1) if (direction.Item1 != 0 && orientation.Item1 == -1 || direction.Item2 != 0 && orientation.Item2 == -1)
{ {
nbCellsInLine++; nbCellsInLine++;
} }
@ -666,7 +673,7 @@ namespace QwirkleClassLibrary.Games
score++; score++;
} }
if (dx == 0 && cellsX == -1 && cellsY != -1 || dy == 0 && cellsY == -1 && cellsX != -1) if (direction.Item1 == 0 && orientation.Item1 == -1 && orientation.Item2 != -1 || direction.Item2 == 0 && orientation.Item2 == -1 && orientation.Item1 != -1)
{ {
score += 1; score += 1;
} }

@ -23,6 +23,5 @@ public interface IPlayer
int CalculateAdjacentScore(Cell cell, Board b, ReadOnlyCollection<Cell> cellsPlayed, int cellsX, int cellsY, ref int nbCellsInLine); int CalculateAdjacentScore(Cell cell, Board b, ReadOnlyCollection<Cell> cellsPlayed, int cellsX, int cellsY, ref int nbCellsInLine);
int CalculateLineScore(ReadOnlyCollection<Cell> cellsPlayed, Cell cell, int dx, int dy, Board b, int cellsX, int CalculateLineScore(ReadOnlyCollection<Cell> cellsPlayed, Cell cell, Tuple<int, int> direction, Board b, Tuple<int, int> orientation, ref int nbCellsInLine);
int cellsY, ref int nbCellsInLine);
} }

@ -24,18 +24,31 @@ namespace QwirkleClassLibrary.Tiles
throw new ArgumentException(nbSet.ToString()); throw new ArgumentException(nbSet.ToString());
} }
for (int i = 0; i < nbSet; i++) // for (int i = 0; i < nbSet; i++)
{ // {
foreach (Shape s in Enum.GetValues(typeof(Shape))) // foreach (Shape s in Enum.GetValues(typeof(Shape)))
{ // {
foreach (Color c in Enum.GetValues(typeof(Color))) // foreach (Color c in Enum.GetValues(typeof(Color)))
{ // {
Tile t = new Tile(s, c); // Tile t = new Tile(s, c);
tiles.Add(t); // tiles.Add(t);
} // }
} // }
} // }
tiles.Add(new Tile(Shape.Club, Color.Blue));
tiles.Add(new Tile(Shape.Club, Color.Green));
tiles.Add(new Tile(Shape.Club, Color.Orange));
tiles.Add(new Tile(Shape.Club, Color.Purple));
tiles.Add(new Tile(Shape.Club, Color.Red));
tiles.Add(new Tile(Shape.Club, Color.Yellow));
tiles.Add(new Tile(Shape.Square, Color.Blue));
tiles.Add(new Tile(Shape.Square, Color.Green));
tiles.Add(new Tile(Shape.Square, Color.Orange));
tiles.Add(new Tile(Shape.Square, Color.Purple));
tiles.Add(new Tile(Shape.Square, Color.Red));
tiles.Add(new Tile(Shape.Square, Color.Yellow));
TilesBag = tiles.AsReadOnly(); TilesBag = tiles.AsReadOnly();
} }

Loading…
Cancel
Save