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:
rektdieandclaude committed 2026-11-15 00:31:20 +01:00
commit d3044c0b88
19 files changed
+1997

No files matched your search

+150
View File
@@ -0,0 +1,150 @@
#include "bitboard.h"
namespace rigby::attacks {
u64 pawn[2][64];
u64 knight[64];
u64 king[64];
u64 between[64][64];
u64 line[64][64];
namespace {
struct Magic {
u64 mask;
u64 magic;
u64* table;
int shift;
unsigned index(u64 occ) const { return unsigned(((occ & mask) * magic) >> shift); }
};
Magic rook_magics[64];
Magic bishop_magics[64];
u64 rook_table[0x19000];
u64 bishop_table[0x1480];
constexpr int ROOK_DIRS[4][2] = {{1, 0}, {-1, 0}, {0, 1}, {0, -1}};
constexpr int BISHOP_DIRS[4][2] = {{1, 1}, {1, -1}, {-1, 1}, {-1, -1}};
u64 slide(int sq, u64 occ, const int (&dirs)[4][2]) {
u64 result = 0;
for (const auto& d : dirs) {
int f = file_of(sq) + d[0];
int r = rank_of(sq) + d[1];
while (f >= 0 && f < 8 && r >= 0 && r < 8) {
const int s = make_square(f, r);
result |= bb(s);
if (occ & bb(s)) break;
f += d[0];
r += d[1];
}
}
return result;
}
u64 step_attacks(int sq, const int (*steps)[2], int count) {
u64 result = 0;
for (int i = 0; i < count; i++) {
const int f = file_of(sq) + steps[i][0];
const int r = rank_of(sq) + steps[i][1];
if (f >= 0 && f < 8 && r >= 0 && r < 8) result |= bb(make_square(f, r));
}
return result;
}
// Finds a magic for every square by trial and error (fancy magic bitboards).
void init_magics(Magic* magics, u64* table, const int (&dirs)[4][2], u64 seed) {
static u64 occupancies[4096];
static u64 reference[4096];
static int epoch[4096];
static int attempt = 0; // shared across calls, so stale epochs never look fresh
Prng rng(seed);
u64* ptr = table;
for (int sq = 0; sq < 64; sq++) {
Magic& m = magics[sq];
const u64 edges = ((RANK_1_BB | RANK_8_BB) & ~rank_bb(sq)) | ((FILE_A_BB | FILE_H_BB) & ~file_bb(sq));
m.mask = slide(sq, 0, dirs) & ~edges;
const int bits = popcount(m.mask);
m.shift = 64 - bits;
m.table = ptr;
int size = 0;
u64 subset = 0;
do {
occupancies[size] = subset;
reference[size] = slide(sq, subset, dirs);
size++;
subset = (subset - m.mask) & m.mask;
} while (subset);
for (;;) {
do {
m.magic = rng.sparse();
} while (popcount((m.magic * m.mask) >> 56) < 6);
attempt++;
bool ok = true;
for (int i = 0; i < size; i++) {
const unsigned idx = m.index(occupancies[i]);
if (epoch[idx] < attempt) {
epoch[idx] = attempt;
ptr[idx] = reference[i];
} else if (ptr[idx] != reference[i]) {
ok = false;
break;
}
}
if (ok) break;
}
ptr += size_t(1) << bits;
}
}
} // namespace
u64 bishop(int sq, u64 occ) {
const Magic& m = bishop_magics[sq];
return m.table[m.index(occ)];
}
u64 rook(int sq, u64 occ) {
const Magic& m = rook_magics[sq];
return m.table[m.index(occ)];
}
void init() {
static constexpr int KNIGHT_STEPS[8][2] = {{1, 2}, {2, 1}, {2, -1}, {1, -2}, {-1, -2}, {-2, -1}, {-2, 1}, {-1, 2}};
static constexpr int KING_STEPS[8][2] = {{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
static constexpr int WHITE_PAWN_STEPS[2][2] = {{-1, 1}, {1, 1}};
static constexpr int BLACK_PAWN_STEPS[2][2] = {{-1, -1}, {1, -1}};
for (int sq = 0; sq < 64; sq++) {
knight[sq] = step_attacks(sq, KNIGHT_STEPS, 8);
king[sq] = step_attacks(sq, KING_STEPS, 8);
pawn[WHITE][sq] = step_attacks(sq, WHITE_PAWN_STEPS, 2);
pawn[BLACK][sq] = step_attacks(sq, BLACK_PAWN_STEPS, 2);
}
init_magics(rook_magics, rook_table, ROOK_DIRS, 0x2545F4914F6CDD1DULL);
init_magics(bishop_magics, bishop_table, BISHOP_DIRS, 0x5DEECE66DULL);
for (int a = 0; a < 64; a++) {
for (int b = 0; b < 64; b++) {
between[a][b] = line[a][b] = 0;
if (a == b) continue;
if (bishop(a, 0) & bb(b)) {
line[a][b] = (bishop(a, 0) & bishop(b, 0)) | bb(a) | bb(b);
between[a][b] = bishop(a, bb(b)) & bishop(b, bb(a));
} else if (rook(a, 0) & bb(b)) {
line[a][b] = (rook(a, 0) & rook(b, 0)) | bb(a) | bb(b);
between[a][b] = rook(a, bb(b)) & rook(b, bb(a));
}
}
}
}
} // namespace rigby::attacks