summaryrefslogtreecommitdiff
path: root/engine/src/eval.rs
diff options
context:
space:
mode:
authorMicha White <botahamec@outlook.com>2023-10-10 16:34:35 -0400
committerMicha White <botahamec@outlook.com>2023-10-10 16:34:35 -0400
commite459e742fd2bb364211d38dde14f1d594d70445e (patch)
treedfe9ae101ccb9d5a65e1435f09d41c9897a84352 /engine/src/eval.rs
parent63f5fafd7cbbf0e0386ef419b126f4de5b238f4d (diff)
Implement a lazy selection sort for move ordering
Diffstat (limited to 'engine/src/eval.rs')
-rw-r--r--engine/src/eval.rs20
1 files changed, 7 insertions, 13 deletions
diff --git a/engine/src/eval.rs b/engine/src/eval.rs
index b0b52df..5754343 100644
--- a/engine/src/eval.rs
+++ b/engine/src/eval.rs
@@ -6,7 +6,7 @@ use std::{
use model::{CheckersBitBoard, Move, PieceColor, PossibleMoves};
-use crate::transposition_table::TranspositionTableRef;
+use crate::{lazysort::LazySort, transposition_table::TranspositionTableRef};
const KING_WORTH: u32 = 2;
@@ -182,17 +182,12 @@ fn eval_jumps(
unsafe fn sort_moves(
a: &Move,
- b: &Move,
board: CheckersBitBoard,
table: TranspositionTableRef,
-) -> std::cmp::Ordering {
- let a_entry = table
+) -> Evaluation {
+ table
.get_any_depth(a.apply_to(board))
- .unwrap_or(Evaluation::DRAW);
- let b_entry = table
- .get_any_depth(b.apply_to(board))
- .unwrap_or(Evaluation::DRAW);
- a_entry.cmp(&b_entry)
+ .unwrap_or(Evaluation::DRAW)
}
pub fn negamax(
@@ -215,15 +210,14 @@ pub fn negamax(
let turn = board.turn();
let mut best_eval = Evaluation::LOSS;
- let mut moves: Vec<Move> = PossibleMoves::moves(board).into_iter().collect();
+ let moves: Box<[Move]> = PossibleMoves::moves(board).into_iter().collect();
if moves.is_empty() {
return Evaluation::LOSS;
}
- moves.sort_unstable_by(|a, b| unsafe { sort_moves(a, b, board, table) });
-
- for current_move in moves {
+ let sorter = LazySort::new(moves, |m| unsafe { sort_moves(m, board, table) });
+ for current_move in sorter.into_iter() {
let board = unsafe { current_move.apply_to(board) };
let current_eval = if board.turn() == turn {
negamax(depth - 1, alpha, beta, board, table).increment()