Move display to Board

This commit is contained in:
Andrew Cooper
2022-09-07 20:41:54 +10:00
parent 28736b9dbe
commit 2ccbdcc98d
2 changed files with 38 additions and 23 deletions

View File

@@ -2,7 +2,10 @@ namespace LifeforTwo;
internal class Board internal class Board
{ {
private const int PieceMask = 0x1100; private const int Empty = 0x0000;
private const int Player1 = 0x0100;
private const int Player2 = 0x1000;
private const int PieceMask = Player1 | Player2;
private const int NeighbourValueOffset = 8; private const int NeighbourValueOffset = 8;
private readonly int[,] _cells = new int[7,7]; private readonly int[,] _cells = new int[7,7];
@@ -36,4 +39,27 @@ internal class Board
} }
} }
} }
public void Display(IReadWrite io)
{
for (var y = 0; y <= 6; y++)
{
io.WriteLine();
for (var x = 0; x <= 6; x++)
{
io.Write(GetDisplay(x, y));
}
}
}
private string GetDisplay(int x, int y) =>
(x, y, this[x, y]) switch
{
(0 or 6, _, _) => $" {y % 6} ",
(_, 0 or 6, _) => $" {x % 6} ",
(_, _, Empty) => " ",
(_, _, Player1) => " * ",
(_, _, Player2) => " # ",
_ => throw new InvalidOperationException($"Unexpected cell value at ({x}, {y}): {this[x, y]}")
};
} }

View File

@@ -12,32 +12,20 @@ var _willLive = new[] { 0x0003, 0x0102, 0x0103, 0x0120, 0x0130, 0x0121, 0x0112,
var _coordinates = new Coordinates[3]; var _coordinates = new Coordinates[3];
int _player1Count, _player2Count; int _player1Count, _player2Count;
void CalculateAndDisplayNext() void CalculateNext()
{ {
_player1Count = _player2Count = 0; _player1Count = _player2Count = 0;
for (var y = 0; y <= 6; y++) for (var y = 1; y <= 5; y++)
{ {
io.WriteLine(); for (var x = 1; x <= 5; x++)
for (var x = 0; x <= 6; x++)
{ {
if (y % 6 == 0) CalculateNextCell(x, y);
{
io.Write($" {x % 6} ");
}
else if (x % 6 == 0)
{
io.Write($" {y % 6} ");
}
else
{
CalculateAndDisplayCell(x, y);
}
} }
} }
return; return;
} }
void CalculateAndDisplayCell(int x, int y) void CalculateNextCell(int x, int y)
{ {
if (_board[x, y] >= 3) if (_board[x, y] >= 3)
{ {
@@ -47,11 +35,11 @@ void CalculateAndDisplayCell(int x, int y)
{ {
if (o < 9) if (o < 9)
{ {
_board[x, y] = 0x0100; _player1Count++; io.Write(" * "); _board[x, y] = 0x0100; _player1Count++;
} }
else else
{ {
_board[x, y] = 0x1000; _player2Count++; io.Write(" # "); _board[x, y] = 0x1000; _player2Count++;
} }
return; return;
} }
@@ -59,7 +47,6 @@ void CalculateAndDisplayCell(int x, int y)
} }
_board[x, y] = 0; _board[x, y] = 0;
io.Write(" ");
} }
for (var _player = 1; _player <= 2; _player++) for (var _player = 1; _player <= 2; _player++)
@@ -73,13 +60,15 @@ for (var _player = 1; _player <= 2; _player++)
} }
} }
CalculateAndDisplayNext(); CalculateNext();
_board.Display(io);
while (true) while (true)
{ {
io.WriteLine(); io.WriteLine();
_board.CalculateNeighbours(); _board.CalculateNeighbours();
CalculateAndDisplayNext(); CalculateNext();
_board.Display(io);
if (_player1Count == 0 || _player2Count == 0) { break; } if (_player1Count == 0 || _player2Count == 0) { break; }