diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/board/engine.rs | 57 |
1 files changed, 28 insertions, 29 deletions
diff --git a/src/board/engine.rs b/src/board/engine.rs index 6a0db61..a5f6894 100644 --- a/src/board/engine.rs +++ b/src/board/engine.rs @@ -229,7 +229,7 @@ impl Board { } } - pub fn order_moves(&self, moves: Vec<Move>) -> Vec<Move> { + pub fn order_moves(&self, moves: Vec<Move>, killer_moves: Vec<Move>) -> Vec<Move> { let mut moves_with_eval: Vec<(Move, f32)> = moves .iter() .map(|m| (*m, self.eval_move(*m))) @@ -248,36 +248,40 @@ impl Board { a_eval.total_cmp(b_eval).reverse() }); - moves_with_eval.iter_mut().map(|(m, _)| *m).collect() - } + let mut ordered_moves: Vec<Move> = moves_with_eval.iter().map(|(m, _)| *m).collect(); - 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(); + // 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) { + Some(x) => x, + None => 0, + }; - let mut moves = self.generate_pseudolegal_moves(color); - moves = self.order_moves(moves); + for killer in killer_moves { + match ordered_moves.iter().position(|m| *m == killer) { + Some(index) => { + let mov = ordered_moves.remove(index); + ordered_moves.insert(losing_capture_index, mov); + } + None => {} + }; + + } match self.hash_move() { - Some(mov) => moves.insert(0, mov), + Some(mov) => ordered_moves.insert(0, mov), None => {}, } - let loosing_capture_index = match moves.iter().position(|m| { - m.is_tactical() && self.eval_move(*m) < 0.0 - }) { - Some(x) => x, - None => 0, - }; + ordered_moves + } - // Insert killer moves (from previous siblings) 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); - } - } + 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); + moves = self.order_moves(moves, parent_killers.to_vec()); if depth_left == 0 { return (self.quiscence(alpha, beta), principal_variation); @@ -358,12 +362,7 @@ impl Board { pub fn quiscence(&mut self, mut alpha: f32, beta: f32) -> f32 { let color = self.color(); let mut moves = self.generate_pseudolegal_moves(color); - moves = self.order_moves(moves); - - match self.hash_move() { - Some(mov) => moves.insert(0, mov), - None => {}, - } + moves = self.order_moves(moves, Vec::new()); let stand_pat = self.evaluate(Some(moves.len() as f32)); |