feat: minimal MCTS engine with UCI
Bitboard board with fancy magics and fully legal move generation, checked by a perft suite. PUCT search over a node arena with FPU reduction and in-tree draw detection. Hand-written value (eval + quiescence) and heuristic policy stand in until the network is trained. UCI loop with a search thread, OpenBench-compatible bench and Makefile. Bench: 546127 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
commit
d3044c0b88
19 files changed
+1997
No files matched your search
@@ -0,0 +1,245 @@
|
||||
#include "position.h"
|
||||
|
||||
#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 {
|
||||
|
||||
// rights &= CASTLE_MASK[from] & CASTLE_MASK[to] after every move.
|
||||
constexpr int castle_mask(int sq) {
|
||||
switch (sq) {
|
||||
case A1: return ~WHITE_OOO & 15;
|
||||
case E1: return ~(WHITE_OO | WHITE_OOO) & 15;
|
||||
case H1: return ~WHITE_OO & 15;
|
||||
case A8: return ~BLACK_OOO & 15;
|
||||
case E8: return ~(BLACK_OO | BLACK_OOO) & 15;
|
||||
case H8: return ~BLACK_OO & 15;
|
||||
default: return 15;
|
||||
}
|
||||
}
|
||||
|
||||
} // 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;
|
||||
|
||||
for (char c : rights) {
|
||||
if (c == 'K') castling |= WHITE_OO;
|
||||
if (c == 'Q') castling |= WHITE_OOO;
|
||||
if (c == 'k') castling |= BLACK_OO;
|
||||
if (c == 'q') castling |= BLACK_OOO;
|
||||
}
|
||||
|
||||
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 += '-';
|
||||
if (castling & WHITE_OO) out += 'K';
|
||||
if (castling & WHITE_OOO) out += 'Q';
|
||||
if (castling & BLACK_OO) out += 'k';
|
||||
if (castling & BLACK_OOO) out += 'q';
|
||||
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_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);
|
||||
}
|
||||
} else if (m.is_castle()) {
|
||||
const int rank_base = us == WHITE ? 0 : 56;
|
||||
if (m.flag() == Move::KING_CASTLE)
|
||||
move_piece(rank_base + 7, rank_base + 5);
|
||||
else
|
||||
move_piece(rank_base + 0, rank_base + 3);
|
||||
}
|
||||
|
||||
key ^= zobrist::castling[castling];
|
||||
castling &= castle_mask(from) & castle_mask(to);
|
||||
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 uci) const {
|
||||
MoveList list;
|
||||
generate_legal(*this, list);
|
||||
for (Move m : list)
|
||||
if (m.to_uci() == uci) return m;
|
||||
return {};
|
||||
}
|
||||
|
||||
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
|
||||
Reference in new issue
Block a user