Move player counts into Board

This commit is contained in:
Andrew Cooper
2022-09-08 08:11:08 +10:00
parent c1d43a742a
commit 5e998088f9
4 changed files with 25 additions and 17 deletions

View File

@@ -17,6 +17,8 @@ internal class Board
private readonly int[,] _cells = new int[7,7]; private readonly int[,] _cells = new int[7,7];
private readonly Dictionary<int, int> _cellCounts = new();
public int this[Coordinates coordinates] public int this[Coordinates coordinates]
{ {
get => _cells[coordinates.X, coordinates.Y]; get => _cells[coordinates.X, coordinates.Y];
@@ -29,9 +31,21 @@ internal class Board
set => _cells[x, y] = value; 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
{
(0, 0) => Strings.Draw,
(_, 0) => string.Format(Formats.Winner, 1),
(0, _) => string.Format(Formats.Winner, 2),
_ => null
};
public void CalculateNextGeneration()
{ {
var _cellCounts = new Dictionary<int, int>() { [Empty] = 0, [Player1] = 0, [Player2] = 0 }; _cellCounts[Empty] = _cellCounts[Player1] = _cellCounts[Player2] = 0;
for (var x = 1; x <= 5; x++) for (var x = 1; x <= 5; x++)
{ {
@@ -49,8 +63,6 @@ internal class Board
_cellCounts[newValue]++; _cellCounts[newValue]++;
} }
} }
return (_cellCounts[Player1], _cellCounts[Player2]);
} }
public void CalculateNeighbours() public void CalculateNeighbours()

View File

@@ -7,7 +7,6 @@ var io = new ConsoleIO();
io.Write(Streams.Title); io.Write(Streams.Title);
var _board = new Board(); var _board = new Board();
int _player1Count, _player2Count;
for (var _player = 1; _player <= 2; _player++) for (var _player = 1; _player <= 2; _player++)
{ {
@@ -26,10 +25,10 @@ while (true)
{ {
io.WriteLine(); io.WriteLine();
_board.CalculateNeighbours(); _board.CalculateNeighbours();
(_player1Count, _player2Count) = _board.CalculateNextGeneration(); _board.CalculateNextGeneration();
_board.Display(io); _board.Display(io);
if (_player1Count == 0 || _player2Count == 0) { break; } if (_board.Result is not null) { break; }
var player1Coordinate = io.ReadCoordinates(1, _board); var player1Coordinate = io.ReadCoordinates(1, _board);
var player2Coordinate = io.ReadCoordinates(2, _board); var player2Coordinate = io.ReadCoordinates(2, _board);
@@ -47,11 +46,4 @@ while (true)
} }
} }
if (_player1Count == 0 && _player2Count == 0) io.WriteLine(_board.Result);
{
io.Write(Streams.Draw);
}
else
{
io.WriteLine(Formats.Winner, _player2Count == 0 ? 1 : 2);
}

View File

@@ -1,2 +1,2 @@
A draw A draw

View File

@@ -8,7 +8,6 @@ internal static class Resource
internal static class Streams internal static class Streams
{ {
public static Stream Title => GetStream(); public static Stream Title => GetStream();
public static Stream Draw => GetStream();
public static Stream IllegalCoords => GetStream(); public static Stream IllegalCoords => GetStream();
public static Stream SameCoords => GetStream(); public static Stream SameCoords => GetStream();
} }
@@ -20,6 +19,11 @@ internal static class Resource
public static string Winner => GetString(); public static string Winner => GetString();
} }
internal static class Strings
{
public static string Draw => GetString();
}
private static string GetString([CallerMemberName] string? name = null) private static string GetString([CallerMemberName] string? name = null)
{ {
using var stream = GetStream(name); using var stream = GetStream(name);