Files
basic-computer-games/56_Life_for_Two/csharp/Generation.cs
2022-09-13 07:21:53 +10:00

78 lines
1.9 KiB
C#

internal class Generation
{
private readonly Board _board;
public Generation(Board board)
{
_board = board;
CountNeighbours();
}
public Board Board => _board;
public int Player1Count => _board.Player1Count;
public int Player2Count => _board.Player2Count;
public string? Result =>
(Player1Count, Player2Count) switch
{
(0, 0) => Strings.Draw,
(_, 0) => string.Format(Formats.Winner, 1),
(0, _) => string.Format(Formats.Winner, 2),
_ => null
};
public static Generation Create(IReadWrite io)
{
var board = new Board();
SetInitialPieces(1, coord => board.AddPlayer1Piece(coord));
SetInitialPieces(2, coord => board.AddPlayer2Piece(coord));
return new Generation(board);
void SetInitialPieces(int player, Action<Coordinates> setPiece)
{
io.WriteLine(Formats.InitialPieces, player);
for (var i = 1; i <= 3; i++)
{
setPiece(io.ReadCoordinates(board));
}
}
}
public Generation CalculateNextGeneration()
{
var board = new Board();
for (var x = 1; x <= 5; x++)
{
for (var y = 1; y <= 5; y++)
{
board[x, y] = _board[x, y].GetNext();
}
}
return new(board);
}
private void CountNeighbours()
{
for (var x = 1; x <= 5; x++)
{
for (var y = 1; y <= 5; y++)
{
var coordinates = new Coordinates(x, y);
var piece = _board[coordinates];
if (piece.IsEmpty) { continue; }
foreach (var neighbour in coordinates.GetNeighbors())
{
_board[neighbour] = _board[neighbour].AddNeighbour(piece);
}
}
}
}
public override string ToString() => _board.ToString();
}