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>
283 lines
9.8 KiB
C++
283 lines
9.8 KiB
C++
#include "position.h"
|
|
|
|
#include <bit>
|
|
#include <cstring>
|
|
#include <sstream>
|
|
|
|
namespace rigby {
|
|
|
|
namespace zobrist {
|
|
|
|
u64 piece_square[12][64];
|
|
u64 castling[16];
|
|
u64 ep_file[8];
|
|
u64 side;
|
|
|
|
void init() {
|
|
Prng rng(0xC0FFEE1234567ULL);
|
|
for (auto& piece : piece_square)
|
|
for (u64& k : piece) k = rng.next();
|
|
for (u64& k : castling) k = rng.next();
|
|
for (u64& k : ep_file) k = rng.next();
|
|
side = rng.next();
|
|
}
|
|
|
|
} // namespace zobrist
|
|
|
|
namespace {
|
|
|
|
// Index into castle_rook of a right (a single CastlingRight bit).
|
|
constexpr int right_index(int right) { return std::countr_zero(unsigned(right)); }
|
|
|
|
} // namespace
|
|
|
|
Position::Position() { std::memset(board, NO_PIECE, sizeof(board)); }
|
|
|
|
void Position::put_piece(int piece, int sq) {
|
|
board[sq] = u8(piece);
|
|
pieces[piece_type(piece)] |= bb(sq);
|
|
colors[piece_color(piece)] |= bb(sq);
|
|
key ^= zobrist::piece_square[piece][sq];
|
|
}
|
|
|
|
void Position::remove_piece(int sq) {
|
|
const int piece = board[sq];
|
|
board[sq] = NO_PIECE;
|
|
pieces[piece_type(piece)] ^= bb(sq);
|
|
colors[piece_color(piece)] ^= bb(sq);
|
|
key ^= zobrist::piece_square[piece][sq];
|
|
}
|
|
|
|
void Position::move_piece(int from, int to) {
|
|
const int piece = board[from];
|
|
const u64 mask = bb(from) | bb(to);
|
|
board[from] = NO_PIECE;
|
|
board[to] = u8(piece);
|
|
pieces[piece_type(piece)] ^= mask;
|
|
colors[piece_color(piece)] ^= mask;
|
|
key ^= zobrist::piece_square[piece][from] ^ zobrist::piece_square[piece][to];
|
|
}
|
|
|
|
bool Position::set_fen(std::string_view fen) {
|
|
*this = Position();
|
|
|
|
std::istringstream ss{std::string(fen)};
|
|
std::string placement, side, rights, ep_str;
|
|
ss >> placement >> side >> rights >> ep_str;
|
|
if (placement.empty() || side.empty()) return false;
|
|
|
|
int rank = 7, file = 0;
|
|
for (char c : placement) {
|
|
if (c == '/') {
|
|
rank--;
|
|
file = 0;
|
|
} else if (c >= '1' && c <= '8') {
|
|
file += c - '0';
|
|
} else {
|
|
const auto idx = PIECE_CHARS.find(c);
|
|
if (idx == std::string_view::npos || file > 7 || rank < 0) return false;
|
|
put_piece(int(idx), make_square(file, rank));
|
|
file++;
|
|
}
|
|
}
|
|
if (popcount(pieces_of(WHITE, KING)) != 1 || popcount(pieces_of(BLACK, KING)) != 1) return false;
|
|
|
|
stm = side == "b" ? BLACK : WHITE;
|
|
|
|
// KQkq (X-FEN: the outermost rook on that side of the king) or
|
|
// Shredder-FEN file letters (HAha) naming the castling rook.
|
|
for (char c : rights) {
|
|
if (c == '-') continue;
|
|
const int color = (c >= 'a' && c <= 'z') ? BLACK : WHITE;
|
|
const char upper = char(color == BLACK ? c - 32 : c);
|
|
const int rank_base = color == WHITE ? 0 : 56;
|
|
const int kfile = file_of(king_square(color));
|
|
if (rank_of(king_square(color)) != rank_of(rank_base)) continue;
|
|
const u64 rooks = pieces_of(color, ROOK) & rank_bb(rank_base);
|
|
int rook = NO_SQUARE;
|
|
if (upper == 'K') {
|
|
for (int f = 7; f > kfile && rook == NO_SQUARE; f--)
|
|
if (rooks & bb(rank_base + f)) rook = rank_base + f;
|
|
} else if (upper == 'Q') {
|
|
for (int f = 0; f < kfile && rook == NO_SQUARE; f++)
|
|
if (rooks & bb(rank_base + f)) rook = rank_base + f;
|
|
} else if (upper >= 'A' && upper <= 'H' && (rooks & bb(rank_base + (upper - 'A')))) {
|
|
rook = rank_base + (upper - 'A');
|
|
}
|
|
if (rook == NO_SQUARE || file_of(rook) == kfile) continue;
|
|
const bool king_side = file_of(rook) > kfile;
|
|
const int right = color == WHITE ? (king_side ? WHITE_OO : WHITE_OOO) : (king_side ? BLACK_OO : BLACK_OOO);
|
|
castling |= right;
|
|
castle_rook[right_index(right)] = u8(rook);
|
|
}
|
|
|
|
if (ep_str.size() == 2 && ep_str[0] >= 'a' && ep_str[0] <= 'h') {
|
|
const int sq = make_square(ep_str[0] - 'a', ep_str[1] - '1');
|
|
if (sq >= 0 && sq < 64 && (attacks::pawn[stm ^ 1][sq] & pieces_of(stm, PAWN))) ep = sq;
|
|
}
|
|
|
|
if (!(ss >> halfmove)) halfmove = 0;
|
|
if (!(ss >> fullmove)) fullmove = 1;
|
|
|
|
key ^= zobrist::castling[castling];
|
|
if (ep != NO_SQUARE) key ^= zobrist::ep_file[file_of(ep)];
|
|
if (stm == BLACK) key ^= zobrist::side;
|
|
|
|
// The side not to move must not be in check.
|
|
return !(attackers_to(king_square(stm ^ 1), occupancy()) & colors[stm]);
|
|
}
|
|
|
|
std::string Position::fen() const {
|
|
std::string out;
|
|
for (int rank = 7; rank >= 0; rank--) {
|
|
int empty = 0;
|
|
for (int file = 0; file < 8; file++) {
|
|
const int piece = board[make_square(file, rank)];
|
|
if (piece == NO_PIECE) {
|
|
empty++;
|
|
continue;
|
|
}
|
|
if (empty) out += char('0' + empty);
|
|
empty = 0;
|
|
out += PIECE_CHARS[piece];
|
|
}
|
|
if (empty) out += char('0' + empty);
|
|
if (rank) out += '/';
|
|
}
|
|
|
|
out += stm == WHITE ? " w " : " b ";
|
|
if (!castling) out += '-';
|
|
for (int right : {WHITE_OO, WHITE_OOO, BLACK_OO, BLACK_OOO}) {
|
|
if (!(castling & right)) continue;
|
|
// K/Q when the rook is the outermost one on its side (X-FEN), else its file.
|
|
const int color = right & (WHITE_OO | WHITE_OOO) ? WHITE : BLACK;
|
|
const int rook = castle_rook[right_index(right)];
|
|
const int ksq = king_square(color);
|
|
const u64 rooks = pieces_of(color, ROOK) & rank_bb(rook);
|
|
const u64 beyond = file_of(rook) > file_of(ksq) ? rooks & ~((bb(rook) << 1) - 1) : rooks & (bb(rook) - 1);
|
|
char c = beyond ? char('A' + file_of(rook)) : (file_of(rook) > file_of(ksq) ? 'K' : 'Q');
|
|
if (color == BLACK) c = char(c + 32);
|
|
out += c;
|
|
}
|
|
out += ' ';
|
|
out += ep == NO_SQUARE ? "-" : square_name(ep);
|
|
out += ' ' + std::to_string(halfmove) + ' ' + std::to_string(fullmove);
|
|
return out;
|
|
}
|
|
|
|
void Position::make_move(Move m) {
|
|
const int from = m.from();
|
|
const int to = m.to();
|
|
const int us = stm;
|
|
const int them = us ^ 1;
|
|
const int moving = piece_type(board[from]);
|
|
|
|
halfmove++;
|
|
if (ep != NO_SQUARE) {
|
|
key ^= zobrist::ep_file[file_of(ep)];
|
|
ep = NO_SQUARE;
|
|
}
|
|
|
|
if (m.is_castle()) {
|
|
// Lift both pieces first: in Chess960 the king and rook squares can overlap.
|
|
const int rook_from = castle_rook_square(m);
|
|
const int rank_base = us == WHITE ? 0 : 56;
|
|
const bool king_side = m.flag() == Move::KING_CASTLE;
|
|
remove_piece(from);
|
|
remove_piece(rook_from);
|
|
put_piece(make_piece(us, KING), to);
|
|
put_piece(make_piece(us, ROOK), rank_base + (king_side ? 5 : 3));
|
|
} else {
|
|
if (m.is_capture()) {
|
|
const int captured_sq = m.is_en_passant() ? (us == WHITE ? to - 8 : to + 8) : to;
|
|
remove_piece(captured_sq);
|
|
halfmove = 0;
|
|
}
|
|
move_piece(from, to);
|
|
}
|
|
|
|
if (moving == PAWN) {
|
|
halfmove = 0;
|
|
if (m.flag() == Move::DOUBLE_PUSH) {
|
|
const int ep_sq = (from + to) / 2;
|
|
if (attacks::pawn[us][ep_sq] & pieces_of(them, PAWN)) {
|
|
ep = ep_sq;
|
|
key ^= zobrist::ep_file[file_of(ep)];
|
|
}
|
|
} else if (m.is_promotion()) {
|
|
remove_piece(to);
|
|
put_piece(make_piece(us, m.promo_type()), to);
|
|
}
|
|
}
|
|
|
|
if (castling) {
|
|
// A king move drops both of its rights; a move from or to a castling
|
|
// rook's square drops that rook's right.
|
|
int lost = moving == KING ? (us == WHITE ? WHITE_OO | WHITE_OOO : BLACK_OO | BLACK_OOO) : 0;
|
|
for (int i = 0; i < 4; i++)
|
|
if (castle_rook[i] == from || castle_rook[i] == to) lost |= 1 << i;
|
|
if (castling & lost) {
|
|
key ^= zobrist::castling[castling];
|
|
castling &= ~lost;
|
|
key ^= zobrist::castling[castling];
|
|
}
|
|
}
|
|
|
|
stm = them;
|
|
key ^= zobrist::side;
|
|
if (stm == WHITE) fullmove++;
|
|
}
|
|
|
|
u64 Position::attackers_to(int sq, u64 occ) const {
|
|
return (attacks::pawn[BLACK][sq] & pieces_of(WHITE, PAWN)) | (attacks::pawn[WHITE][sq] & pieces_of(BLACK, PAWN)) |
|
|
(attacks::knight[sq] & pieces[KNIGHT]) | (attacks::king[sq] & pieces[KING]) |
|
|
(attacks::bishop(sq, occ) & (pieces[BISHOP] | pieces[QUEEN])) |
|
|
(attacks::rook(sq, occ) & (pieces[ROOK] | pieces[QUEEN]));
|
|
}
|
|
|
|
bool Position::square_attacked(int sq, int by, u64 occ) const {
|
|
const u64 them = colors[by];
|
|
return (attacks::pawn[by ^ 1][sq] & pieces[PAWN] & them) || (attacks::knight[sq] & pieces[KNIGHT] & them) ||
|
|
(attacks::king[sq] & pieces[KING] & them) ||
|
|
(attacks::bishop(sq, occ) & (pieces[BISHOP] | pieces[QUEEN]) & them) ||
|
|
(attacks::rook(sq, occ) & (pieces[ROOK] | pieces[QUEEN]) & them);
|
|
}
|
|
|
|
bool Position::insufficient_material() const {
|
|
if (pieces[PAWN] | pieces[ROOK] | pieces[QUEEN]) return false;
|
|
// K vs K, or K + single minor vs K.
|
|
return popcount(occupancy()) <= 3;
|
|
}
|
|
|
|
Move Position::parse_uci(std::string_view text) const {
|
|
MoveList list;
|
|
generate_legal(*this, list);
|
|
for (Move m : list)
|
|
if (uci(m) == text) return m;
|
|
return {};
|
|
}
|
|
|
|
std::string Position::uci(Move m) const {
|
|
if (!m.is_castle() || !chess960) return m.to_uci();
|
|
return square_name(m.from()) + square_name(castle_rook_square(m));
|
|
}
|
|
|
|
std::string Position::pretty() const {
|
|
std::string out = "\n +---+---+---+---+---+---+---+---+\n";
|
|
for (int rank = 7; rank >= 0; rank--) {
|
|
for (int file = 0; file < 8; file++) {
|
|
const int piece = board[make_square(file, rank)];
|
|
out += " | ";
|
|
out += piece == NO_PIECE ? ' ' : PIECE_CHARS[piece];
|
|
}
|
|
out += " | " + std::to_string(rank + 1) + "\n +---+---+---+---+---+---+---+---+\n";
|
|
}
|
|
out += " a b c d e f g h\n\nFen: " + fen() + "\n";
|
|
char buf[32];
|
|
std::snprintf(buf, sizeof(buf), "%016llX", static_cast<unsigned long long>(key));
|
|
out += "Key: " + std::string(buf) + "\n";
|
|
return out;
|
|
}
|
|
|
|
} // namespace rigby
|