added (failing) perft, debugging required

This commit is contained in:
Moritz 2025-11-14 13:56:49 +01:00
parent 2aca5ed841
commit 196e5ba265
4 changed files with 45 additions and 78 deletions

View file

@ -1,7 +1,37 @@
use chess_engine::board::Board;
use chess_engine::movegen::generate_pseudo_legal_moves;
use chess_engine::movegen::legal_check::is_king_attacked;
use chess_engine::r#move::MoveList;
fn count_legal_moves_recursive(board: &mut Board, depth: u8) -> u64 {
if depth == 0 {
return 1_u64;
}
let mut list = MoveList::new();
generate_pseudo_legal_moves(&board, &mut list);
let mut leaf_nodes = 0_u64;
for mv in list.iter() {
// Store the undo info when making the move
let undo_info = board.make_move(*mv);
if !is_king_attacked(board) {
leaf_nodes += count_legal_moves_recursive(board, depth - 1);
}
// Undo the move to restore the board state for the next iteration
board.undo_move(undo_info);
}
leaf_nodes
}
#[test]
fn perft() {
let mut board = Board::from_fen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
println!("{}", count_legal_moves_recursive(&mut board, 4));
}
// "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1" 7 3195901860 "false"
// "r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq -" 5 193690690 "false"