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>
80 lines
2.6 KiB
C++
80 lines
2.6 KiB
C++
#pragma once
|
|
|
|
#include <string>
|
|
#include <string_view>
|
|
|
|
#include "bitboard.h"
|
|
#include "types.h"
|
|
|
|
namespace rigby {
|
|
|
|
namespace zobrist {
|
|
void init();
|
|
}
|
|
|
|
// UCI_Chess960: castling moves are written as the king taking its own rook
|
|
// (e1h1) instead of the king's destination (e1g1). Positions themselves always
|
|
// support any castling rook placement.
|
|
inline bool chess960 = false;
|
|
|
|
// Plain copy-make position. Small enough to copy at every tree step.
|
|
struct Position {
|
|
u64 pieces[6] = {};
|
|
u64 colors[2] = {};
|
|
u8 board[64];
|
|
int stm = WHITE;
|
|
int castling = 0;
|
|
// The rook each castling right moves, indexed by the right's bit
|
|
// (WHITE_OO, WHITE_OOO, BLACK_OO, BLACK_OOO).
|
|
u8 castle_rook[4] = {H1, A1, H8, A8};
|
|
int ep = NO_SQUARE; // only set when a capture there is pseudo-legal
|
|
int halfmove = 0;
|
|
int fullmove = 1;
|
|
u64 key = 0;
|
|
|
|
Position();
|
|
|
|
bool set_fen(std::string_view fen);
|
|
std::string fen() const;
|
|
|
|
void make_move(Move m);
|
|
|
|
u64 occupancy() const { return colors[WHITE] | colors[BLACK]; }
|
|
u64 pieces_of(int color, int type) const { return pieces[type] & colors[color]; }
|
|
int king_square(int color) const { return lsb(pieces_of(color, KING)); }
|
|
int piece_on(int sq) const { return board[sq]; }
|
|
|
|
u64 attackers_to(int sq, u64 occ) const;
|
|
bool square_attacked(int sq, int by, u64 occ) const;
|
|
u64 checkers() const { return attackers_to(king_square(stm), occupancy()) & colors[stm ^ 1]; }
|
|
bool in_check() const { return checkers() != 0; }
|
|
bool insufficient_material() const;
|
|
|
|
// Returns a null move when the string is not a legal move here.
|
|
Move parse_uci(std::string_view uci) const;
|
|
// The move in UCI notation, castling written as chess960 asks. Works for
|
|
// any move of the game (the castling rooks never change), so the root
|
|
// position can print a whole PV.
|
|
std::string uci(Move m) const;
|
|
// The rook a castling move takes along; the king's rank tells the colour.
|
|
int castle_rook_square(Move m) const {
|
|
return castle_rook[(rank_of(m.from()) == 0 ? 0 : 2) + (m.flag() == Move::QUEEN_CASTLE)];
|
|
}
|
|
|
|
std::string pretty() const;
|
|
|
|
private:
|
|
void put_piece(int piece, int sq);
|
|
void remove_piece(int sq);
|
|
void move_piece(int from, int to);
|
|
};
|
|
|
|
void generate_legal(const Position& pos, MoveList& list);
|
|
// Legal captures (en passant included) and queen promotions, in the order
|
|
// generate_legal lists them.
|
|
void generate_noisy(const Position& pos, MoveList& list);
|
|
// Same as a non-empty generate_legal, but stops at the first move it finds.
|
|
bool has_legal_move(const Position& pos);
|
|
|
|
} // namespace rigby
|