Move generation calculation to Board

This commit is contained in:
Andrew Cooper
2022-09-07 21:37:28 +10:00
parent 2ccbdcc98d
commit 96a0d7bee5
2 changed files with 34 additions and 41 deletions

View File

@@ -1,3 +1,5 @@
using System.Collections.Immutable;
namespace LifeforTwo;
internal class Board
@@ -7,6 +9,12 @@ internal class Board
private const int Player2 = 0x1000;
private const int PieceMask = Player1 | Player2;
private const int NeighbourValueOffset = 8;
private readonly ImmutableHashSet<int> _willBePlayer1 =
new[] { 0x0003, 0x0102, 0x0103, 0x0120, 0x0130, 0x0121, 0x0112, 0x0111, 0x0012 }.ToImmutableHashSet();
private readonly ImmutableHashSet<int> _willBePlayer2 =
new[] { 0x0021, 0x0030, 0x1020, 0x1030, 0x1011, 0x1021, 0x1003, 0x1002, 0x1012 }.ToImmutableHashSet();
private readonly int[,] _cells = new int[7,7];
public int this[Coordinates coordinates]
@@ -21,6 +29,30 @@ internal class Board
set => _cells[x, y] = value;
}
public (int Player1Count, int Player2Count) CalculateNextGeneration()
{
var _cellCounts = new Dictionary<int, int>() { [Empty] = 0, [Player1] = 0, [Player2] = 0 };
for (var x = 1; x <= 5; x++)
{
for (var y = 1; y <= 5; y++)
{
var currentValue = this[x, y];
var newValue = currentValue switch
{
_ when _willBePlayer1.Contains(currentValue) => Player1,
_ when _willBePlayer2.Contains(currentValue) => Player2,
_ => Empty
};
this[x, y] = newValue;
_cellCounts[newValue]++;
}
}
return (_cellCounts[Player1], _cellCounts[Player2]);
}
public void CalculateNeighbours()
{
for (var x = 1; x <= 5; x++)