Finish game

This commit is contained in:
Andrew Cooper
2022-08-19 08:41:11 +10:00
parent 876c71d89b
commit c603549db3
4 changed files with 44 additions and 26 deletions

View File

@@ -3,7 +3,8 @@ namespace OneCheck;
internal class Board
{
private readonly bool[][] _checkers;
private int _count;
private int _pieceCount;
private int _moveCount;
public Board()
{
@@ -12,16 +13,33 @@ internal class Board
.Select(r => Enumerable.Range(0, 8)
.Select(c => r <= 1 || r >= 6 || c <= 1 || c >= 6).ToArray())
.ToArray();
_count = 48;
_pieceCount = 48;
}
private bool this[int index]
{
get => _checkers[(index - 1) / 8][(index-1) % 8];
set => _checkers[(index - 1) / 8][(index-1) % 8] = value;
get => _checkers[index / 8][index % 8];
set => _checkers[index / 8][index % 8] = value;
}
public int Count => _count;
public bool PlayMove(IReadWrite io)
{
while (true)
{
var from = (int)io.ReadNumber(Prompts.From);
if (from == 0) { return false; }
var move = new Move { From = from - 1, To = (int)io.ReadNumber(Prompts.To) - 1 };
if (TryMove(move))
{
_moveCount++;
return true;
}
io.Write(Streams.IllegalMove);
}
}
public bool TryMove(Move move)
{
@@ -30,7 +48,7 @@ internal class Board
this[move.From] = false;
this[move.Jumped] = false;
this[move.To] = true;
_count--;
_pieceCount--;
return true;
}
@@ -39,6 +57,8 @@ internal class Board
private bool IsPieceJumpingPieceToEmptySpace(Move move) => this[move.From] && this[move.Jumped] && !this[move.To];
public string GetReport() => string.Format(Formats.Results, _moveCount, _pieceCount);
public override string ToString() =>
string.Join(Environment.NewLine, _checkers.Select(r => string.Join(" ", r.Select(c => c ? " 1" : " 0"))));
}

View File

@@ -3,13 +3,10 @@ namespace OneCheck;
internal class Game
{
private readonly IReadWrite _io;
private readonly Board _board;
private int _moveCount;
public Game(IReadWrite io)
{
_io = io;
_board = new Board();
}
public void Play()
@@ -18,29 +15,31 @@ internal class Game
do
{
_io.WriteLine(_board);
_io.WriteLine();
} while (PlayMove());
var board = new Board();
do
{
_io.WriteLine(board);
_io.WriteLine();
} while (board.PlayMove(_io));
_io.WriteLine(Formats.Results, _moveCount, _board.Count);
_io.WriteLine(board.GetReport());
} while (_io.ReadYesNo(Prompts.TryAgain) == "yes");
_io.Write(Streams.Bye);
}
}
private bool PlayMove()
internal static class IOExtensions
{
internal static string ReadYesNo(this IReadWrite io, string prompt)
{
while (true)
{
var from = (int)_io.ReadNumber(Prompts.From);
if (from == 0) { return false; }
var response = io.ReadString(prompt).ToLower();
var move = new Move { From = from, To = (int)_io.ReadNumber(Prompts.To) };
if (response == "yes" || response == "no") { return response; }
if (_board.TryMove(move))
{
_moveCount++;
return true;
}
_io.Write(Streams.IllegalMove);
io.Write(Streams.YesOrNo);
}
}
}

View File

@@ -6,7 +6,7 @@ internal class Move
public int To { get; init; }
public int Jumped => (From + To) / 2;
public bool IsInRange => From >= 1 && From <= 64 && To >= 1 && To <= 64;
public bool IsInRange => From >= 0 && From <= 63 && To >= 0 && To <= 63;
public bool IsTwoSpacesDiagonally => RowDelta == 2 && ColumnDelta == 2;
private int RowDelta => Math.Abs(From / 8 - To / 8);
private int ColumnDelta => Math.Abs(From % 8 - To % 8);

View File

@@ -1,4 +1,3 @@
You made {0} jumps and had {1} pieces
remaining on the board.