Add coordinates enumerator

This commit is contained in:
Andrew Cooper
2022-09-13 08:17:26 +10:00
parent a9ec4e3eb1
commit 307c5e8ee7
2 changed files with 24 additions and 17 deletions

View File

@@ -1,8 +1,9 @@
using System.Collections;
using System.Text;
namespace LifeforTwo;
internal class Board
internal class Board : IEnumerable<Coordinates>
{
private readonly Piece[,] _cells = new Piece[7, 7];
private readonly Dictionary<int, int> _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<Coordinates> GetEnumerator()
{
for (var x = 1; x <= 5; x++)
{
for (var y = 1; y <= 5; y++)
{
yield return new(x, y);
}
}
}
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}

View File

@@ -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);
}
}
}