aboutsummaryrefslogtreecommitdiff
path: root/src/board/engine.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/board/engine.rs')
-rw-r--r--src/board/engine.rs25
1 files changed, 21 insertions, 4 deletions
diff --git a/src/board/engine.rs b/src/board/engine.rs
index a5f6894..c43a722 100644
--- a/src/board/engine.rs
+++ b/src/board/engine.rs
@@ -250,8 +250,8 @@ impl Board {
let mut ordered_moves: Vec<Move> = moves_with_eval.iter().map(|(m, _)| *m).collect();
- // Insert killer moves after non-losing captures
- let losing_capture_index = match moves_with_eval.iter().position(|(m, eval)| m.is_tactical() && *eval < 0.0) {
+ // Insert killer moves after winning captures
+ let equal_capture_index = match moves_with_eval.iter().position(|(m, eval)| m.is_tactical() && *eval == 0.0) {
Some(x) => x,
None => 0,
};
@@ -260,7 +260,7 @@ impl Board {
match ordered_moves.iter().position(|m| *m == killer) {
Some(index) => {
let mov = ordered_moves.remove(index);
- ordered_moves.insert(losing_capture_index, mov);
+ ordered_moves.insert(equal_capture_index, mov);
}
None => {}
};
@@ -287,6 +287,7 @@ impl Board {
return (self.quiscence(alpha, beta), principal_variation);
}
+ let mut should_pv_search = true;
let mut legal_move_found = false;
for mov in moves {
let ep_target_before = self.ep_target.clone();
@@ -296,7 +297,22 @@ impl Board {
if !self.is_king_in_check(color) {
legal_move_found = true;
- let (mut score, mut subtree_pv) = self.negamax_search(-beta, -alpha, depth_left - 1, &mut killer_moves, deadline);
+
+ let (mut score, mut subtree_pv) = if should_pv_search {
+ // Assume PV-node is high in the list (if move ordering is good)
+ self.negamax_search(-beta, -alpha, depth_left - 1, &mut killer_moves, deadline)
+ } else {
+ // After we have PV-node (that raised alpha) all other nodes will be searched
+ // with zero-window first to confirm this assumption
+ let result = self.negamax_search(-(alpha + 0.0001), -alpha, depth_left - 1, &mut killer_moves, deadline);
+ // In case some of the other nodes raises alpha, then it's true PV node now,
+ // let's research with full window to find its exact value
+ if -result.0 > alpha {
+ self.negamax_search(-beta, -alpha, depth_left - 1, &mut killer_moves, deadline)
+ } else {
+ result
+ }
+ };
score *= -1.;
self.unmake_move(mov, captured_piece, ep_target_before, castling_rights_before, hash_before);
@@ -320,6 +336,7 @@ impl Board {
}
if score > alpha {
alpha = score;
+ should_pv_search = false; // Once we have PV-node we can start zero-window searching
principal_variation = Vec::with_capacity(depth_left as usize);
principal_variation.push(mov);
principal_variation.append(&mut subtree_pv);