51 lines
1.1 KiB
Rust
51 lines
1.1 KiB
Rust
use anyhow::{bail, Result};
|
|
|
|
use crate::{board::game::Game, evaluation::evaluation::evaluate_position};
|
|
|
|
use super::{
|
|
move_ordering,
|
|
time::{hard_limit, TimeInfo},
|
|
};
|
|
|
|
pub fn quiescence(game: &mut Game, mut alpha: i32, beta: i32, time_info: &TimeInfo) -> Result<i32> {
|
|
if hard_limit(&time_info.time, time_info.remaining_time_in_ms) {
|
|
bail!("Time is up! In Quiescence");
|
|
}
|
|
|
|
let stand_pat = evaluate_position(&game.board);
|
|
|
|
if stand_pat >= beta {
|
|
return Ok(beta);
|
|
}
|
|
|
|
if stand_pat > alpha {
|
|
alpha = stand_pat;
|
|
}
|
|
|
|
let color = game.current_player();
|
|
let all_moves = game.board.pseudo_moves_all_captures();
|
|
let moves = move_ordering::sort_moves(all_moves, &game.mailbox, None);
|
|
|
|
for mv in moves {
|
|
game.make_move(&mv);
|
|
|
|
if game.board.king_under_check(color) {
|
|
game.unmake_move();
|
|
continue;
|
|
}
|
|
|
|
let score = -quiescence(game, -beta, -alpha, time_info)?;
|
|
game.unmake_move();
|
|
|
|
if score >= beta {
|
|
return Ok(beta);
|
|
}
|
|
|
|
if score > alpha {
|
|
alpha = score
|
|
}
|
|
}
|
|
|
|
Ok(alpha)
|
|
}
|