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>
237 lines
9.9 KiB
C++
237 lines
9.9 KiB
C++
#include <bit>
|
|
|
|
#include "position.h"
|
|
|
|
namespace rigby {
|
|
|
|
namespace {
|
|
|
|
void add_targets(MoveList& list, int from, u64 targets, u64 theirs) {
|
|
while (targets) {
|
|
const int to = pop_lsb(targets);
|
|
list.add(Move(from, to, (theirs & bb(to)) ? Move::CAPTURE : Move::QUIET));
|
|
}
|
|
}
|
|
|
|
void add_pawn_move(MoveList& list, int from, int to, bool capture, bool promotion) {
|
|
if (promotion) {
|
|
const int base = capture ? Move::PROMO_CAPTURE : Move::PROMO;
|
|
// Queen first: it is by far the most common promotion.
|
|
for (int k = 3; k >= 0; k--) list.add(Move(from, to, base + k));
|
|
} else {
|
|
list.add(Move(from, to, capture ? Move::CAPTURE : Move::QUIET));
|
|
}
|
|
}
|
|
|
|
// Our pieces pinned to our king by an enemy slider.
|
|
u64 pinned_pieces(const Position& pos, int us, int ksq, u64 occ) {
|
|
const u64 diag_sliders = pos.pieces[BISHOP] | pos.pieces[QUEEN];
|
|
const u64 orth_sliders = pos.pieces[ROOK] | pos.pieces[QUEEN];
|
|
u64 pinned = 0;
|
|
u64 snipers = ((attacks::rook(ksq, 0) & orth_sliders) | (attacks::bishop(ksq, 0) & diag_sliders)) & pos.colors[us ^ 1];
|
|
while (snipers) {
|
|
const int s = pop_lsb(snipers);
|
|
const u64 blockers = attacks::between[ksq][s] & occ;
|
|
if (blockers && !(blockers & (blockers - 1)) && (blockers & pos.colors[us])) pinned |= blockers;
|
|
}
|
|
return pinned;
|
|
}
|
|
|
|
// Fully legal generation using check and pin masks. With NOISY only captures
|
|
// (en passant included) and queen promotions, in the same order as the full
|
|
// generation lists them.
|
|
template <bool NOISY>
|
|
void generate(const Position& pos, MoveList& list) {
|
|
list.clear();
|
|
|
|
const int us = pos.stm;
|
|
const int them = us ^ 1;
|
|
const u64 occ = pos.occupancy();
|
|
const u64 ours = pos.colors[us];
|
|
const u64 theirs = pos.colors[them];
|
|
const int ksq = pos.king_square(us);
|
|
|
|
const u64 checkers = pos.attackers_to(ksq, occ) & theirs;
|
|
const int num_checkers = popcount(checkers);
|
|
|
|
// King moves: the king itself must not block a slider ray on its destination.
|
|
{
|
|
const u64 occ_no_king = occ ^ bb(ksq);
|
|
u64 targets = attacks::king[ksq] & (NOISY ? theirs : ~ours);
|
|
while (targets) {
|
|
const int to = pop_lsb(targets);
|
|
if (!pos.square_attacked(to, them, occ_no_king))
|
|
list.add(Move(ksq, to, (theirs & bb(to)) ? Move::CAPTURE : Move::QUIET));
|
|
}
|
|
}
|
|
|
|
if (num_checkers > 1) return;
|
|
|
|
const u64 check_mask = num_checkers ? (attacks::between[ksq][lsb(checkers)] | checkers) : ~0ULL;
|
|
// Squares a piece may move to: anything but our own pieces, or only theirs.
|
|
const u64 target_mask = check_mask & (NOISY ? theirs : ~ours);
|
|
const u64 diag_sliders = pos.pieces[BISHOP] | pos.pieces[QUEEN];
|
|
const u64 orth_sliders = pos.pieces[ROOK] | pos.pieces[QUEEN];
|
|
const u64 pinned = pinned_pieces(pos, us, ksq, occ);
|
|
|
|
// A pinned knight can never move.
|
|
u64 knights = pos.pieces_of(us, KNIGHT) & ~pinned;
|
|
while (knights) {
|
|
const int from = pop_lsb(knights);
|
|
add_targets(list, from, attacks::knight[from] & target_mask, theirs);
|
|
}
|
|
|
|
u64 diag = diag_sliders & ours;
|
|
while (diag) {
|
|
const int from = pop_lsb(diag);
|
|
u64 targets = attacks::bishop(from, occ) & target_mask;
|
|
if (pinned & bb(from)) targets &= attacks::line[ksq][from];
|
|
add_targets(list, from, targets, theirs);
|
|
}
|
|
|
|
u64 orth = orth_sliders & ours;
|
|
while (orth) {
|
|
const int from = pop_lsb(orth);
|
|
u64 targets = attacks::rook(from, occ) & target_mask;
|
|
if (pinned & bb(from)) targets &= attacks::line[ksq][from];
|
|
add_targets(list, from, targets, theirs);
|
|
}
|
|
|
|
const int up = us == WHITE ? 8 : -8;
|
|
const u64 start_rank = us == WHITE ? (RANK_1_BB << 8) : (RANK_8_BB >> 8);
|
|
const u64 promo_rank = us == WHITE ? RANK_8_BB : RANK_1_BB;
|
|
|
|
u64 pawns = pos.pieces_of(us, PAWN);
|
|
while (pawns) {
|
|
const int from = pop_lsb(pawns);
|
|
u64 allowed = check_mask;
|
|
if (pinned & bb(from)) allowed &= attacks::line[ksq][from];
|
|
|
|
const int to = from + up;
|
|
if (!(occ & bb(to))) {
|
|
if constexpr (NOISY) {
|
|
if ((allowed & bb(to)) && (promo_rank & bb(to))) list.add(Move(from, to, Move::PROMO + 3));
|
|
} else {
|
|
if (allowed & bb(to)) add_pawn_move(list, from, to, false, promo_rank & bb(to));
|
|
const int to2 = to + up;
|
|
if ((start_rank & bb(from)) && !(occ & bb(to2)) && (allowed & bb(to2)))
|
|
list.add(Move(from, to2, Move::DOUBLE_PUSH));
|
|
}
|
|
}
|
|
|
|
u64 captures = attacks::pawn[us][from] & theirs & allowed;
|
|
while (captures) {
|
|
const int cap = pop_lsb(captures);
|
|
add_pawn_move(list, from, cap, true, promo_rank & bb(cap));
|
|
}
|
|
|
|
if (pos.ep != NO_SQUARE && (attacks::pawn[us][from] & bb(pos.ep))) {
|
|
// Rare enough to verify by brute force: rebuild the occupancy and look for any attacker.
|
|
const int captured_sq = pos.ep - up;
|
|
const u64 occ_after = (occ ^ bb(from) ^ bb(captured_sq)) | bb(pos.ep);
|
|
const u64 their_pawns = pos.pieces_of(them, PAWN) & ~bb(captured_sq);
|
|
const u64 attackers = (attacks::rook(ksq, occ_after) & orth_sliders & theirs) |
|
|
(attacks::bishop(ksq, occ_after) & diag_sliders & theirs) |
|
|
(attacks::knight[ksq] & pos.pieces_of(them, KNIGHT)) |
|
|
(attacks::pawn[us][ksq] & their_pawns);
|
|
if (!attackers) list.add(Move(from, pos.ep, Move::EN_PASSANT));
|
|
}
|
|
}
|
|
|
|
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 u64 our_rooks = pos.pieces_of(us, ROOK);
|
|
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
|
|
|
|
void generate_legal(const Position& pos, MoveList& list) { generate<false>(pos, list); }
|
|
|
|
void generate_noisy(const Position& pos, MoveList& list) { generate<true>(pos, list); }
|
|
|
|
bool has_legal_move(const Position& pos) {
|
|
const int us = pos.stm;
|
|
const int them = us ^ 1;
|
|
const u64 occ = pos.occupancy();
|
|
const u64 ours = pos.colors[us];
|
|
const int ksq = pos.king_square(us);
|
|
|
|
// 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();
|
|
}
|
|
|
|
// Not in check, so castling only adds moves when the king can already step
|
|
// to the square next to it; the king, the pieces and the pawns decide.
|
|
const u64 pinned = pinned_pieces(pos, us, ksq, occ);
|
|
|
|
u64 knights = pos.pieces_of(us, KNIGHT) & ~pinned;
|
|
while (knights)
|
|
if (attacks::knight[pop_lsb(knights)] & ~ours) return true;
|
|
|
|
u64 targets = attacks::king[ksq] & ~ours;
|
|
while (targets)
|
|
if (!pos.square_attacked(pop_lsb(targets), them, occ ^ bb(ksq))) return true;
|
|
|
|
u64 sliders = (pos.pieces[BISHOP] | pos.pieces[QUEEN]) & ours;
|
|
while (sliders) {
|
|
const int from = pop_lsb(sliders);
|
|
u64 t = attacks::bishop(from, occ) & ~ours;
|
|
if (pinned & bb(from)) t &= attacks::line[ksq][from];
|
|
if (t) return true;
|
|
}
|
|
sliders = (pos.pieces[ROOK] | pos.pieces[QUEEN]) & ours;
|
|
while (sliders) {
|
|
const int from = pop_lsb(sliders);
|
|
u64 t = attacks::rook(from, occ) & ~ours;
|
|
if (pinned & bb(from)) t &= attacks::line[ksq][from];
|
|
if (t) return true;
|
|
}
|
|
|
|
const int up = us == WHITE ? 8 : -8;
|
|
u64 pawns = pos.pieces_of(us, PAWN);
|
|
while (pawns) {
|
|
const int from = pop_lsb(pawns);
|
|
u64 t = attacks::pawn[us][from] & pos.colors[them];
|
|
if (!(occ & bb(from + up))) t |= bb(from + up);
|
|
if (pinned & bb(from)) t &= attacks::line[ksq][from];
|
|
if (t) return true;
|
|
}
|
|
return false;
|
|
}
|
|
|
|
} // namespace rigby
|