mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-23 07:29:02 -08:00
Add move logic
This commit is contained in:
@@ -4,6 +4,7 @@ internal class Game
|
||||
{
|
||||
private readonly IReadWrite _io;
|
||||
private readonly Board _board;
|
||||
private int _moveCount;
|
||||
|
||||
public Game(IReadWrite io)
|
||||
{
|
||||
@@ -15,6 +16,31 @@ internal class Game
|
||||
{
|
||||
_io.Write(Streams.Introduction);
|
||||
|
||||
_io.WriteLine(_board);
|
||||
do
|
||||
{
|
||||
_io.WriteLine(_board);
|
||||
_io.WriteLine();
|
||||
} while (PlayMove());
|
||||
|
||||
_io.WriteLine(Formats.Results, _moveCount, _board.Count);
|
||||
}
|
||||
|
||||
private bool PlayMove()
|
||||
{
|
||||
while (true)
|
||||
{
|
||||
var from = (int)_io.ReadNumber(Prompts.From);
|
||||
if (from == 0) { return false; }
|
||||
|
||||
var move = new Move { From = from, To = (int)_io.ReadNumber(Prompts.To) };
|
||||
|
||||
if (_board.TryMove(move))
|
||||
{
|
||||
_moveCount++;
|
||||
return true;
|
||||
}
|
||||
|
||||
_io.Write(Streams.IllegalMove);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user