58 lines
1.8 KiB
C#
58 lines
1.8 KiB
C#
namespace CheckersSpielBot
|
|
{
|
|
public class MoveGeneratorService
|
|
{
|
|
private readonly int[,] _board;
|
|
|
|
public MoveGeneratorService(int[,] board)
|
|
{
|
|
_board = board;
|
|
}
|
|
|
|
public List<Move> GetLegalMoves(int row, int col, bool capturesOnly)
|
|
{
|
|
var moves = new List<Move>();
|
|
int piece = _board[row, col];
|
|
if (piece == 0) return moves;
|
|
|
|
var dirs = ResolveDirections(piece);
|
|
|
|
// Captures first
|
|
foreach (var (dr, dc) in dirs)
|
|
{
|
|
int mr = row + dr, mc = col + dc;
|
|
int lr = row + 2 * dr, lc = col + 2 * dc;
|
|
|
|
if (!BoardHelper.IsWithinBounds(mr, mc) || !BoardHelper.IsWithinBounds(lr, lc))
|
|
continue;
|
|
|
|
int mid = _board[mr, mc];
|
|
if (mid != 0 && BoardHelper.IsOpponentPiece(piece, mid) && _board[lr, lc] == 0)
|
|
moves.Add(new Move(lr, lc, mr, mc));
|
|
}
|
|
|
|
if (capturesOnly || moves.Count > 0) return moves;
|
|
|
|
// Normal moves
|
|
foreach (var (dr, dc) in dirs)
|
|
{
|
|
int lr = row + dr, lc = col + dc;
|
|
if (BoardHelper.IsWithinBounds(lr, lc) && _board[lr, lc] == 0)
|
|
moves.Add(new Move(lr, lc, -1, -1));
|
|
}
|
|
|
|
return moves;
|
|
}
|
|
|
|
private static List<(int dr, int dc)> ResolveDirections(int piece)
|
|
{
|
|
bool isKing = BoardHelper.IsKing(piece);
|
|
bool isRedPiece = BoardHelper.IsRedPiece(piece);
|
|
|
|
var dirs = new List<(int, int)>();
|
|
if (isRedPiece || isKing) { dirs.Add((-1, -1)); dirs.Add((-1, +1)); }
|
|
if (!isRedPiece || isKing) { dirs.Add((+1, -1)); dirs.Add((+1, +1)); }
|
|
return dirs;
|
|
}
|
|
}
|
|
} |