mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-23 07:29:02 -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();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -17,13 +17,20 @@ internal class Game
|
|||||||
_io.Write(Resource.Streams.Rules);
|
_io.Write(Resource.Streams.Rules);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
while (true)
|
while (true)
|
||||||
{
|
{
|
||||||
_io.Write(Resource.Streams.HereWeGo);
|
_io.Write(Resource.Streams.HereWeGo);
|
||||||
|
|
||||||
var (playerCount, rowCount, columnCount) = _io.ReadParameters();
|
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; }
|
if (_io.ReadNumber("Again (1=Yes, 0=No!)") != 1) { break; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
32
26_Chomp/csharp/PlayerNumber.cs
Normal file
32
26_Chomp/csharp/PlayerNumber.cs
Normal 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();
|
||||||
|
}
|
||||||
1
26_Chomp/csharp/Resources/NoFair.txt
Normal file
1
26_Chomp/csharp/Resources/NoFair.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
No fair. You're trying to chomp on empty space!
|
||||||
@@ -1,2 +1 @@
|
|||||||
Player {0}
|
Player{0}
|
||||||
Player {0}
|
|
||||||
@@ -10,8 +10,7 @@ internal static class Resource
|
|||||||
public static Stream HereWeGo => GetStream();
|
public static Stream HereWeGo => GetStream();
|
||||||
public static Stream Introduction => GetStream();
|
public static Stream Introduction => GetStream();
|
||||||
public static Stream Rules => GetStream();
|
public static Stream Rules => GetStream();
|
||||||
public static Stream TooManyColumns => GetStream();
|
public static Stream NoFair => GetStream();
|
||||||
public static Stream TooManyRows => GetStream();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
internal static class Formats
|
internal static class Formats
|
||||||
|
|||||||
@@ -1 +1 @@
|
|||||||
You lose, player {0}
|
You lose, player{0}
|
||||||
Reference in New Issue
Block a user