Move neighbour calculation

This commit is contained in:
Andrew Cooper
2022-09-02 07:54:18 +10:00
parent 99fb001f6c
commit b156755ee0
2 changed files with 24 additions and 20 deletions

View File

@@ -2,6 +2,10 @@ namespace LifeforTwo;
internal class Board
{
private const int Player1Piece = 100;
private const int Player2Piece = 1000;
private const int Player1Neighbour = 1;
private const int Player2Neighbour = 10;
private readonly int[,] _cells = new int[7,7];
public int this[Coordinates coordinates]
@@ -15,4 +19,23 @@ internal class Board
get => _cells[x, y];
set => _cells[x, y] = value;
}
public void CalculateNeighbours()
{
for (var x = 1; x <= 5; x++)
{
for (var y = 1; y <= 5; y++)
{
var coordinates = new Coordinates(x, y);
if (this[coordinates] >= Player1Piece)
{
int _playerPiece = this[coordinates] > Player2Piece ? Player2Neighbour : Player1Neighbour;
foreach (var neighbour in coordinates.GetNeighbors())
{
this[neighbour] += _playerPiece;
}
}
}
}
}
}

View File

@@ -11,25 +11,6 @@ var _willLive = new[] { 3, 102, 103, 120, 130, 121, 112, 111, 12, 21, 30, 1020,
var _coordinates = new Coordinates[3];
int _player1Count, _player2Count;
void CalculateNeighbors()
{
for (var x = 1; x <= 5; x++)
{
for (var y = 1; y <= 5; y++)
{
var coordinates = new Coordinates(x, y);
if (_board[coordinates] > 99)
{
int B = _board[coordinates] > 999 ? 10 : 1;
foreach (var neighbor in coordinates.GetNeighbors())
{
_board[neighbor] += B;
}
}
}
}
}
void CalculateAndDisplayNext()
{
_player1Count = _player2Count = 0;
@@ -96,7 +77,7 @@ CalculateAndDisplayNext();
while (true)
{
io.WriteLine();
CalculateNeighbors();
_board.CalculateNeighbours();
CalculateAndDisplayNext();
if (_player1Count == 0 || _player2Count == 0) { break; }