diff --git a/56_Life_for_Two/csharp/Board.cs b/56_Life_for_Two/csharp/Board.cs index 7886d4c2..f3cb0329 100644 --- a/56_Life_for_Two/csharp/Board.cs +++ b/56_Life_for_Two/csharp/Board.cs @@ -1,8 +1,9 @@ +using System.Collections; using System.Text; namespace LifeforTwo; -internal class Board +internal class Board : IEnumerable { private readonly Piece[,] _cells = new Piece[7, 7]; private readonly Dictionary _cellCounts = @@ -14,7 +15,7 @@ internal class Board set => this[coordinates.X, coordinates.Y] = value; } - public Piece this[int x, int y] + private Piece this[int x, int y] { get => _cells[x, y]; set @@ -57,4 +58,17 @@ internal class Board (_, 0 or 6) => $" {x % 6} ", _ => $" {this[x, y]} " }; + + public IEnumerator GetEnumerator() + { + for (var x = 1; x <= 5; x++) + { + for (var y = 1; y <= 5; y++) + { + yield return new(x, y); + } + } + } + + IEnumerator IEnumerable.GetEnumerator() => GetEnumerator(); } diff --git a/56_Life_for_Two/csharp/Generation.cs b/56_Life_for_Two/csharp/Generation.cs index 26f8189e..b96ee478 100644 --- a/56_Life_for_Two/csharp/Generation.cs +++ b/56_Life_for_Two/csharp/Generation.cs @@ -45,12 +45,9 @@ internal class Generation { var board = new Board(); - for (var x = 1; x <= 5; x++) + foreach (var coordinates in _board) { - for (var y = 1; y <= 5; y++) - { - board[x, y] = _board[x, y].GetNext(); - } + board[coordinates] = _board[coordinates].GetNext(); } return new(board); @@ -76,18 +73,14 @@ internal class Generation private void CountNeighbours() { - for (var x = 1; x <= 5; x++) + foreach (var coordinates in _board) { - for (var y = 1; y <= 5; y++) - { - var coordinates = new Coordinates(x, y); - var piece = _board[coordinates]; - if (piece.IsEmpty) { continue; } + var piece = _board[coordinates]; + if (piece.IsEmpty) { continue; } - foreach (var neighbour in coordinates.GetNeighbors()) - { - _board[neighbour] = _board[neighbour].AddNeighbour(piece); - } + foreach (var neighbour in coordinates.GetNeighbors()) + { + _board[neighbour] = _board[neighbour].AddNeighbour(piece); } } }