mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-22 23:26:40 -08:00
Move player counts into Board
This commit is contained in:
@@ -17,6 +17,8 @@ internal class Board
|
||||
|
||||
private readonly int[,] _cells = new int[7,7];
|
||||
|
||||
private readonly Dictionary<int, int> _cellCounts = new();
|
||||
|
||||
public int this[Coordinates coordinates]
|
||||
{
|
||||
get => _cells[coordinates.X, coordinates.Y];
|
||||
@@ -29,9 +31,21 @@ internal class Board
|
||||
set => _cells[x, y] = value;
|
||||
}
|
||||
|
||||
public (int Player1Count, int Player2Count) CalculateNextGeneration()
|
||||
public int Player1Count => _cellCounts[Player1];
|
||||
public int Player2Count => _cellCounts[Player2];
|
||||
|
||||
public string? Result =>
|
||||
(Player1Count, Player2Count) switch
|
||||
{
|
||||
var _cellCounts = new Dictionary<int, int>() { [Empty] = 0, [Player1] = 0, [Player2] = 0 };
|
||||
(0, 0) => Strings.Draw,
|
||||
(_, 0) => string.Format(Formats.Winner, 1),
|
||||
(0, _) => string.Format(Formats.Winner, 2),
|
||||
_ => null
|
||||
};
|
||||
|
||||
public void CalculateNextGeneration()
|
||||
{
|
||||
_cellCounts[Empty] = _cellCounts[Player1] = _cellCounts[Player2] = 0;
|
||||
|
||||
for (var x = 1; x <= 5; x++)
|
||||
{
|
||||
@@ -49,8 +63,6 @@ internal class Board
|
||||
_cellCounts[newValue]++;
|
||||
}
|
||||
}
|
||||
|
||||
return (_cellCounts[Player1], _cellCounts[Player2]);
|
||||
}
|
||||
|
||||
public void CalculateNeighbours()
|
||||
|
||||
@@ -7,7 +7,6 @@ var io = new ConsoleIO();
|
||||
io.Write(Streams.Title);
|
||||
|
||||
var _board = new Board();
|
||||
int _player1Count, _player2Count;
|
||||
|
||||
for (var _player = 1; _player <= 2; _player++)
|
||||
{
|
||||
@@ -26,10 +25,10 @@ while (true)
|
||||
{
|
||||
io.WriteLine();
|
||||
_board.CalculateNeighbours();
|
||||
(_player1Count, _player2Count) = _board.CalculateNextGeneration();
|
||||
_board.CalculateNextGeneration();
|
||||
_board.Display(io);
|
||||
|
||||
if (_player1Count == 0 || _player2Count == 0) { break; }
|
||||
if (_board.Result is not null) { break; }
|
||||
|
||||
var player1Coordinate = io.ReadCoordinates(1, _board);
|
||||
var player2Coordinate = io.ReadCoordinates(2, _board);
|
||||
@@ -47,11 +46,4 @@ while (true)
|
||||
}
|
||||
}
|
||||
|
||||
if (_player1Count == 0 && _player2Count == 0)
|
||||
{
|
||||
io.Write(Streams.Draw);
|
||||
}
|
||||
else
|
||||
{
|
||||
io.WriteLine(Formats.Winner, _player2Count == 0 ? 1 : 2);
|
||||
}
|
||||
io.WriteLine(_board.Result);
|
||||
@@ -8,7 +8,6 @@ internal static class Resource
|
||||
internal static class Streams
|
||||
{
|
||||
public static Stream Title => GetStream();
|
||||
public static Stream Draw => GetStream();
|
||||
public static Stream IllegalCoords => GetStream();
|
||||
public static Stream SameCoords => GetStream();
|
||||
}
|
||||
@@ -20,6 +19,11 @@ internal static class Resource
|
||||
public static string Winner => GetString();
|
||||
}
|
||||
|
||||
internal static class Strings
|
||||
{
|
||||
public static string Draw => GetString();
|
||||
}
|
||||
|
||||
private static string GetString([CallerMemberName] string? name = null)
|
||||
{
|
||||
using var stream = GetStream(name);
|
||||
|
||||
Reference in New Issue
Block a user