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; namespace LifeforTwo;
internal class Board internal class Board
@@ -7,6 +9,12 @@ internal class Board
private const int Player2 = 0x1000; private const int Player2 = 0x1000;
private const int PieceMask = Player1 | Player2; private const int PieceMask = Player1 | Player2;
private const int NeighbourValueOffset = 8; 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]; private readonly int[,] _cells = new int[7,7];
public int this[Coordinates coordinates] public int this[Coordinates coordinates]
@@ -21,6 +29,30 @@ internal class Board
set => _cells[x, y] = value; 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() public void CalculateNeighbours()
{ {
for (var x = 1; x <= 5; x++) for (var x = 1; x <= 5; x++)

View File

@@ -7,48 +7,9 @@ var io = new ConsoleIO();
io.Write(Streams.Title); io.Write(Streams.Title);
var _board = new Board(); var _board = new Board();
var _willLive = new[] { 0x0003, 0x0102, 0x0103, 0x0120, 0x0130, 0x0121, 0x0112, 0x0111, 0x0012,
0x0021, 0x0030, 0x1020, 0x1030, 0x1011, 0x1021, 0x1003, 0x1002, 0x1012 };
var _coordinates = new Coordinates[3]; var _coordinates = new Coordinates[3];
int _player1Count, _player2Count; int _player1Count, _player2Count;
void CalculateNext()
{
_player1Count = _player2Count = 0;
for (var y = 1; y <= 5; y++)
{
for (var x = 1; x <= 5; x++)
{
CalculateNextCell(x, y);
}
}
return;
}
void CalculateNextCell(int x, int y)
{
if (_board[x, y] >= 3)
{
for (var o = 0; o < 18; o++)
{
if (_board[x, y] == _willLive[o])
{
if (o < 9)
{
_board[x, y] = 0x0100; _player1Count++;
}
else
{
_board[x, y] = 0x1000; _player2Count++;
}
return;
}
}
}
_board[x, y] = 0;
}
for (var _player = 1; _player <= 2; _player++) for (var _player = 1; _player <= 2; _player++)
{ {
var P1 = _player == 2 ? 0x30 : 0x03; var P1 = _player == 2 ? 0x30 : 0x03;
@@ -60,14 +21,14 @@ for (var _player = 1; _player <= 2; _player++)
} }
} }
CalculateNext(); _board.CalculateNextGeneration();
_board.Display(io); _board.Display(io);
while (true) while (true)
{ {
io.WriteLine(); io.WriteLine();
_board.CalculateNeighbours(); _board.CalculateNeighbours();
CalculateNext(); (_player1Count, _player2Count) = _board.CalculateNextGeneration();
_board.Display(io); _board.Display(io);
if (_player1Count == 0 || _player2Count == 0) { break; } if (_player1Count == 0 || _player2Count == 0) { break; }