feat: Chess960 support

Castling works with any rook placement: each castling right remembers
its rook square (Position::castle_rook), set from KQkq (X-FEN, the
outermost rook on that side of the king) or Shredder-FEN file letters.
Castling moves stay king-from to king-destination (g/c file), so move
indices, history tables and the bench are unchanged; make_move lifts
king and rook before placing them, since in 960 their squares can
overlap. Rights are dropped by a king move or a move from or to a
castling rook's square. Generation follows the 960 rules: every square
the king or rook crosses or lands on is empty apart from the two, and
no square the king crosses or lands on is attacked with the moving rook
out of the occupancy (the king's own square included when it does not
move: the rook leaving can uncover the rank). has_legal_move falls back
to full generation for non-standard castling setups.

UCI_Chess960 writes and reads castling as the king taking its rook;
FEN output uses KQkq for outermost rooks and file letters otherwise.
New command perftfile <epd> [depth] runs an EPD perft file.

Checked: the 960 positions of the standard Chess960 perft suite
(fischer.epd) all match to depth 5 (17.6 billion nodes), perftsuite 6
passes, has_legal_move agrees with full generation at every perft node
to depth 4 of the 960 suite, castling both ways over UCI in 960 mode.
Lc0's training data has about 4% Chess960 games, which the policy data
converter now keeps.

Bench: 529903

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
rektdieandclaude committed 2026-11-15 21:26:45 +01:00
1 parent 1dacc9c402
commit 9643c7b0a5
10 files changed
+203 -57

No files matched your search

+34 -14
View File
@@ -1,3 +1,5 @@
#include <bit>
#include "position.h"
namespace rigby {
@@ -138,20 +140,31 @@ void generate(const Position& pos, MoveList& list) {
if (NOISY || num_checkers) return;
// Castling, Chess960 rules: the king goes to the g/c file and the rook to
// the f/d file. Every square either piece crosses or lands on must be empty
// apart from the two of them, and the king must not cross or land on an
// attacked square. The moved rook is taken out of the occupancy for that
// check, since it may be what shields one of those squares.
const int rank_base = us == WHITE ? 0 : 56;
const int oo = us == WHITE ? WHITE_OO : BLACK_OO;
const int ooo = us == WHITE ? WHITE_OOO : BLACK_OOO;
const u64 our_rooks = pos.pieces_of(us, ROOK);
if ((pos.castling & oo) && (our_rooks & bb(rank_base + 7)) &&
!(occ & (bb(rank_base + 5) | bb(rank_base + 6))) && !pos.square_attacked(rank_base + 5, them, occ) &&
!pos.square_attacked(rank_base + 6, them, occ))
list.add(Move(ksq, rank_base + 6, Move::KING_CASTLE));
if ((pos.castling & ooo) && (our_rooks & bb(rank_base + 0)) &&
!(occ & (bb(rank_base + 1) | bb(rank_base + 2) | bb(rank_base + 3))) &&
!pos.square_attacked(rank_base + 3, them, occ) && !pos.square_attacked(rank_base + 2, them, occ))
list.add(Move(ksq, rank_base + 2, Move::QUEEN_CASTLE));
for (const bool king_side : {true, false}) {
const int right = us == WHITE ? (king_side ? WHITE_OO : WHITE_OOO) : (king_side ? BLACK_OO : BLACK_OOO);
if (!(pos.castling & right)) continue;
const int rook = pos.castle_rook[std::countr_zero(unsigned(right))];
if (!(our_rooks & bb(rook))) continue;
const int king_to = rank_base + (king_side ? 6 : 2);
const int rook_to = rank_base + (king_side ? 5 : 3);
const u64 others = occ ^ bb(ksq) ^ bb(rook);
const u64 king_path = attacks::between[ksq][king_to] | bb(king_to);
const u64 rook_path = attacks::between[rook][rook_to] | bb(rook_to);
if ((king_path | rook_path) & others) continue;
// The destination is checked even when the king already stands on it:
// the rook leaving can uncover an attack along the rank.
u64 check = king_path;
bool safe = true;
while (check && safe) safe = !pos.square_attacked(pop_lsb(check), them, others);
if (safe) list.add(Move(ksq, king_to, king_side ? Move::KING_CASTLE : Move::QUEEN_CASTLE));
}
}
} // namespace
@@ -167,8 +180,15 @@ bool has_legal_move(const Position& pos) {
const u64 ours = pos.colors[us];
const int ksq = pos.king_square(us);
// In check or with an en passant capture around, just generate everything.
if (pos.in_check() || pos.ep != NO_SQUARE) {
// In check, with an en passant capture around, or with Chess960 castling
// that a king step cannot stand in for, just generate everything.
const int our_rights = pos.castling & (us == WHITE ? WHITE_OO | WHITE_OOO : BLACK_OO | BLACK_OOO);
const bool standard_castling = !our_rights || (ksq == (us == WHITE ? E1 : E8) &&
(!(our_rights & (WHITE_OO | BLACK_OO)) ||
file_of(pos.castle_rook[us == WHITE ? 0 : 2]) == 7) &&
(!(our_rights & (WHITE_OOO | BLACK_OOO)) ||
file_of(pos.castle_rook[us == WHITE ? 1 : 3]) == 0));
if (pos.in_check() || pos.ep != NO_SQUARE || !standard_castling) {
MoveList list;
generate<false>(pos, list);
return !list.empty();