Remove tt cutoffs, fix capture move sorting, refactor
This commit is contained in:
@@ -6,10 +6,9 @@ use crate::{
|
||||
};
|
||||
|
||||
use super::{
|
||||
iterative_deepening::hard_limit,
|
||||
move_ordering::mvv_lva,
|
||||
move_ordering,
|
||||
quiescence::quiescence,
|
||||
time::TimeInfo,
|
||||
time::{hard_limit, TimeInfo},
|
||||
transposition_table::{Bound, TTEntry, TranspositionTable},
|
||||
SearchResult,
|
||||
};
|
||||
@@ -32,33 +31,18 @@ pub fn negamax(
|
||||
return Ok(SearchResult::new(None, q_score));
|
||||
}
|
||||
|
||||
let mut tt_move = None;
|
||||
|
||||
if let Some(entry) = tt.lookup(game.hash) {
|
||||
if entry.depth >= depth
|
||||
&& (matches!(entry.bound, Bound::Exact)
|
||||
|| (matches!(entry.bound, Bound::Lower) && entry.score >= beta)
|
||||
|| (matches!(entry.bound, Bound::Upper) && entry.score <= alpha))
|
||||
{
|
||||
return Ok(SearchResult::new(entry.mv, entry.score));
|
||||
}
|
||||
tt_move = entry.mv;
|
||||
}
|
||||
|
||||
let color = game.current_player();
|
||||
let (mut best_move, mut best_score, mate_score) = (None, MIN_SCORE, -MATE_SCORE + plies as i32);
|
||||
let mut best_move = None;
|
||||
let mut best_score = MIN_SCORE;
|
||||
let mate_score = -MATE_SCORE + plies as i32;
|
||||
let mut legal_moves = 0;
|
||||
let mut pseudo_legal_moves = game.board.pseudo_moves_all();
|
||||
pseudo_legal_moves.sort_unstable_by_key(|mv| mvv_lva(&game.mailbox, *mv));
|
||||
let mut bound = Bound::Alpha;
|
||||
let all_moves = game.board.pseudo_moves_all();
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
let tt_move = tt.lookup(game.hash).and_then(|entry| entry.mv);
|
||||
let moves = move_ordering::sort_moves(all_moves, &game.mailbox, tt_move);
|
||||
|
||||
for mv in pseudo_legal_moves {
|
||||
for mv in moves {
|
||||
game.make_move(&mv);
|
||||
|
||||
if game.board.king_under_check(color) {
|
||||
@@ -67,62 +51,35 @@ pub fn negamax(
|
||||
}
|
||||
|
||||
legal_moves += 1;
|
||||
let move_score =
|
||||
-negamax(game, -beta, -alpha, depth - 1, plies + 1, time_info, tt)?.best_score;
|
||||
let score = -negamax(game, -beta, -alpha, depth - 1, plies + 1, time_info, tt)?.best_score;
|
||||
game.unmake_move();
|
||||
|
||||
if move_score > best_score {
|
||||
best_score = move_score;
|
||||
if score > best_score {
|
||||
best_score = score;
|
||||
best_move = Some(mv);
|
||||
}
|
||||
|
||||
if move_score >= beta {
|
||||
tt.insert(TTEntry::new(
|
||||
game.hash,
|
||||
depth,
|
||||
best_score,
|
||||
best_move,
|
||||
Bound::Lower,
|
||||
));
|
||||
return Ok(SearchResult::new(best_move, beta));
|
||||
if score >= beta {
|
||||
bound = Bound::Beta;
|
||||
best_score = beta;
|
||||
best_move = Some(mv);
|
||||
break;
|
||||
}
|
||||
|
||||
alpha = alpha.max(move_score);
|
||||
if score > alpha {
|
||||
bound = Bound::Exact;
|
||||
alpha = score;
|
||||
}
|
||||
}
|
||||
|
||||
if legal_moves == 0 {
|
||||
if game.board.king_under_check(color) {
|
||||
tt.insert(TTEntry::new(
|
||||
game.hash,
|
||||
depth,
|
||||
mate_score,
|
||||
best_move,
|
||||
Bound::Exact,
|
||||
));
|
||||
return Ok(SearchResult::new(None, mate_score));
|
||||
}
|
||||
tt.insert(TTEntry::new(game.hash, depth, 0, best_move, Bound::Exact));
|
||||
return Ok(SearchResult::new(None, 0));
|
||||
}
|
||||
|
||||
if best_score < alpha {
|
||||
tt.insert(TTEntry::new(
|
||||
game.hash,
|
||||
depth,
|
||||
best_score,
|
||||
best_move,
|
||||
Bound::Upper,
|
||||
));
|
||||
} else {
|
||||
tt.insert(TTEntry::new(
|
||||
game.hash,
|
||||
depth,
|
||||
best_score,
|
||||
best_move,
|
||||
Bound::Exact,
|
||||
));
|
||||
}
|
||||
|
||||
tt.insert(TTEntry::new(game.hash, depth, best_score, best_move, bound));
|
||||
Ok(SearchResult::new(best_move, best_score))
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user