diff --git a/56_Life_for_Two/csharp/Board.cs b/56_Life_for_Two/csharp/Board.cs index 4618f881..1469749b 100644 --- a/56_Life_for_Two/csharp/Board.cs +++ b/56_Life_for_Two/csharp/Board.cs @@ -2,6 +2,10 @@ 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 readonly int[,] _cells = new int[7,7]; public int this[Coordinates coordinates] @@ -15,4 +19,23 @@ internal class Board get => _cells[x, y]; set => _cells[x, y] = value; } + + public void CalculateNeighbours() + { + for (var x = 1; x <= 5; x++) + { + for (var y = 1; y <= 5; y++) + { + var coordinates = new Coordinates(x, y); + if (this[coordinates] >= Player1Piece) + { + int _playerPiece = this[coordinates] > Player2Piece ? Player2Neighbour : Player1Neighbour; + foreach (var neighbour in coordinates.GetNeighbors()) + { + this[neighbour] += _playerPiece; + } + } + } + } + } } \ No newline at end of file diff --git a/56_Life_for_Two/csharp/Program.cs b/56_Life_for_Two/csharp/Program.cs index 6208be04..73d3aa85 100644 --- a/56_Life_for_Two/csharp/Program.cs +++ b/56_Life_for_Two/csharp/Program.cs @@ -11,25 +11,6 @@ var _willLive = new[] { 3, 102, 103, 120, 130, 121, 112, 111, 12, 21, 30, 1020, var _coordinates = new Coordinates[3]; int _player1Count, _player2Count; -void CalculateNeighbors() -{ - for (var x = 1; x <= 5; x++) - { - for (var y = 1; y <= 5; y++) - { - var coordinates = new Coordinates(x, y); - if (_board[coordinates] > 99) - { - int B = _board[coordinates] > 999 ? 10 : 1; - foreach (var neighbor in coordinates.GetNeighbors()) - { - _board[neighbor] += B; - } - } - } - } -} - void CalculateAndDisplayNext() { _player1Count = _player2Count = 0; @@ -96,7 +77,7 @@ CalculateAndDisplayNext(); while (true) { io.WriteLine(); - CalculateNeighbors(); + _board.CalculateNeighbours(); CalculateAndDisplayNext(); if (_player1Count == 0 || _player2Count == 0) { break; }