isValidMove static method

bool isValidMove(
  1. ChessBoardInterface board,
  2. Position from,
  3. Position to
)

Validates if a move is legal based on the PieceType and current board state.

Implementation

static bool isValidMove(
  ChessBoardInterface board,
  Position from,
  Position to,
) {
  ChessPiece? piece = board.getPiece(from);
  if (piece == null) return false; // No piece to move

  ChessPiece? targetPiece = board.getPiece(to);
  if (targetPiece != null && targetPiece.color == piece.color) {
    return false; // Cannot capture own piece
  }

  switch (piece.type) {
    case PieceType.pawn:
      return _validatePawnMove(piece, from, to, board);
    case PieceType.knight:
      return _validateKnightMove(from, to);
    case PieceType.bishop:
      return _validateBishopMove(from, to, board);
    case PieceType.rook:
      return _validateRookMove(from, to, board);
    case PieceType.queen:
      return _validateQueenMove(from, to, board);
    case PieceType.king:
      return _validateKingMove(from, to);
  }
}