From 5e998088f91279fa7ca52da2b9b99428b9efdfd5 Mon Sep 17 00:00:00 2001 From: Andrew Cooper Date: Thu, 8 Sep 2022 08:11:08 +1000 Subject: [PATCH] Move player counts into Board --- 56_Life_for_Two/csharp/Board.cs | 20 ++++++++++++++++---- 56_Life_for_Two/csharp/Program.cs | 14 +++----------- 56_Life_for_Two/csharp/Resources/Draw.txt | 2 +- 56_Life_for_Two/csharp/Resources/Resource.cs | 6 +++++- 4 files changed, 25 insertions(+), 17 deletions(-) diff --git a/56_Life_for_Two/csharp/Board.cs b/56_Life_for_Two/csharp/Board.cs index f17a6f48..48cf8bdc 100644 --- a/56_Life_for_Two/csharp/Board.cs +++ b/56_Life_for_Two/csharp/Board.cs @@ -17,6 +17,8 @@ internal class Board private readonly int[,] _cells = new int[7,7]; + private readonly Dictionary _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 + { + (0, 0) => Strings.Draw, + (_, 0) => string.Format(Formats.Winner, 1), + (0, _) => string.Format(Formats.Winner, 2), + _ => null + }; + + public void CalculateNextGeneration() { - var _cellCounts = new Dictionary() { [Empty] = 0, [Player1] = 0, [Player2] = 0 }; + _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() diff --git a/56_Life_for_Two/csharp/Program.cs b/56_Life_for_Two/csharp/Program.cs index 51b4f909..bc2a3440 100644 --- a/56_Life_for_Two/csharp/Program.cs +++ b/56_Life_for_Two/csharp/Program.cs @@ -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); \ No newline at end of file diff --git a/56_Life_for_Two/csharp/Resources/Draw.txt b/56_Life_for_Two/csharp/Resources/Draw.txt index a4549a74..9b9fd8fc 100644 --- a/56_Life_for_Two/csharp/Resources/Draw.txt +++ b/56_Life_for_Two/csharp/Resources/Draw.txt @@ -1,2 +1,2 @@ -A draw +A draw \ No newline at end of file diff --git a/56_Life_for_Two/csharp/Resources/Resource.cs b/56_Life_for_Two/csharp/Resources/Resource.cs index 51e64c69..c6f59d3b 100644 --- a/56_Life_for_Two/csharp/Resources/Resource.cs +++ b/56_Life_for_Two/csharp/Resources/Resource.cs @@ -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);