46 lines
1.1 KiB
Rust
46 lines
1.1 KiB
Rust
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()
|
|
.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)
|
|
}
|