Make cell values binary

This commit is contained in:
Andrew Cooper
2022-09-02 08:40:46 +10:00
parent b156755ee0
commit 28736b9dbe
2 changed files with 12 additions and 13 deletions

View File

@@ -2,10 +2,8 @@ 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 const int PieceMask = 0x1100;
private const int NeighbourValueOffset = 8;
private readonly int[,] _cells = new int[7,7];
public int this[Coordinates coordinates]
@@ -27,12 +25,12 @@ internal class Board
for (var y = 1; y <= 5; y++)
{
var coordinates = new Coordinates(x, y);
if (this[coordinates] >= Player1Piece)
var neighbourValue = (this[coordinates] & PieceMask) >> NeighbourValueOffset;
if (neighbourValue > 0)
{
int _playerPiece = this[coordinates] > Player2Piece ? Player2Neighbour : Player1Neighbour;
foreach (var neighbour in coordinates.GetNeighbors())
{
this[neighbour] += _playerPiece;
this[neighbour] += neighbourValue;
}
}
}

View File

@@ -7,7 +7,8 @@ var io = new ConsoleIO();
io.Write(Streams.Title);
var _board = new Board();
var _willLive = new[] { 3, 102, 103, 120, 130, 121, 112, 111, 12, 21, 30, 1020, 1030, 1011, 1021, 1003, 1002, 1012 };
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];
int _player1Count, _player2Count;
@@ -46,11 +47,11 @@ void CalculateAndDisplayCell(int x, int y)
{
if (o < 9)
{
_board[x, y] = 100; _player1Count++; io.Write(" * ");
_board[x, y] = 0x0100; _player1Count++; io.Write(" * ");
}
else
{
_board[x, y] = 1000; _player2Count++; io.Write(" # ");
_board[x, y] = 0x1000; _player2Count++; io.Write(" # ");
}
return;
}
@@ -63,7 +64,7 @@ void CalculateAndDisplayCell(int x, int y)
for (var _player = 1; _player <= 2; _player++)
{
var P1 = _player == 2 ? 30 : 3;
var P1 = _player == 2 ? 0x30 : 0x03;
io.WriteLine(Formats.InitialPieces, _player);
for (var i = 1; i <= 3; i++)
{
@@ -87,8 +88,8 @@ while (true)
io.WriteLine(Formats.Player, _player);
if (ReadCoordinates(_player))
{
_board[_coordinates[1]] = 100;
_board[_coordinates[2]] = 1000;
_board[_coordinates[1]] = 0x0100;
_board[_coordinates[2]] = 0x1000;
}
}
}