Skip to content

feat: Add complete Chess960 (Fischer Random) support with UCI convention#134

Open
fafriat wants to merge 3 commits intobhlangonijr:masterfrom
fafriat:master
Open

feat: Add complete Chess960 (Fischer Random) support with UCI convention#134
fafriat wants to merge 3 commits intobhlangonijr:masterfrom
fafriat:master

Conversation

@fafriat
Copy link
Copy Markdown

@fafriat fafriat commented Mar 26, 2026

Summary

Adds full Chess960 castling support to chesslib, including move generation, validation, execution, undo, SAN/UCI parsing, and FEN round-trip. All 960 starting positions are handled correctly.

Closes #122

Changes

Detection

  • Board.loadFromFen() auto-detects Chess960 from Shredder-FEN (AHah) or KQkq with king not on e-file
  • Board.loadFromFen(fen, true) forces Chess960 mode (useful when PGN has [Variant "Chess960"])

GameContext

  • loadChess960() configures king/rook moves, traversed squares, and "must be empty" bitboards dynamically from actual piece positions

Move execution

  • Board.doMove() handles Chess960 castling with manual piece placement (avoids capture issues when king/rook are adjacent)
  • Board.isMoveLegal() excludes king/rook from occupancy checks for castling validation
  • MoveBackup.restore() handles Chess960 castle undo

Move generation

  • MoveGenerator.generateCastleMoves() excludes king/rook from occupancy checks

UCI support

  • Board.toUci(Move) returns king→rook notation for Chess960 castling (e.g. g1h1), following the official UCI Chess960 convention
  • Board.fromUci(String) parses king→rook UCI notation as castling
  • MoveList.loadFromText() uses board.fromUci() for correct Chess960 UCI parsing

SAN support

  • MoveList.encode() uses context.isCastleMove() instead of file-delta for castle detection (handles king moves of 0-1 files in Chess960)

FEN

  • Board.getFen() outputs Shredder-FEN castling rights for Chess960 positions

Edge cases handled

  • King already on destination (e.g. king on g1 doing O-O)
  • King and rook adjacent or swapping squares
  • Rook between king start and king destination
  • Castle rights properly lost when rook or king moves

Tests

14 new tests covering detection, castling O-O/O-O-O, undo, SAN parsing, UCI conversion, loadFromText, FEN round-trip, standard chess unaffected, and castle rights loss.

All 125 existing tests pass unchanged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

doMove allows invalid moves to be executed, isMoveLegal does not work

1 participant