diff --git a/56_Life_for_Two/csharp/Board.cs b/56_Life_for_Two/csharp/Board.cs index 48cf8bdc..1da68626 100644 --- a/56_Life_for_Two/csharp/Board.cs +++ b/56_Life_for_Two/csharp/Board.cs @@ -34,6 +34,8 @@ internal class Board public int Player1Count => _cellCounts[Player1]; public int Player2Count => _cellCounts[Player2]; + internal bool IsEmptyAt(Coordinates coordinates) => (this[coordinates] & PieceMask) == Empty; + public string? Result => (Player1Count, Player2Count) switch { @@ -43,6 +45,12 @@ internal class Board _ => null }; + internal void ClearCell(Coordinates coordinates) => this[coordinates] = Empty; + + internal void AddPlayer1Piece(Coordinates coordinates) => this[coordinates] = Player1; + + internal void AddPlayer2Piece(Coordinates coordinates) => this[coordinates] = Player2; + public void CalculateNextGeneration() { _cellCounts[Empty] = _cellCounts[Player1] = _cellCounts[Player2] = 0; @@ -63,9 +71,11 @@ internal class Board _cellCounts[newValue]++; } } + + CountNeighbours(); } - public void CalculateNeighbours() + private void CountNeighbours() { for (var x = 1; x <= 5; x++) { @@ -97,7 +107,7 @@ internal class Board } private string GetDisplay(int x, int y) => - (x, y, this[x, y]) switch + (x, y, this[x, y] & PieceMask) switch { (0 or 6, _, _) => $" {y % 6} ", (_, 0 or 6, _) => $" {x % 6} ", diff --git a/56_Life_for_Two/csharp/Game.cs b/56_Life_for_Two/csharp/Game.cs index 0b2704bf..e8fb05aa 100644 --- a/56_Life_for_Two/csharp/Game.cs +++ b/56_Life_for_Two/csharp/Game.cs @@ -1,18 +1,18 @@ internal class Game { private readonly IReadWrite _io; + private readonly Board _board; public Game(IReadWrite io) { _io = io; + _board = new Board(); } public void Play() { _io.Write(Streams.Title); - var _board = new Board(); - for (var _player = 1; _player <= 2; _player++) { var P1 = _player == 2 ? 0x30 : 0x03; @@ -26,11 +26,10 @@ internal class Game _board.CalculateNextGeneration(); _board.Display(_io); - while (true) + while(true) { - _io.WriteLine(); - _board.CalculateNeighbours(); _board.CalculateNextGeneration(); + _io.WriteLine(); _board.Display(_io); if (_board.Result is not null) { break; } @@ -42,15 +41,15 @@ internal class Game { _io.Write(Streams.SameCoords); // This is a bug existing in the original code. The line should be _board[_coordinates[_player]] = 0; - _board[player1Coordinate + 1] = 0; + _board.ClearCell(player1Coordinate + 1); } else { - _board[player1Coordinate] = 0x0100; - _board[player2Coordinate] = 0x1000; + _board.AddPlayer1Piece(player1Coordinate); + _board.AddPlayer2Piece(player2Coordinate); } } _io.WriteLine(_board.Result); } -} \ No newline at end of file +} diff --git a/56_Life_for_Two/csharp/IOExtensions.cs b/56_Life_for_Two/csharp/IOExtensions.cs index 7d8bfc62..d7db9bf4 100644 --- a/56_Life_for_Two/csharp/IOExtensions.cs +++ b/56_Life_for_Two/csharp/IOExtensions.cs @@ -12,7 +12,7 @@ internal static class IOExtensions { io.WriteLine("X,Y"); var values = io.Read2Numbers("&&&&&&\r"); - if (Coordinates.TryCreate(values, out var coordinates) && board[coordinates] == 0) + if (Coordinates.TryCreate(values, out var coordinates) && board.IsEmptyAt(coordinates)) { return coordinates; }