hasLostCastlingRights static method

bool hasLostCastlingRights(
  1. ChessBoardInterface board,
  2. PieceColor color,
  3. bool kingSide
)

Checks if the player has lost castling rights based on the ChessBoardInterface.history.

Implementation

static bool hasLostCastlingRights(
  ChessBoardInterface board,
  PieceColor color,
  bool kingSide,
) {
  for (String fen in board.history) {
    List<String> parts = fen.split(" ");
    if (parts.length < 2) continue; // Invalid FEN format

    String castlingRights = parts[2]; // Extract castling field

    if (color == PieceColor.white) {
      if (kingSide && !castlingRights.contains("K")) return true;
      if (!kingSide && !castlingRights.contains("Q")) return true;
    } else {
      if (kingSide && !castlingRights.contains("k")) return true;
      if (!kingSide && !castlingRights.contains("q")) return true;
    }
  }
  return false;
}