[explorer, perft, position] Move node count into a new PerftCounters struct
This commit is contained in:
parent
4ce7e89cdb
commit
0f5a538f0a
3 changed files with 47 additions and 19 deletions
|
@ -350,9 +350,9 @@ fn do_perft_command(
|
||||||
.ok_or(CommandHandlingError::MissingArgument("depth"))?;
|
.ok_or(CommandHandlingError::MissingArgument("depth"))?;
|
||||||
|
|
||||||
let mut position = state.position.clone();
|
let mut position = state.position.clone();
|
||||||
let nodes_count = position.perft(depth);
|
let counters = position.perft(depth);
|
||||||
|
|
||||||
println!("nodes {nodes_count}");
|
println!("{counters}");
|
||||||
|
|
||||||
Ok(CommandResult {
|
Ok(CommandResult {
|
||||||
should_continue: true,
|
should_continue: true,
|
||||||
|
|
|
@ -7,6 +7,7 @@ use clap::Parser;
|
||||||
#[derive(Parser, Debug)]
|
#[derive(Parser, Debug)]
|
||||||
#[command(name = "Perft")]
|
#[command(name = "Perft")]
|
||||||
struct Arguments {
|
struct Arguments {
|
||||||
|
#[arg(long, short, value_name = "INT")]
|
||||||
depth: usize,
|
depth: usize,
|
||||||
|
|
||||||
#[arg(long, short, value_name = "FEN")]
|
#[arg(long, short, value_name = "FEN")]
|
||||||
|
@ -26,9 +27,9 @@ fn main() -> anyhow::Result<()> {
|
||||||
println!("fen \"{}\"", position.to_fen_str().unwrap());
|
println!("fen \"{}\"", position.to_fen_str().unwrap());
|
||||||
println!("depth {depth}");
|
println!("depth {depth}");
|
||||||
|
|
||||||
let nodes_searched = position.perft(depth);
|
let counters = position.perft(depth);
|
||||||
|
|
||||||
println!("nodes {nodes_searched}");
|
println!("\n{counters}");
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,20 +1,29 @@
|
||||||
// Eryn Wells <eryn@erynwells.me>
|
// Eryn Wells <eryn@erynwells.me>
|
||||||
|
|
||||||
|
use chessfriend_moves::Move;
|
||||||
|
|
||||||
use crate::{GeneratedMove, Position, ValidateMove};
|
use crate::{GeneratedMove, Position, ValidateMove};
|
||||||
|
use std::fmt;
|
||||||
|
|
||||||
|
#[derive(Clone, Debug, Default, Eq, PartialEq)]
|
||||||
|
pub struct PerftCounters {
|
||||||
|
nodes: u64,
|
||||||
|
}
|
||||||
|
|
||||||
impl Position {
|
impl Position {
|
||||||
pub fn perft(&mut self, depth: usize) -> u64 {
|
pub fn perft(&mut self, depth: usize) -> PerftCounters {
|
||||||
self.perft_recursive(depth, depth)
|
self.perft_recursive(0, depth)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Position {
|
impl Position {
|
||||||
fn perft_recursive(&mut self, depth: usize, max_depth: usize) -> u64 {
|
fn perft_recursive(&mut self, depth: usize, max_depth: usize) -> PerftCounters {
|
||||||
if depth == 0 {
|
let mut counters = PerftCounters::default();
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
||||||
let mut total_nodes_counted = 0u64;
|
if depth == max_depth {
|
||||||
|
counters.count_node();
|
||||||
|
return counters;
|
||||||
|
}
|
||||||
|
|
||||||
let legal_moves: Vec<GeneratedMove> = self.all_legal_moves(None).collect();
|
let legal_moves: Vec<GeneratedMove> = self.all_legal_moves(None).collect();
|
||||||
|
|
||||||
|
@ -25,21 +34,39 @@ impl Position {
|
||||||
.make_move(ply, ValidateMove::No)
|
.make_move(ply, ValidateMove::No)
|
||||||
.expect("unable to make generated move");
|
.expect("unable to make generated move");
|
||||||
|
|
||||||
let nodes_counted = if has_seen_position {
|
let recursive_counters = if has_seen_position {
|
||||||
1
|
let mut counters = PerftCounters::default();
|
||||||
|
counters.count_node();
|
||||||
|
counters
|
||||||
} else {
|
} else {
|
||||||
self.perft_recursive(depth - 1, max_depth)
|
self.perft_recursive(depth + 1, max_depth)
|
||||||
};
|
};
|
||||||
|
|
||||||
total_nodes_counted += nodes_counted;
|
|
||||||
|
|
||||||
self.unmake_last_move().expect("unable to unmake last move");
|
self.unmake_last_move().expect("unable to unmake last move");
|
||||||
|
|
||||||
if depth == max_depth {
|
counters.fold(&recursive_counters);
|
||||||
println!(" {ply} {nodes_counted}");
|
|
||||||
|
if depth == 0 {
|
||||||
|
println!(" {ply}: {}", recursive_counters.nodes);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
total_nodes_counted
|
counters
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PerftCounters {
|
||||||
|
fn count_node(&mut self) {
|
||||||
|
self.nodes += 1;
|
||||||
|
}
|
||||||
|
fn fold(&mut self, results: &Self) {
|
||||||
|
self.nodes += results.nodes;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl fmt::Display for PerftCounters {
|
||||||
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
|
writeln!(f, "Perft Results")?;
|
||||||
|
write!(f, " Nodes: {}", self.nodes)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue