diff --git a/56_Life_for_Two/csharp/Board.cs b/56_Life_for_Two/csharp/Board.cs index 7c5d53c5..f17a6f48 100644 --- a/56_Life_for_Two/csharp/Board.cs +++ b/56_Life_for_Two/csharp/Board.cs @@ -1,3 +1,5 @@ +using System.Collections.Immutable; + namespace LifeforTwo; internal class Board @@ -7,6 +9,12 @@ internal class Board private const int Player2 = 0x1000; private const int PieceMask = Player1 | Player2; private const int NeighbourValueOffset = 8; + + private readonly ImmutableHashSet _willBePlayer1 = + new[] { 0x0003, 0x0102, 0x0103, 0x0120, 0x0130, 0x0121, 0x0112, 0x0111, 0x0012 }.ToImmutableHashSet(); + private readonly ImmutableHashSet _willBePlayer2 = + new[] { 0x0021, 0x0030, 0x1020, 0x1030, 0x1011, 0x1021, 0x1003, 0x1002, 0x1012 }.ToImmutableHashSet(); + private readonly int[,] _cells = new int[7,7]; public int this[Coordinates coordinates] @@ -21,6 +29,30 @@ internal class Board set => _cells[x, y] = value; } + public (int Player1Count, int Player2Count) CalculateNextGeneration() + { + var _cellCounts = new Dictionary() { [Empty] = 0, [Player1] = 0, [Player2] = 0 }; + + for (var x = 1; x <= 5; x++) + { + for (var y = 1; y <= 5; y++) + { + var currentValue = this[x, y]; + var newValue = currentValue switch + { + _ when _willBePlayer1.Contains(currentValue) => Player1, + _ when _willBePlayer2.Contains(currentValue) => Player2, + _ => Empty + }; + + this[x, y] = newValue; + _cellCounts[newValue]++; + } + } + + return (_cellCounts[Player1], _cellCounts[Player2]); + } + public void CalculateNeighbours() { for (var x = 1; x <= 5; x++) diff --git a/56_Life_for_Two/csharp/Program.cs b/56_Life_for_Two/csharp/Program.cs index 1754cdc5..5f2ed7dc 100644 --- a/56_Life_for_Two/csharp/Program.cs +++ b/56_Life_for_Two/csharp/Program.cs @@ -7,48 +7,9 @@ var io = new ConsoleIO(); io.Write(Streams.Title); var _board = new Board(); -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; -void CalculateNext() -{ - _player1Count = _player2Count = 0; - for (var y = 1; y <= 5; y++) - { - for (var x = 1; x <= 5; x++) - { - CalculateNextCell(x, y); - } - } - return; -} - -void CalculateNextCell(int x, int y) -{ - if (_board[x, y] >= 3) - { - for (var o = 0; o < 18; o++) - { - if (_board[x, y] == _willLive[o]) - { - if (o < 9) - { - _board[x, y] = 0x0100; _player1Count++; - } - else - { - _board[x, y] = 0x1000; _player2Count++; - } - return; - } - } - } - - _board[x, y] = 0; -} - for (var _player = 1; _player <= 2; _player++) { var P1 = _player == 2 ? 0x30 : 0x03; @@ -60,14 +21,14 @@ for (var _player = 1; _player <= 2; _player++) } } -CalculateNext(); +_board.CalculateNextGeneration(); _board.Display(io); while (true) { io.WriteLine(); _board.CalculateNeighbours(); - CalculateNext(); + (_player1Count, _player2Count) = _board.CalculateNextGeneration(); _board.Display(io); if (_player1Count == 0 || _player2Count == 0) { break; }