From 28736b9dbeb72559d3b95663c9e034beecdb92e0 Mon Sep 17 00:00:00 2001 From: Andrew Cooper Date: Fri, 2 Sep 2022 08:40:46 +1000 Subject: [PATCH] Make cell values binary --- 56_Life_for_Two/csharp/Board.cs | 12 +++++------- 56_Life_for_Two/csharp/Program.cs | 13 +++++++------ 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/56_Life_for_Two/csharp/Board.cs b/56_Life_for_Two/csharp/Board.cs index 1469749b..dbe7ece9 100644 --- a/56_Life_for_Two/csharp/Board.cs +++ b/56_Life_for_Two/csharp/Board.cs @@ -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; } } } diff --git a/56_Life_for_Two/csharp/Program.cs b/56_Life_for_Two/csharp/Program.cs index 73d3aa85..e63db901 100644 --- a/56_Life_for_Two/csharp/Program.cs +++ b/56_Life_for_Two/csharp/Program.cs @@ -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; } } }