mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-23 07:29:02 -08:00
Move neighbour count to generation calculation
This commit is contained in:
@@ -34,6 +34,8 @@ internal class Board
|
|||||||
public int Player1Count => _cellCounts[Player1];
|
public int Player1Count => _cellCounts[Player1];
|
||||||
public int Player2Count => _cellCounts[Player2];
|
public int Player2Count => _cellCounts[Player2];
|
||||||
|
|
||||||
|
internal bool IsEmptyAt(Coordinates coordinates) => (this[coordinates] & PieceMask) == Empty;
|
||||||
|
|
||||||
public string? Result =>
|
public string? Result =>
|
||||||
(Player1Count, Player2Count) switch
|
(Player1Count, Player2Count) switch
|
||||||
{
|
{
|
||||||
@@ -43,6 +45,12 @@ internal class Board
|
|||||||
_ => null
|
_ => null
|
||||||
};
|
};
|
||||||
|
|
||||||
|
internal void ClearCell(Coordinates coordinates) => this[coordinates] = Empty;
|
||||||
|
|
||||||
|
internal void AddPlayer1Piece(Coordinates coordinates) => this[coordinates] = Player1;
|
||||||
|
|
||||||
|
internal void AddPlayer2Piece(Coordinates coordinates) => this[coordinates] = Player2;
|
||||||
|
|
||||||
public void CalculateNextGeneration()
|
public void CalculateNextGeneration()
|
||||||
{
|
{
|
||||||
_cellCounts[Empty] = _cellCounts[Player1] = _cellCounts[Player2] = 0;
|
_cellCounts[Empty] = _cellCounts[Player1] = _cellCounts[Player2] = 0;
|
||||||
@@ -63,9 +71,11 @@ internal class Board
|
|||||||
_cellCounts[newValue]++;
|
_cellCounts[newValue]++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
CountNeighbours();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void CalculateNeighbours()
|
private void CountNeighbours()
|
||||||
{
|
{
|
||||||
for (var x = 1; x <= 5; x++)
|
for (var x = 1; x <= 5; x++)
|
||||||
{
|
{
|
||||||
@@ -97,7 +107,7 @@ internal class Board
|
|||||||
}
|
}
|
||||||
|
|
||||||
private string GetDisplay(int x, int y) =>
|
private string GetDisplay(int x, int y) =>
|
||||||
(x, y, this[x, y]) switch
|
(x, y, this[x, y] & PieceMask) switch
|
||||||
{
|
{
|
||||||
(0 or 6, _, _) => $" {y % 6} ",
|
(0 or 6, _, _) => $" {y % 6} ",
|
||||||
(_, 0 or 6, _) => $" {x % 6} ",
|
(_, 0 or 6, _) => $" {x % 6} ",
|
||||||
|
|||||||
@@ -1,18 +1,18 @@
|
|||||||
internal class Game
|
internal class Game
|
||||||
{
|
{
|
||||||
private readonly IReadWrite _io;
|
private readonly IReadWrite _io;
|
||||||
|
private readonly Board _board;
|
||||||
|
|
||||||
public Game(IReadWrite io)
|
public Game(IReadWrite io)
|
||||||
{
|
{
|
||||||
_io = io;
|
_io = io;
|
||||||
|
_board = new Board();
|
||||||
}
|
}
|
||||||
|
|
||||||
public void Play()
|
public void Play()
|
||||||
{
|
{
|
||||||
_io.Write(Streams.Title);
|
_io.Write(Streams.Title);
|
||||||
|
|
||||||
var _board = new Board();
|
|
||||||
|
|
||||||
for (var _player = 1; _player <= 2; _player++)
|
for (var _player = 1; _player <= 2; _player++)
|
||||||
{
|
{
|
||||||
var P1 = _player == 2 ? 0x30 : 0x03;
|
var P1 = _player == 2 ? 0x30 : 0x03;
|
||||||
@@ -28,9 +28,8 @@ internal class Game
|
|||||||
|
|
||||||
while(true)
|
while(true)
|
||||||
{
|
{
|
||||||
_io.WriteLine();
|
|
||||||
_board.CalculateNeighbours();
|
|
||||||
_board.CalculateNextGeneration();
|
_board.CalculateNextGeneration();
|
||||||
|
_io.WriteLine();
|
||||||
_board.Display(_io);
|
_board.Display(_io);
|
||||||
|
|
||||||
if (_board.Result is not null) { break; }
|
if (_board.Result is not null) { break; }
|
||||||
@@ -42,12 +41,12 @@ internal class Game
|
|||||||
{
|
{
|
||||||
_io.Write(Streams.SameCoords);
|
_io.Write(Streams.SameCoords);
|
||||||
// This is a bug existing in the original code. The line should be _board[_coordinates[_player]] = 0;
|
// This is a bug existing in the original code. The line should be _board[_coordinates[_player]] = 0;
|
||||||
_board[player1Coordinate + 1] = 0;
|
_board.ClearCell(player1Coordinate + 1);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
_board[player1Coordinate] = 0x0100;
|
_board.AddPlayer1Piece(player1Coordinate);
|
||||||
_board[player2Coordinate] = 0x1000;
|
_board.AddPlayer2Piece(player2Coordinate);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -12,7 +12,7 @@ internal static class IOExtensions
|
|||||||
{
|
{
|
||||||
io.WriteLine("X,Y");
|
io.WriteLine("X,Y");
|
||||||
var values = io.Read2Numbers("&&&&&&\r");
|
var values = io.Read2Numbers("&&&&&&\r");
|
||||||
if (Coordinates.TryCreate(values, out var coordinates) && board[coordinates] == 0)
|
if (Coordinates.TryCreate(values, out var coordinates) && board.IsEmptyAt(coordinates))
|
||||||
{
|
{
|
||||||
return coordinates;
|
return coordinates;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user