-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
288 additions
and
348 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
use crate::Position; | ||
|
||
/// perft is a function to walk the move generation tree of strictly legal moves | ||
/// to count all the leaf nodes of a certain depth. | ||
/// | ||
/// If `SPLIT` is set to `true`, the perft value contributed by each legal move | ||
/// in the current position is displayed separately. If `BULK` is set to `true`, | ||
/// a trick known as bulk-counting is used, which makes it significantly faster. | ||
/// | ||
/// In perft, nodes are only counted at the end after the last make-move. Thus | ||
/// "higher" terminal nodes (e.g. mate or stalemate) are not counted, instead | ||
/// the number of move paths of a certain depth. Perft ignores draws by | ||
/// repetition, by the fifty-move rule and by insufficient material. | ||
pub fn perft<const SPLIT: bool, const BULK: bool>(position: Position, depth: u8) -> u64 { | ||
// Bulk counting if enabled. Instead of calling make move and perft for each | ||
// move at depth 1, just return the number of legal moves, which is equivalent. | ||
if BULK && depth == 1 { | ||
return position.count_moves() as u64; | ||
} | ||
|
||
// At depth 0, perft is defined to be 1. | ||
if depth == 0 { | ||
return 1; | ||
} | ||
|
||
let mut nodes: u64 = 0; | ||
let movelist = position.generate_moves(); | ||
|
||
for m in movelist.iter() { | ||
let new_position = position.after_move(m); | ||
|
||
// Spilt should always be disabled for child perft calls, and a child perft | ||
// should have the same bulk counting behavior as the parent perft call. | ||
let new_nodes = perft::<false, BULK>(new_position, depth - 1); | ||
|
||
// If spilt perft is enabled, print the nodes added due to this move. | ||
if SPLIT { | ||
println!("{}: {}", m, new_nodes); | ||
} | ||
|
||
// Add the new node count to the cumulative total. | ||
nodes += new_nodes; | ||
} | ||
|
||
nodes | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.