From 83d13da85bf089e74ebc7c1996e65e05eeabcd69 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Wed, 25 Jan 2023 08:12:52 +0300 Subject: feat: enable quiscence in negamax --- src/board/engine.rs | 34 ++++++++++++++++++++++------------ 1 file changed, 22 insertions(+), 12 deletions(-) (limited to 'src/board/engine.rs') diff --git a/src/board/engine.rs b/src/board/engine.rs index 75a505e..c6ac512 100644 --- a/src/board/engine.rs +++ b/src/board/engine.rs @@ -122,8 +122,6 @@ impl Board { } pub fn negamax_search(&mut self, mut alpha: f32, beta: f32, depth_left: u8) -> (f32, Vec) { - let transposition = self.transposition_table[(self.hash % TTABLE_SIZE) as usize]; - let mut principal_variation = Vec::new(); let color = self.color(); @@ -135,20 +133,17 @@ impl Board { a_eval.total_cmp(&b_eval) }); - if transposition.is_some() { - match transposition { - Some(item) => { - if item.node_type == NodeType::PV { - // println!("Cache hit at depth {}: {:?}", depth_left, item); - moves.insert(0, item.best_move); - } + match self.transposition_table[(self.hash % TTABLE_SIZE) as usize] { + Some(item) => { + if item.node_type == NodeType::PV { + moves.insert(0, item.best_move); } - None => panic!("Unreachable"), } + None => {}, } if depth_left == 0 { - return (self.evaluate(Some(moves.len() as f32)), principal_variation); + return (self.quiscence(alpha, beta), principal_variation); } for mov in moves { @@ -190,7 +185,22 @@ impl Board { pub fn quiscence(&mut self, mut alpha: f32, beta: f32) -> f32 { let color = self.color(); - let moves = self.generate_pseudolegal_moves(color); + let mut moves = self.generate_pseudolegal_moves(color); + + moves.sort_unstable_by(|a, b| { + let a_eval = self.eval_move(*a); + let b_eval = self.eval_move(*b); + a_eval.total_cmp(&b_eval) + }); + + match self.transposition_table[(self.hash % TTABLE_SIZE) as usize] { + Some(item) => { + if item.node_type == NodeType::PV { + moves.insert(0, item.best_move); + } + } + None => {}, + } let stand_pat = self.evaluate(Some(moves.len() as f32)); -- cgit v1.2.3