[position, perft] Move Perft into the position crate
Move the Perft trait into the position crate, and let the perft binary call into that. Amend Position::make_move to return a bool in the Ok case that indicates whether the position has been seen before. Use this to decide whether to continue recursing during the Perft run. I haven't seen that this makes a difference in the counts returned by Perft yet.
This commit is contained in:
parent
f0b6cb5f08
commit
7744dd06f0
4 changed files with 82 additions and 39 deletions
|
@ -1,4 +1,4 @@
|
|||
use chessfriend_position::{fen::FromFenStr, GeneratedMove, Position, ValidateMove};
|
||||
use chessfriend_position::{fen::FromFenStr, perft::Perft, Position};
|
||||
use clap::Parser;
|
||||
|
||||
#[derive(Parser, Debug)]
|
||||
|
@ -10,38 +10,11 @@ struct Arguments {
|
|||
fen: Option<String>,
|
||||
}
|
||||
|
||||
trait Perft {
|
||||
fn perft(&mut self, depth: usize) -> u64;
|
||||
}
|
||||
|
||||
impl Perft for Position {
|
||||
fn perft(&mut self, depth: usize) -> u64 {
|
||||
if depth == 0 {
|
||||
return 1;
|
||||
}
|
||||
|
||||
let mut nodes_counted: u64 = 0;
|
||||
|
||||
let legal_moves: Vec<GeneratedMove> = self.all_legal_moves(None).collect();
|
||||
|
||||
for generated_ply in legal_moves {
|
||||
self.make_move(generated_ply.into(), ValidateMove::No)
|
||||
.expect("unable to make generated move");
|
||||
|
||||
nodes_counted += self.perft(depth - 1);
|
||||
|
||||
self.unmake_last_move().expect("unable to unmake last move");
|
||||
}
|
||||
|
||||
nodes_counted
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> anyhow::Result<()> {
|
||||
let args = Arguments::parse();
|
||||
let depth = args.depth;
|
||||
|
||||
println!("Searching to depth {depth}");
|
||||
println!("depth={depth}");
|
||||
|
||||
let mut position = if let Some(fen) = args.fen {
|
||||
Position::from_fen_str(&fen)?
|
||||
|
@ -51,7 +24,7 @@ fn main() -> anyhow::Result<()> {
|
|||
|
||||
let nodes_searched = position.perft(depth);
|
||||
|
||||
println!("Nodes searched: {nodes_searched}");
|
||||
println!("nodes={nodes_searched}");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue