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

View File

@@ -1,2 +1,3 @@
pub mod negamax;
pub mod perft;
pub mod quiescence;

View File

@@ -4,6 +4,8 @@ use crate::{
movegen::r#move::Move,
};
use super::quiescence::quiescence;
pub fn negamax(
game: &mut Game,
mut alpha: i32,
@@ -14,7 +16,7 @@ pub fn negamax(
let color = game.current_player();
if depth == 0 {
return (None, evaluate_position(&game.board));
return (None, quiescence(game, alpha, beta).1);
}
let (mut best_move, mut best_score, mate_score) = (None, -100000, -50000 + plies as i32);

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)
}