Use TT to store best move after each ID iteration

This commit is contained in:
stefiosif
2025-01-18 17:44:27 +02:00
parent 824f8a37b5
commit 1c7100f510
4 changed files with 19 additions and 51 deletions

View File

@@ -10,7 +10,6 @@ use super::{
quiescence::quiescence,
time::{hard_limit, TimeInfo},
transposition_table::TTEntry,
SearchResult,
};
pub fn negamax(
@@ -20,14 +19,14 @@ pub fn negamax(
depth: u8,
plies: u8,
time_info: &TimeInfo,
) -> Result<SearchResult> {
) -> Result<i32> {
if hard_limit(&time_info.time, time_info.remaining_time_in_ms) {
bail!("Time is up! In Negamax");
}
if depth == 0 {
let q_score = quiescence(game, alpha, beta, time_info).map_err(|e| anyhow!("{e}"))?;
return Ok(SearchResult::new(None, q_score));
return Ok(q_score);
}
let color = game.current_player();
@@ -49,7 +48,7 @@ pub fn negamax(
}
legal_moves += 1;
let score = -negamax(game, -beta, -alpha, depth - 1, plies + 1, time_info)?.best_score;
let score = -negamax(game, -beta, -alpha, depth - 1, plies + 1, time_info)?;
game.unmake_move();
if score > best_score {
@@ -70,13 +69,13 @@ pub fn negamax(
if legal_moves == 0 {
if game.board.king_under_check(color) {
return Ok(SearchResult::new(None, mate_score));
return Ok(mate_score);
}
return Ok(SearchResult::new(None, 0));
return Ok(0);
}
game.tt.insert(TTEntry::new(game.hash, best_move));
Ok(SearchResult::new(best_move, best_score))
Ok(best_score)
}
#[cfg(test)]
@@ -100,22 +99,18 @@ mod tests {
let e3f2 = Move::new(Square::E3, Square::F2);
let time_info = TimeInfo::new(std::time::Instant::now(), 1000);
let anointed_move = negamax(&mut game, MIN_SCORE, MAX_SCORE, 2, 0, &time_info)
.expect("Expected a search result")
.best_move
.expect("Expected a move");
negamax(&mut game, MIN_SCORE, MAX_SCORE, 2, 0, &time_info)
.expect("Expected a search result");
assert_eq!(e3f2, anointed_move);
assert_eq!(e3f2, game.tt.lookup(game.hash).unwrap().mv.unwrap());
let mut game = from_fen(FEN_MATE_IN_1[1]).unwrap();
let e3f2 = Move::new(Square::E3, Square::F2);
let anointed_move = negamax(&mut game, MIN_SCORE, MAX_SCORE, 2, 0, &time_info)
.expect("Expected a search result")
.best_move
.expect("Expected a move");
negamax(&mut game, MIN_SCORE, MAX_SCORE, 2, 0, &time_info)
.expect("Expected a search result");
assert_eq!(e3f2, anointed_move);
assert_eq!(e3f2, game.tt.lookup(game.hash).unwrap().mv.unwrap());
Ok(())
}