#include "perft.h" #include #include 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 << m.to_uci() << ": " << n << "\n"; } const auto ms = std::chrono::duration_cast(std::chrono::steady_clock::now() - start).count(); std::cout << "\nNodes: " << total << "\nTime: " << ms << " ms\nNPS: " << (ms ? total * 1000 / ms : total) << std::endl; } 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::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