Implement quiescence search for leaf nodes

This commit is contained in:
stefiosif
2024-09-04 18:56:20 +03:00
parent e817229d12
commit fec27b0d02
3 changed files with 49 additions and 1 deletions

45
src/search/quiescence.rs Normal file
View File

@@ -0,0 +1,45 @@
use crate::{
board::{game::Game, history::MoveParameters},
evaluation::evaluation::evaluate_position,
movegen::r#move::{Move, MoveType},
};
pub fn quiescence(game: &mut Game, mut alpha: i32, beta: i32) -> (Option<Move>, i32) {
let color = game.current_player();
let stand_pat = evaluate_position(&game.board);
if stand_pat >= beta {
return (None, beta);
}
if alpha < stand_pat {
alpha = stand_pat;
}
let captures = game
.board
.pseudo_moves_all(color)
.into_iter()
.filter(|m| !matches!(m.move_type, MoveType::Quiet));
for mv in captures {
let move_parameters = MoveParameters::build(&game.board, &mv);
game.board.make_move(&mv);
if game.board.king_under_check(color) {
game.board.unmake_move(move_parameters);
continue;
}
let move_score = -quiescence(game, -beta, -alpha).1;
game.board.unmake_move(move_parameters);
if move_score >= beta {
return (Some(mv), beta);
}
alpha = alpha.max(move_score);
}
(None, 0)
}