9-add-perft #17
@@ -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',
|
||||
|
||||
@@ -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)
|
||||
|
||||
33
test/test_perft.py
Normal file
33
test/test_perft.py
Normal file
@@ -0,0 +1,33 @@
|
||||
from test.base import ChessLibTestBase
|
||||
from test.chess_ffi import Board, perft
|
||||
|
||||
|
||||
class TestPerft(ChessLibTestBase):
|
||||
def test_start_position(self):
|
||||
b = Board()
|
||||
self.load_fen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1", board=b)
|
||||
expected = {
|
||||
1: 20,
|
||||
2: 400,
|
||||
3: 8902,
|
||||
#4: 197281,
|
||||
# 5: 4865609,
|
||||
# 6: 119060324
|
||||
}
|
||||
for d, want in expected.items():
|
||||
got = int(perft(b, d))
|
||||
self.assertEqual(got, want, f"perft({d}) start pos")
|
||||
|
||||
"""
|
||||
def test_kiwipete(self):
|
||||
b = Board()
|
||||
self.load_fen("r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1", board=b)
|
||||
expected = {
|
||||
1: 48,
|
||||
2: 2039,
|
||||
3: 97862
|
||||
}
|
||||
for d, want in expected.items():
|
||||
got = int(perft(b, d))
|
||||
self.assertEqual(got, want, f"perft({d}) kiwipete")
|
||||
"""
|
||||
Reference in New Issue
Block a user