mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-22 15:16:33 -08:00
Add PlayerNumber
This commit is contained in:
53
26_Chomp/csharp/Cookie.cs
Normal file
53
26_Chomp/csharp/Cookie.cs
Normal 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();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user