mirror of
https://github.com/coding-horror/basic-computer-games.git
synced 2025-12-26 12:51:29 -08:00
Add move logic
This commit is contained in:
@@ -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"))));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user