Make mvv_lva to map pieces to their material score instead of abritrary matrix

Add time limits, use anyhow crate to improve error handling, remove depth limit on quiescence search
This commit is contained in:
stefiosif
2024-11-13 21:13:05 +02:00
parent 429485ae73
commit c2414c3f6e
11 changed files with 207 additions and 128 deletions

View File

@@ -1,17 +1,19 @@
use crate::{board::game::Game, evaluation::evaluation::evaluate_position, movegen::r#move::Move};
use anyhow::{bail, Result};
use super::move_ordering::score_by_mvv_lva;
use crate::{board::game::Game, evaluation::evaluation::evaluate_position};
pub fn quiescence(game: &mut Game, mut alpha: i32, beta: i32, depth: u8) -> (Option<Move>, i32) {
if depth == 0 {
return (None, evaluate_position(&game.board));
use super::{iterative_deepening::hard_limit, move_ordering::mvv_lva, time::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 color = game.current_player();
let stand_pat = evaluate_position(&game.board);
if stand_pat >= beta {
return (None, beta);
return Ok(beta);
}
if alpha < stand_pat {
@@ -19,7 +21,7 @@ pub fn quiescence(game: &mut Game, mut alpha: i32, beta: i32, depth: u8) -> (Opt
}
let mut captures: Vec<_> = game.board.pseudo_moves_all_captures();
captures.sort_unstable_by_key(|mv| score_by_mvv_lva(&game.mailbox, *mv));
captures.sort_unstable_by_key(|mv| mvv_lva(&game.mailbox, *mv));
for mv in captures {
game.make_move(&mv);
@@ -28,16 +30,17 @@ pub fn quiescence(game: &mut Game, mut alpha: i32, beta: i32, depth: u8) -> (Opt
game.unmake_move();
continue;
}
let move_score = -quiescence(game, -beta, -alpha, depth - 1).1;
let move_score = -quiescence(game, -beta, -alpha, time_info)?;
game.unmake_move();
if move_score >= beta {
return (Some(mv), beta);
return Ok(beta);
}
alpha = alpha.max(move_score);
if alpha > move_score {
alpha = move_score
}
}
(None, alpha)
Ok(alpha)
}