chessfriend/position/src/perft.rs

46 lines
1.1 KiB
Rust
Raw Normal View History

// 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
}
}