mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2026-01-15 22:42:38 -08:00
Add coordinates enumerator
This commit is contained in:
@@ -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();
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user