aboutsummaryrefslogtreecommitdiff
path: root/src/board/engine.rs
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2023-01-27 21:47:48 +0300
committereug-vs <eugene@eug-vs.xyz>2023-01-27 21:47:48 +0300
commit9749c21b06f20bcaf983140970d881a0f43c6297 (patch)
treee38358b2b92daf72c198c4e36fa2e76d952097d0 /src/board/engine.rs
parent345781ddc28b3f5987beb5b21acda6e532e0a37a (diff)
downloadchessnost-9749c21b06f20bcaf983140970d881a0f43c6297.tar.gz
tmp: killer moves
Diffstat (limited to 'src/board/engine.rs')
-rw-r--r--src/board/engine.rs33
1 files changed, 30 insertions, 3 deletions
diff --git a/src/board/engine.rs b/src/board/engine.rs
index 05cb1e7..8852c1f 100644
--- a/src/board/engine.rs
+++ b/src/board/engine.rs
@@ -204,13 +204,29 @@ impl Board {
});
}
- pub fn negamax_search(&mut self, mut alpha: f32, beta: f32, depth_left: u8, deadline: Instant) -> (f32, Vec<Move>) {
+ pub fn negamax_search(&mut self, mut alpha: f32, beta: f32, depth_left: u8, parent_killers: &mut Vec<Move>, deadline: Instant) -> (f32, Vec<Move>) {
let mut principal_variation = Vec::new();
+ let mut killer_moves = Vec::new();
let color = self.color();
let mut moves = self.generate_pseudolegal_moves(color);
self.order_moves(&mut moves);
+ let loosing_capture_index = match moves.iter().position(|m| {
+ m.is_tactical() && self.eval_move(*m) < 0.0
+ }) {
+ Some(x) => x,
+ None => 0,
+ };
+
+ // Insert killer moves after winning and equal captures
+ for mov in &mut *parent_killers {
+ // Validate that killer piece still exists
+ if mov.source.to_bitboard() & self.color_occupancy(color) > 0 {
+ moves.insert(loosing_capture_index, *mov);
+ }
+ }
+
match self.transposition_table[(self.hash % TTABLE_SIZE) as usize] {
Some(item) => {
if item.hash == self.hash {
@@ -231,7 +247,7 @@ impl Board {
let captured_piece = self.make_move(mov);
if !self.is_king_in_check(color) {
- let (mut score, mut subtree_pv) = self.negamax_search(-beta, -alpha, depth_left - 1, deadline);
+ let (mut score, mut subtree_pv) = self.negamax_search(-beta, -alpha, depth_left - 1, &mut killer_moves, deadline);
score *= -1.;
self.unmake_move(mov, captured_piece, ep_target_before, castling_rights_before, hash_before);
@@ -244,6 +260,15 @@ impl Board {
score,
});
+ if mov.kind == MoveKind::Quiet {
+ // println!("Killer {:?} found at depth {}", mov, depth_left);
+ // self.print();
+ match parent_killers.iter().find(|m| **m == mov) {
+ None => parent_killers.push(mov),
+ Some(..) => {},
+ }
+ }
+
return (beta, principal_variation);
}
if score > alpha {
@@ -338,10 +363,12 @@ impl Board {
let mut depth = 1;
let mut alpha = -INFINITY;
let mut beta = INFINITY;
+ let mut root_killers: Vec<Move> = Vec::new();
let window_size = 0.25;
+
while depth <= max_depth {
- let search_result = self.negamax_search(alpha, beta, depth, deadline);
+ let search_result = self.negamax_search(alpha, beta, depth, &mut root_killers, deadline);
println!("Finished depth({}) {:?} [{:?} left]", depth, search_result, deadline - Instant::now());
if Instant::now() > deadline {