Add move logic

This commit is contained in:
Andrew Cooper
2022-08-16 09:01:14 +10:00
parent 35f8248b1f
commit 876c71d89b
3 changed files with 70 additions and 5 deletions

View File

@@ -2,17 +2,43 @@ namespace OneCheck;
internal class Board
{
private readonly int[][] _checkers;
private readonly bool[][] _checkers;
private int _count;
public Board()
{
_checkers =
Enumerable.Range(0, 8)
.Select(r => Enumerable.Range(0, 8)
.Select(c => r > 1 && r < 6 && c > 1 && c < 6 ? 0 : 1).ToArray())
.Select(c => r <= 1 || r >= 6 || c <= 1 || c >= 6).ToArray())
.ToArray();
_count = 48;
}
private bool this[int index]
{
get => _checkers[(index - 1) / 8][(index-1) % 8];
set => _checkers[(index - 1) / 8][(index-1) % 8] = value;
}
public int Count => _count;
public bool TryMove(Move move)
{
if (move.IsInRange && move.IsTwoSpacesDiagonally && IsPieceJumpingPieceToEmptySpace(move))
{
this[move.From] = false;
this[move.Jumped] = false;
this[move.To] = true;
_count--;
return true;
}
return false;
}
private bool IsPieceJumpingPieceToEmptySpace(Move move) => this[move.From] && this[move.Jumped] && !this[move.To];
public override string ToString() =>
string.Join(Environment.NewLine, _checkers.Select(r => string.Join(" ", r.Select(c => $" {c}"))));
}
string.Join(Environment.NewLine, _checkers.Select(r => string.Join(" ", r.Select(c => c ? " 1" : " 0"))));
}