mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-12 07:40:50 -08:00
Add inner game loop
This commit is contained in:
@@ -23,13 +23,16 @@ internal class Cookie
|
||||
_bits[0][0] = 'P';
|
||||
}
|
||||
|
||||
public bool TryChomp(int row, int column)
|
||||
public bool TryChomp(int row, int column, out char chomped)
|
||||
{
|
||||
if (row < 1 || row > _rowCount || column < 1 || column > _columnCount || _bits[row - 1][column - 1] == ' ')
|
||||
{
|
||||
chomped = default;
|
||||
return false;
|
||||
}
|
||||
|
||||
chomped = _bits[row - 1][column - 1];
|
||||
|
||||
for (int r = row; r <= _rowCount; r++)
|
||||
{
|
||||
for (int c = column; c <= _columnCount; c++)
|
||||
|
||||
@@ -23,15 +23,42 @@ internal class Game
|
||||
|
||||
var (playerCount, rowCount, columnCount) = _io.ReadParameters();
|
||||
|
||||
var cookie = new Cookie(rowCount, columnCount);
|
||||
var player = new PlayerNumber(playerCount);
|
||||
var loser = Play(new Cookie(rowCount, columnCount), new PlayerNumber(playerCount));
|
||||
|
||||
_io.WriteLine(cookie);
|
||||
|
||||
_io.WriteLine(string.Format(Resource.Formats.Player, player));
|
||||
var (row, column) = _io.Read2Numbers(Resource.Prompts.Coordinates);
|
||||
_io.WriteLine(string.Format(Resource.Formats.YouLose, loser));
|
||||
|
||||
if (_io.ReadNumber("Again (1=Yes, 0=No!)") != 1) { break; }
|
||||
}
|
||||
}
|
||||
|
||||
private PlayerNumber Play(Cookie cookie, PlayerNumber player)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
_io.WriteLine(cookie);
|
||||
|
||||
var poisoned = Chomp(cookie, player);
|
||||
|
||||
if (poisoned) { return player; }
|
||||
|
||||
player++;
|
||||
}
|
||||
}
|
||||
|
||||
private bool Chomp(Cookie cookie, PlayerNumber player)
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
_io.WriteLine(string.Format(Resource.Formats.Player, player));
|
||||
|
||||
var (row, column) = _io.Read2Numbers(Resource.Prompts.Coordinates);
|
||||
|
||||
if (cookie.TryChomp((int)row, (int)column, out char chomped))
|
||||
{
|
||||
return chomped == 'P';
|
||||
}
|
||||
|
||||
_io.Write(Resource.Streams.NoFair);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1 +1 @@
|
||||
You lose, player{0}
|
||||
You lose, player{0}
|
||||
|
||||
Reference in New Issue
Block a user