Add PlayerNumber

This commit is contained in:
Andrew Cooper
2022-07-17 18:34:48 +10:00
parent bbbf5560f4
commit fae67d6a2a
7 changed files with 97 additions and 6 deletions

53
26_Chomp/csharp/Cookie.cs Normal file
View File

@@ -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();
}
}

View File

@@ -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; }
}
}

View File

@@ -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();
}

View File

@@ -0,0 +1 @@
No fair. You're trying to chomp on empty space!

View File

@@ -1,2 +1 @@
Player {0}
Player {0}
Player{0}

View File

@@ -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

View File

@@ -1 +1 @@
You lose, player {0}
You lose, player{0}