Files
Rigby/src/perft.cpp
T
rektdieandclaude 9643c7b0a5 feat: Chess960 support
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>
2026-11-15 21:26:45 +01:00

134 lines
4.6 KiB
C++

#include "perft.h"
#include <chrono>
#include <cstdlib>
#include <fstream>
#include <sstream>
#include <string>
#include <iostream>
namespace rigby {
u64 perft(const Position& pos, int depth) {
MoveList list;
generate_legal(pos, list);
if (depth <= 1) return depth == 1 ? u64(list.size()) : 1;
u64 nodes = 0;
for (Move m : list) {
Position child = pos;
child.make_move(m);
nodes += perft(child, depth - 1);
}
return nodes;
}
void perft_divide(const Position& pos, int depth) {
const auto start = std::chrono::steady_clock::now();
MoveList list;
generate_legal(pos, list);
u64 total = 0;
for (Move m : list) {
Position child = pos;
child.make_move(m);
const u64 n = depth > 1 ? perft(child, depth - 1) : 1;
total += n;
std::cout << pos.uci(m) << ": " << n << "\n";
}
const auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start).count();
std::cout << "\nNodes: " << total << "\nTime: " << ms << " ms\nNPS: " << (ms ? total * 1000 / ms : total) << std::endl;
}
bool perft_file(const std::string& path, int max_depth) {
std::ifstream in(path);
if (!in) {
std::cout << "cannot open " << path << std::endl;
return false;
}
bool all_ok = true;
int lines = 0, failed = 0;
u64 total = 0;
const auto start = std::chrono::steady_clock::now();
std::string line;
while (std::getline(in, line)) {
const auto semi = line.find(';');
if (semi == std::string::npos) continue;
Position pos;
if (!pos.set_fen(line.substr(0, semi))) {
std::cout << "[FAIL] bad fen: " << line << std::endl;
all_ok = false;
continue;
}
lines++;
std::istringstream ss(line.substr(semi));
std::string tag;
u64 expected = 0;
bool ok = true;
while (ss >> tag >> expected) {
const int depth = std::atoi(tag.c_str() + 2); // ";D<n>"
if (depth > max_depth) break;
const u64 n = perft(pos, depth);
total += n;
if (n != expected) {
std::cout << "[FAIL] " << line.substr(0, semi) << " depth " << depth << ": " << n << " (expected "
<< expected << ")" << std::endl;
ok = false;
break;
}
}
all_ok &= ok;
failed += !ok;
}
const auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start).count();
std::cout << (all_ok ? "All perft tests passed" : "PERFT FAILED") << " - " << lines << " positions, " << failed
<< " failed, " << total << " nodes in " << ms << " ms" << std::endl;
return all_ok;
}
bool perft_suite(int max_depth) {
struct Case {
const char* fen;
int depth;
u64 nodes;
};
static constexpr Case CASES[] = {
{"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", 6, 119060324},
{"r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1", 5, 193690690},
{"8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - 0 1", 7, 178633661},
{"r3k2r/Pppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/Pp1P2PP/R2Q1RK1 w kq - 0 1", 5, 15833292},
{"rnbq1k1r/pp1Pbppp/2p5/8/2B5/8/PPP1NnPP/RNBQK2R w KQ - 1 8", 5, 89941194},
{"r4rk1/1pp1qppp/p1np1n2/2b1p1B1/2B1P1b1/P1NP1N2/1PP1QPPP/R4RK1 w - - 0 10", 5, 164075551},
{"8/8/1k6/2b5/2pP4/8/5K2/8 b - d3 0 1", 6, 1440467},
{"8/8/4k3/8/2p5/8/B2P2K1/8 w - - 0 1", 6, 1015133},
{"r3k2r/1b4bq/8/8/8/8/7B/R3K2R w KQkq - 0 1", 4, 1274206},
{"2K2r2/4P3/8/8/8/8/8/3k4 w - - 0 1", 6, 3821001},
{"8/8/2k5/5q2/5n2/8/5K2/8 b - - 0 1", 4, 23527},
};
bool all_ok = true;
u64 total = 0;
const auto start = std::chrono::steady_clock::now();
for (const Case& c : CASES) {
if (c.depth > max_depth) continue;
Position pos;
pos.set_fen(c.fen);
const u64 n = perft(pos, c.depth);
total += n;
const bool ok = n == c.nodes;
all_ok &= ok;
std::cout << (ok ? "[ OK ] " : "[FAIL] ") << c.fen << " depth " << c.depth << ": " << n;
if (!ok) std::cout << " (expected " << c.nodes << ")";
std::cout << std::endl;
}
const auto ms = std::chrono::duration_cast<std::chrono::milliseconds>(std::chrono::steady_clock::now() - start).count();
std::cout << (all_ok ? "All perft tests passed" : "PERFT FAILED") << " - " << total << " nodes in " << ms
<< " ms (" << (ms ? total * 1000 / ms : total) << " nps)" << std::endl;
return all_ok;
}
} // namespace rigby