Skip to content

Commit

Permalink
Move perft into the crate, and test it
Browse files Browse the repository at this point in the history
  • Loading branch information
kraktus committed Aug 18, 2022
1 parent 051b38a commit 81ed2da
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 41 deletions.
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ mod unmove;
pub use crate::unmove::{MoveKind, UnMove, UnMoveList};

mod retroboard;
pub use crate::retroboard::RetroBoard;
pub use crate::retroboard::{perft, RetroBoard};

mod retropocket;
pub use crate::retropocket::{ParseRetroPocketError, RetroPocket, RetroPockets};
Expand Down
37 changes: 1 addition & 36 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,42 +1,7 @@
use std::time::Instant;

use retroboard::shakmaty::{fen::Fen, perft as shakmaty_perft, CastlingMode, Chess};
use retroboard::RetroBoard;
/// From shakmaty code source
/// Counts legal move paths of a given length.
///
/// Shorter paths (due to mate or stalemate) are not counted.
/// Computing perft numbers is useful for comparing, testing and
/// debugging move generation correctness and performance.
///
/// The method used here is simply recursively enumerating the entire tree of
/// legal moves. While this is fine for testing there is much
/// faster specialized software.
///
/// Warning: Computing perft numbers can take a long time, even at moderate
/// depths. The simple recursive algorithm can also overflow the stack at
/// high depths, but this will only come into consideration in the rare case
/// that high depths are feasible at all.
fn perft(r: &RetroBoard, depth: u32) -> u64 {
if depth < 1 {
1
} else {
let moves = r.legal_unmoves();

if depth == 1 {
moves.len() as u64
} else {
moves
.iter()
.map(|m| {
let mut child = r.clone();
child.push(m);
perft(&child, depth - 1)
})
.sum()
}
}
}
use retroboard::{perft, RetroBoard};

fn _shakmaty(fen: &str) {
let pos: Chess = fen
Expand Down
44 changes: 40 additions & 4 deletions src/retroboard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -567,6 +567,42 @@ fn closest_further_square(bb: Bitboard, of: Square) -> (Square, Square) {
}
}

/// From shakmaty code source
/// Counts legal move paths of a given length.
///
/// Shorter paths (due to mate or stalemate) are not counted.
/// Computing perft numbers is useful for comparing, testing and
/// debugging move generation correctness and performance.
///
/// The method used here is simply recursively enumerating the entire tree of
/// legal moves. While this is fine for testing there is much
/// faster specialized software.
///
/// Warning: Computing perft numbers can take a long time, even at moderate
/// depths. The simple recursive algorithm can also overflow the stack at
/// high depths, but this will only come into consideration in the rare case
/// that high depths are feasible at all.
pub fn perft(r: &RetroBoard, depth: u32) -> u64 {
if depth < 1 {
1
} else {
let moves = r.legal_unmoves();

if depth == 1 {
moves.len() as u64
} else {
moves
.iter()
.map(|m| {
let mut child = r.clone();
child.push(m);
perft(&child, depth - 1)
})
.sum()
}
}
}

#[cfg(test)]
mod tests {
// use pretty_assertions::{assert_eq, assert_ne};
Expand Down Expand Up @@ -1050,6 +1086,7 @@ mod tests {
}

// does not take into account internal positions, contrary to `test_final_unmoves`
// As it names says it is behavior identical to `perft` but with additional debug built-in in case it fails
fn perft_debug(r: RetroBoard, depth: u32) -> Option<u64> {
if depth < 1 {
Some(1)
Expand Down Expand Up @@ -1097,10 +1134,9 @@ mod tests {
} else {
RetroBoard::new(fen, white_p, black_p).expect("Valid retroboard")
};
assert_eq!(perft_debug(r.clone(), 0), Some(1));
assert_eq!(perft_debug(r.clone(), 1), Some(24));
assert_eq!(perft_debug(r.clone(), 2), Some(3951));
// assert_eq!(perft_debug(r, 3), Some(640054))
assert_eq!(perft_debug(r.clone(), 0), Some(perft(&r, 0)));
assert_eq!(perft_debug(r.clone(), 1), Some(perft(&r, 1)));
assert_eq!(perft_debug(r.clone(), 2), Some(perft(&r, 2)));
}
}
}

0 comments on commit 81ed2da

Please sign in to comment.