Refactor based on clippy, move tt to search module and call iterative deepening from uci_go

This commit is contained in:
stefiosif
2024-11-03 01:19:12 +02:00
parent 802bdcdb37
commit 1b28de5064
11 changed files with 67 additions and 92 deletions

View File

@@ -1,15 +1,10 @@
use crate::{
board::{
game::Game,
transposition_table::{Bound, TTEntry, TranspositionTable},
},
board::game::Game,
evaluation::{MATE_SCORE, MIN_SCORE},
movegen::r#move::Move,
};
use super::{move_ordering::score_by_mvv_lva, quiescence::quiescence};
const QUIESCENCE_DEPTH: u8 = 3;
use super::{move_ordering::score_by_mvv_lva, quiescence::quiescence, transposition_table::{Bound, TTEntry, TranspositionTable}, QUIESCENCE_DEPTH};
pub fn negamax(
game: &mut Game,
@@ -18,15 +13,14 @@ pub fn negamax(
depth: u8,
plies: u8,
tt: &mut TranspositionTable,
nodes: &mut u64,
) -> (Option<Move>, i32) {
*nodes += 1;
if depth == 0 {
let q = quiescence(game, alpha, beta, nodes, QUIESCENCE_DEPTH).1;
let q = quiescence(game, alpha, beta, QUIESCENCE_DEPTH).1;
return (None, q);
}
let mut tt_move = None;
if let Some(entry) = tt.lookup(game.hash) {
if entry.depth >= depth
&& (matches!(entry.bound, Bound::Exact)
@@ -35,6 +29,7 @@ pub fn negamax(
{
return (entry.mv, entry.score);
}
tt_move = entry.mv;
}
let color = game.current_player();
@@ -43,6 +38,13 @@ pub fn negamax(
let mut pseudo_legal_moves = game.board.pseudo_moves_all();
pseudo_legal_moves.sort_unstable_by_key(|mv| score_by_mvv_lva(&game.mailbox, *mv));
if let Some(tt_move) = tt_move {
if let Some(tt_move_index) = pseudo_legal_moves.iter().position(|&mv| mv == tt_move) {
pseudo_legal_moves.remove(tt_move_index);
pseudo_legal_moves.insert(0, tt_move);
}
}
for mv in pseudo_legal_moves {
game.make_move(&mv);
@@ -52,7 +54,7 @@ pub fn negamax(
}
legal_moves += 1;
let move_score = -negamax(game, -beta, -alpha, depth - 1, plies + 1, tt, nodes).1;
let move_score = -negamax(game, -beta, -alpha, depth - 1, plies + 1, tt).1;
game.unmake_move();
if move_score > best_score {
@@ -65,7 +67,7 @@ pub fn negamax(
game.hash,
depth,
best_score,
None,
best_move,
Bound::Lower,
));
return (best_move, beta);
@@ -80,12 +82,12 @@ pub fn negamax(
game.hash,
depth,
mate_score,
None,
best_move,
Bound::Exact,
));
return (None, mate_score);
}
tt.insert(TTEntry::new(game.hash, depth, 0, None, Bound::Exact));
tt.insert(TTEntry::new(game.hash, depth, 0, best_move, Bound::Exact));
return (None, 0);
}
@@ -104,12 +106,13 @@ pub fn negamax(
#[cfg(test)]
mod tests {
use crate::board::transposition_table::TranspositionTable;
use crate::board::{fen::from_fen, square::Square};
use crate::evaluation::{MAX_SCORE, MIN_SCORE};
use crate::movegen::attack_generator::init_attacks;
use crate::movegen::r#move::Move;
use crate::search::negamax::negamax;
use crate::search::transposition_table::TranspositionTable;
use crate::search::MAX_TT_SIZE;
const FEN_MATE_IN_1: [&str; 2] = [
"8/8/8/8/8/4q1k1/8/5K2 b - - 0 1",
@@ -121,13 +124,11 @@ mod tests {
init_attacks();
let mut game = from_fen(FEN_MATE_IN_1[0])?;
let mut tt = TranspositionTable::new(1000000);
let mut tt = TranspositionTable::new(MAX_TT_SIZE);
let e3f2 = Move::new(Square::E3, Square::F2);
let mut nodes = 0;
let anointed_move = negamax(&mut game, MIN_SCORE, MAX_SCORE, 2, 0, &mut tt, &mut nodes)
let anointed_move = negamax(&mut game, MIN_SCORE, MAX_SCORE, 2, 0, &mut tt)
.0
.unwrap();
dbg!(nodes);
assert_eq!(e3f2, anointed_move);