From dc120dc65742be2312a53afc887dd30a3ae96e04 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Sun, 29 Jan 2023 13:56:57 +0300 Subject: feat: immediately return score of hash moves --- src/board/engine.rs | 32 ++++++++++++++++++++++++-------- src/board/ttable.rs | 2 +- 2 files changed, 25 insertions(+), 9 deletions(-) (limited to 'src') diff --git a/src/board/engine.rs b/src/board/engine.rs index c43a722..da0f38b 100644 --- a/src/board/engine.rs +++ b/src/board/engine.rs @@ -217,11 +217,12 @@ impl Board { 0.0 } - pub fn hash_move(&self) -> Option { + /// Find current transposition in Transposition Table + pub fn transposition(&self) -> Option { match self.transposition_table[(self.hash % TTABLE_SIZE) as usize] { Some(item) => { if item.hash == self.hash { - return Some(item.best_move) + return Some(item) } None } @@ -267,8 +268,8 @@ impl Board { } - match self.hash_move() { - Some(mov) => ordered_moves.insert(0, mov), + match self.transposition() { + Some(transposition) => ordered_moves.insert(0, transposition.mov), None => {}, } @@ -280,6 +281,21 @@ impl Board { let mut killer_moves = Vec::new(); let color = self.color(); + match self.transposition() { + Some(transposition) => { + if transposition.depth >= depth_left { + match transposition.node_type { + NodeType::PV => { // PV-nodes have exact score + principal_variation.push(transposition.mov); + return (transposition.score, principal_variation); + } + _ => {} // TODO: prune when out ouf bounds + } + } + } + None => {}, + } + let mut moves = self.generate_pseudolegal_moves(color); moves = self.order_moves(moves, parent_killers.to_vec()); @@ -319,7 +335,7 @@ impl Board { if score >= beta { self.transposition_table[(self.hash % TTABLE_SIZE) as usize] = Some(TranspositionTableItem { hash: self.hash, - best_move: mov, + mov, depth: depth_left, // TODO: should be actual depth searched node_type: NodeType::Cut, score, @@ -343,7 +359,7 @@ impl Board { self.transposition_table[(self.hash % TTABLE_SIZE) as usize] = Some(TranspositionTableItem { hash: self.hash, - best_move: mov, + mov, depth: depth_left, // TODO: should be actual depth searched node_type: NodeType::PV, score, @@ -351,7 +367,7 @@ impl Board { } else { self.transposition_table[(self.hash % TTABLE_SIZE) as usize] = Some(TranspositionTableItem { hash: self.hash, - best_move: mov, + mov, depth: depth_left, // TODO: should be actual depth searched node_type: NodeType::All, score, @@ -363,7 +379,7 @@ impl Board { // Could not finish in time, return what we have so far if Instant::now() > deadline { - return (alpha, principal_variation) + break; } } diff --git a/src/board/ttable.rs b/src/board/ttable.rs index 3a103da..9f9e0b0 100644 --- a/src/board/ttable.rs +++ b/src/board/ttable.rs @@ -15,7 +15,7 @@ pub enum NodeType { pub struct TranspositionTableItem { /// Zobrist hash of this position pub hash: u64, - pub best_move: Move, + pub mov: Move, pub depth: u8, pub score: f32, pub node_type: NodeType, -- cgit v1.2.3