From febbbe784a4e62cef6f454437c19e797b551a567 Mon Sep 17 00:00:00 2001 From: Josh Date: Tue, 19 Aug 2025 22:33:27 +0000 Subject: [PATCH] 9-add-perft (#17) Reviewed-on: https://git.joshuaschuett.com/projects/chess/pulls/17 Co-authored-by: Josh Co-committed-by: Josh --- engine/src/bitboard.c | 24 +++++++++++- test/chess_ffi.py | 2 + test/test_perft.py | 90 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 114 insertions(+), 2 deletions(-) create mode 100644 test/test_perft.py diff --git a/engine/src/bitboard.c b/engine/src/bitboard.c index d1e5c9f..bc7ba41 100644 --- a/engine/src/bitboard.c +++ b/engine/src/bitboard.c @@ -604,8 +604,8 @@ void rebuild_occ(struct Board *board) { uint64_t white=0; uint64_t black=0; - for (int p = P; p <= K; ++p) white |= board->pieces[p]; - for (int p = p; p <= k; ++p) black |= board->pieces[p]; + for (int i = P; i <= K; ++i) white |= board->pieces[i]; + for (int i = p; i <= k; ++i) black |= board->pieces[i]; board->occ[WHITE] = white; board->occ[BLACK] = black; @@ -785,6 +785,26 @@ int get_legal_moves(struct Board *board, struct Move *out) { return count; } +uint64_t perft(struct Board *board, int depth) { + if (depth == 0) return 1; + + struct Move moves[256]; + int n = get_legal_moves(board, moves); + + if (depth == 1) return (uint64_t) n; + + uint64_t nodes = 0; + for (int i = 0; i < n; ++i) { + struct Board after; + if (!apply_move_on_copy(board, &after, moves[i])) { + // Shouldn't happen with legal moves, but be defensive + continue; + } + nodes += perft(&after, depth - 1); + } + return nodes; +} + void print_board(const struct Board *board) { const char PIECE_CH[12] = { 'P','N','B','R','Q','K', diff --git a/test/chess_ffi.py b/test/chess_ffi.py index 7ad92ca..57b0ea5 100644 --- a/test/chess_ffi.py +++ b/test/chess_ffi.py @@ -98,6 +98,8 @@ in_check = _bind_opt("in_check", INCHECK_ARGS, C.c_bool) attackers_to = _bind_opt("attackers_to", ATTACKERS_TO, C.c_uint64) get_legal_moves = _bind_opt("get_legal_moves", GEN_LEGAL_MOVES, C.c_int) +PERFT_SIG = (C.POINTER(Board), C.c_int) +perft = _bind_opt("perft", PERFT_SIG, C.c_uint64) # Attack cache tables. KnightArr = (C.c_uint64 * 64) diff --git a/test/test_perft.py b/test/test_perft.py new file mode 100644 index 0000000..55aa2e5 --- /dev/null +++ b/test/test_perft.py @@ -0,0 +1,90 @@ +from test.base import ChessLibTestBase +from test.chess_ffi import Board +from test.chess_ffi import perft + + +class TestPerftQuick(ChessLibTestBase): + def test_perft_1(self): + b = Board() + self.load_fen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", board=b) + node_count = { + 1: 20, + 2: 400, + 3: 8902, + 4: 197281, + 5: 4865609, + } + for node, count in node_count.items(): + actual = int(perft(b, node)) + self.assertEqual(count, actual, f"perft({node}) start pos") + + + def test_perft_2(self): + b = Board() + self.load_fen("r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1", board=b) + node_count = { + 1: 48, + 2: 2039, + 3: 97862, + 4: 4085603, + } + for node, count in node_count.items(): + actual = int(perft(b, node)) + self.assertEqual(count, actual, f"perft({node}) start pos") + + + def test_perft_3(self): + b = Board() + self.load_fen("8/2p5/3p4/KP5r/1R3p1k/8/4P1P1/8 w - - 0 1", board=b) + node_count = { + 1: 14, + 2: 191, + 3: 2812, + 4: 43238, + 5: 674624, + } + for node, count in node_count.items(): + actual = int(perft(b, node)) + self.assertEqual(count, actual, f"perft({node}) start pos") + + + def test_perft_4(self): + b = Board() + self.load_fen("r3k2r/Pppp1ppp/1b3nbN/nP6/BBP1P3/q4N2/Pp1P2PP/R2Q1RK1 w kq - 0 1", board=b) + node_count = { + 1: 6, + 2: 264, + 3: 9467, + 4: 422333, + } + for node, count in node_count.items(): + actual = int(perft(b, node)) + self.assertEqual(count, actual, f"perft({node}) start pos") + + + def test_perft_5(self): + b = Board() + self.load_fen("rnbq1k1r/pp1Pbppp/2p5/8/2B5/8/PPP1NnPP/RNBQK2R w KQ - 1 8 ", board=b) + node_count = { + 1: 44, + 2: 1486, + 3: 62379, + 4: 2103487, + } + for node, count in node_count.items(): + actual = int(perft(b, node)) + self.assertEqual(count, actual, f"perft({node}) start pos") + + + def test_perft_6(self): + b = Board() + self.load_fen("r4rk1/1pp1qppp/p1np1n2/2b1p1B1/2B1P1b1/P1NP1N2/1PP1QPPP/R4RK1 w - - 0 10 ", board=b) + node_count = { + 1: 46, + 2: 2079, + 3: 89890, + 4: 3894594, + } + for node, count in node_count.items(): + actual = int(perft(b, node)) + self.assertEqual(count, actual, f"perft({node}) start pos") \ No newline at end of file