diff options
-rw-r--r-- | src/grossmeister/search.rs | 95 |
1 files changed, 50 insertions, 45 deletions
diff --git a/src/grossmeister/search.rs b/src/grossmeister/search.rs index f56d97d..4a9e5a2 100644 --- a/src/grossmeister/search.rs +++ b/src/grossmeister/search.rs @@ -67,9 +67,19 @@ impl Grossmeister { } } + let mut transposition = TranspositionTableItem { + hash: self.board.hash, + depth: depth_left, + mov: None, + score: alpha, + node_type: NodeType::All, + }; + // Mate distance pruning let mating_score = Grossmeister::MDP(&mut alpha, &mut beta, root_distance); if mating_score != 0.0 { + transposition.score = mating_score; + self.store_transposition(transposition); return mating_score } @@ -109,13 +119,10 @@ impl Grossmeister { self.board.unmake_move(mov, captured_piece, ep_target_before, castling_rights_before, hash_before); if score >= beta { - self.store_transposition(TranspositionTableItem { - hash: self.board.hash, - mov: Some(mov), - depth: depth_left, - node_type: NodeType::Cut, - score: beta, - }); + transposition.mov = Some(mov); + transposition.score = beta; + transposition.node_type = NodeType::Cut; + self.store_transposition(transposition); if mov.kind == MoveKind::Quiet { self.register_killer(mov); @@ -126,22 +133,13 @@ impl Grossmeister { if score > alpha { alpha = score; should_pv_search = false; // Once we have PV-node we can start zero-window searching - - self.store_transposition(TranspositionTableItem { - hash: self.board.hash, - mov: Some(mov), - depth: depth_left, - node_type: NodeType::PV, - score, - }); - } else { - self.store_transposition(TranspositionTableItem { - hash: self.board.hash, - mov: None, - depth: depth_left, - node_type: NodeType::All, - score: alpha, - }); + transposition.mov = Some(mov); + transposition.score = score; + transposition.node_type = NodeType::PV; + } else if transposition.node_type == NodeType::All { + transposition.mov = None; + transposition.score = alpha; + transposition.node_type = NodeType::All; } } else { self.board.unmake_move(mov, captured_piece, ep_target_before, castling_rights_before, hash_before); @@ -155,12 +153,18 @@ impl Grossmeister { if !legal_move_found { if self.board.is_king_in_check(color) { + transposition.score = -SCORE_MATE + root_distance as Score; + self.store_transposition(transposition); return -SCORE_MATE + root_distance as Score } else { + transposition.score = 0.0; + self.store_transposition(transposition); return 0.0 } } + self.store_transposition(transposition); + alpha } @@ -189,9 +193,18 @@ impl Grossmeister { } } + let mut transposition = TranspositionTableItem { + hash: self.board.hash, + depth: 0, + mov: None, + score: alpha, + node_type: NodeType::All, + }; + // Mate distance pruning let mating_score = Grossmeister::MDP(&mut alpha, &mut beta, root_distance); if mating_score != 0.0 { + transposition.score = mating_score; return mating_score } @@ -226,33 +239,22 @@ impl Grossmeister { self.board.unmake_move(mov, captured_piece, ep_target_before, castling_rights_before, hash_before); if evaluation >= beta { - self.store_transposition(TranspositionTableItem { - hash: self.board.hash, - mov: Some(mov), - depth: 0, - node_type: NodeType::Cut, - score: beta, - }); + transposition.mov = Some(mov); + transposition.score = beta; + transposition.node_type = NodeType::Cut; + self.store_transposition(transposition); return beta; // Fail-hard beta-cutoff } if evaluation > alpha { - self.store_transposition(TranspositionTableItem { - hash: self.board.hash, - mov: Some(mov), - depth: 0, - node_type: NodeType::PV, - score: evaluation, - }); + transposition.mov = Some(mov); + transposition.score = evaluation; + transposition.node_type = NodeType::Cut; alpha = evaluation; - } else { - self.store_transposition(TranspositionTableItem { - hash: self.board.hash, - mov: None, - depth: 0, - node_type: NodeType::All, - score: alpha, - }); + } else if transposition.node_type == NodeType::All { + transposition.mov = None; + transposition.score = alpha; + transposition.node_type = NodeType::All; } } else { self.board.unmake_move(mov, captured_piece, ep_target_before, castling_rights_before, hash_before); @@ -260,9 +262,12 @@ impl Grossmeister { } if !legal_move_found && self.board.is_king_in_check(color) { + transposition.score = -SCORE_MATE + root_distance as Score; + self.store_transposition(transposition); return -SCORE_MATE + root_distance as Score } + self.store_transposition(transposition); alpha } |