From fae67d6a2a6e0a17619b2413faa7b531a4a8fec8 Mon Sep 17 00:00:00 2001 From: Andrew Cooper Date: Sun, 17 Jul 2022 18:34:48 +1000 Subject: [PATCH] Add PlayerNumber --- 26_Chomp/csharp/Cookie.cs | 53 +++++++++++++++++++++++++++ 26_Chomp/csharp/Game.cs | 9 ++++- 26_Chomp/csharp/PlayerNumber.cs | 32 ++++++++++++++++ 26_Chomp/csharp/Resources/NoFair.txt | 1 + 26_Chomp/csharp/Resources/Player.txt | 3 +- 26_Chomp/csharp/Resources/Resource.cs | 3 +- 26_Chomp/csharp/Resources/YouLose.txt | 2 +- 7 files changed, 97 insertions(+), 6 deletions(-) create mode 100644 26_Chomp/csharp/Cookie.cs create mode 100644 26_Chomp/csharp/PlayerNumber.cs create mode 100644 26_Chomp/csharp/Resources/NoFair.txt diff --git a/26_Chomp/csharp/Cookie.cs b/26_Chomp/csharp/Cookie.cs new file mode 100644 index 00000000..3a32c8c3 --- /dev/null +++ b/26_Chomp/csharp/Cookie.cs @@ -0,0 +1,53 @@ +using System.Text; + +namespace Chomp; + +internal class Cookie +{ + private readonly int _rowCount; + private readonly int _columnCount; + private readonly char[][] _bits; + + public Cookie(int rowCount, int columnCount) + { + _rowCount = rowCount; + _columnCount = columnCount; + + // The calls to Math.Max here are to duplicate the original behaviour + // when negative values are given for the row or column count. + _bits = new char[Math.Max(_rowCount, 1)][]; + for (int row = 0; row < _bits.Length; row++) + { + _bits[row] = Enumerable.Repeat('*', Math.Max(_columnCount, 1)).ToArray(); + } + _bits[0][0] = 'P'; + } + + public bool TryChomp(int row, int column) + { + if (row < 1 || row > _rowCount || column < 1 || column > _columnCount || _bits[row - 1][column - 1] == ' ') + { + return false; + } + + for (int r = row; r <= _rowCount; r++) + { + for (int c = column; c <= _columnCount; c++) + { + _bits[r - 1][c - 1] = ' '; + } + } + + return true; + } + + public override string ToString() + { + var builder = new StringBuilder().AppendLine(" 1 2 3 4 5 6 7 8 9"); + for (int row = 1; row <= _bits.Length; row++) + { + builder.Append(' ').Append(row).Append(" ").AppendLine(string.Join(' ', _bits[row - 1])); + } + return builder.ToString(); + } +} \ No newline at end of file diff --git a/26_Chomp/csharp/Game.cs b/26_Chomp/csharp/Game.cs index 96ea19b4..a9219823 100644 --- a/26_Chomp/csharp/Game.cs +++ b/26_Chomp/csharp/Game.cs @@ -17,13 +17,20 @@ internal class Game _io.Write(Resource.Streams.Rules); } - while (true) { _io.Write(Resource.Streams.HereWeGo); var (playerCount, rowCount, columnCount) = _io.ReadParameters(); + var cookie = new Cookie(rowCount, columnCount); + var player = new PlayerNumber(playerCount); + + _io.WriteLine(cookie); + + _io.WriteLine(string.Format(Resource.Formats.Player, player)); + var (row, column) = _io.Read2Numbers(Resource.Prompts.Coordinates); + if (_io.ReadNumber("Again (1=Yes, 0=No!)") != 1) { break; } } } diff --git a/26_Chomp/csharp/PlayerNumber.cs b/26_Chomp/csharp/PlayerNumber.cs new file mode 100644 index 00000000..16138a9b --- /dev/null +++ b/26_Chomp/csharp/PlayerNumber.cs @@ -0,0 +1,32 @@ +namespace Chomp; + +internal class PlayerNumber +{ + private readonly float _playerCount; + private int _counter; + private float _number; + + // The original code does not constrain playerCount to be an integer + public PlayerNumber(float playerCount) + { + _playerCount = playerCount; + _number = 0; + Increment(); + } + + public static PlayerNumber operator ++(PlayerNumber number) => number.Increment(); + + private PlayerNumber Increment() + { + if (_playerCount == 0) { throw new DivideByZeroException(); } + + // The increment logic here is the same as the original program, and exhibits + // interesting behaviour when _playerCount is not an integer. + _counter++; + _number = _counter - (float)Math.Floor(_counter / _playerCount) * _playerCount; + if (_number == 0) { _number = _playerCount; } + return this; + } + + public override string ToString() => (_number >= 0 ? " " : "") + _number.ToString(); +} \ No newline at end of file diff --git a/26_Chomp/csharp/Resources/NoFair.txt b/26_Chomp/csharp/Resources/NoFair.txt new file mode 100644 index 00000000..9fe310eb --- /dev/null +++ b/26_Chomp/csharp/Resources/NoFair.txt @@ -0,0 +1 @@ +No fair. You're trying to chomp on empty space! diff --git a/26_Chomp/csharp/Resources/Player.txt b/26_Chomp/csharp/Resources/Player.txt index 60f9ba5e..f743df3a 100644 --- a/26_Chomp/csharp/Resources/Player.txt +++ b/26_Chomp/csharp/Resources/Player.txt @@ -1,2 +1 @@ -Player {0} -Player {0} \ No newline at end of file +Player{0} \ No newline at end of file diff --git a/26_Chomp/csharp/Resources/Resource.cs b/26_Chomp/csharp/Resources/Resource.cs index efa933dc..d080b1fb 100644 --- a/26_Chomp/csharp/Resources/Resource.cs +++ b/26_Chomp/csharp/Resources/Resource.cs @@ -10,8 +10,7 @@ internal static class Resource public static Stream HereWeGo => GetStream(); public static Stream Introduction => GetStream(); public static Stream Rules => GetStream(); - public static Stream TooManyColumns => GetStream(); - public static Stream TooManyRows => GetStream(); + public static Stream NoFair => GetStream(); } internal static class Formats diff --git a/26_Chomp/csharp/Resources/YouLose.txt b/26_Chomp/csharp/Resources/YouLose.txt index c6e22300..126d8f1e 100644 --- a/26_Chomp/csharp/Resources/YouLose.txt +++ b/26_Chomp/csharp/Resources/YouLose.txt @@ -1 +1 @@ -You lose, player {0} \ No newline at end of file +You lose, player{0} \ No newline at end of file