9-add-perft (#17)
All checks were successful
Python tests (make) / test (push) Successful in 11s

Reviewed-on: #17
Co-authored-by: Josh <josh@joshuaschuett.com>
Co-committed-by: Josh <josh@joshuaschuett.com>
This commit is contained in:
2025-08-19 22:33:27 +00:00
committed by Josh
parent 15b1a03928
commit febbbe784a
3 changed files with 114 additions and 2 deletions

View File

@@ -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',