chessfriend/position/src/perft.rs
Eryn Wells d73630c85e [perft, position] Print moves and nodes counted at first level
At the first level of depth, print the move and the number of nodes counted in
the tree underneath that node. This behavior imitates Stockfish, and helps with
debugging.

Clean up the output of the Perft binary, and update the check-positions script
to compensate.
2025-06-17 16:17:46 -07:00

45 lines
1.1 KiB
Rust

// Eryn Wells <eryn@erynwells.me>
use crate::{GeneratedMove, Position, ValidateMove};
pub trait Perft {
fn perft(&mut self, depth: usize) -> u64;
}
impl Perft for Position {
fn perft(&mut self, depth: usize) -> u64 {
self.perft_recursive(depth, depth)
}
}
impl Position {
fn perft_recursive(&mut self, depth: usize, max_depth: usize) -> u64 {
if depth == 0 {
return 1;
}
let mut total_nodes_counted = 0u64;
let legal_moves: Vec<GeneratedMove> = self.all_legal_moves(None).collect();
for ply in legal_moves {
let ply = ply.ply();
let _has_seen_position = self
.make_move(ply, ValidateMove::No)
.expect("unable to make generated move");
let nodes_counted = self.perft_recursive(depth - 1, depth);
total_nodes_counted += nodes_counted;
self.unmake_last_move().expect("unable to unmake last move");
if depth == max_depth {
println!(" {ply} {nodes_counted}");
}
}
total_nodes_counted
}
}