From fa6f6c3f5f8f872a216ccdb593492775c0d265a1 Mon Sep 17 00:00:00 2001 From: eug-vs Date: Tue, 14 Feb 2023 17:45:10 +0300 Subject: feat: consider checks in quiscence --- src/board/engine.rs | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) (limited to 'src') diff --git a/src/board/engine.rs b/src/board/engine.rs index 46cf8ed..1516784 100644 --- a/src/board/engine.rs +++ b/src/board/engine.rs @@ -472,26 +472,36 @@ impl Board { let mut moves = self.generate_pseudolegal_moves(color); moves = self.order_moves(moves, Vec::new()); - let stand_pat = self.evaluate(); + if !self.is_king_in_check(color) { + // If we are not in check, we can evaluate stand pat + let stand_pat = self.evaluate(); - if stand_pat >= beta { - return beta; - } - if alpha < stand_pat { - alpha = stand_pat; - } + if stand_pat >= beta { + return beta; + } + if alpha < stand_pat { + alpha = stand_pat; + } - let tactical_moves = moves.iter().filter(|m| m.is_tactical()); + // If we are not in check, we can only search tactical moves + moves = moves + .iter() + .filter(|m| m.is_tactical()) + .map(|m| *m) + .collect() + } - for mov in tactical_moves { + let mut legal_move_found = false; + for mov in moves { let ep_target_before = self.ep_target.clone(); let castling_rights_before = self.castling_rights.clone(); let hash_before = self.hash.clone(); - let captured_piece = self.make_move(*mov); + let captured_piece = self.make_move(mov); if !self.is_king_in_check(color) { + legal_move_found = true; let evaluation = -self.quiscence(-beta, -alpha); - self.unmake_move(*mov, captured_piece, ep_target_before, castling_rights_before, hash_before); + self.unmake_move(mov, captured_piece, ep_target_before, castling_rights_before, hash_before); if evaluation >= beta { return beta; // Fail-hard beta-cutoff @@ -500,7 +510,13 @@ impl Board { alpha = evaluation; } } else { - self.unmake_move(*mov, captured_piece, ep_target_before, castling_rights_before, hash_before); + self.unmake_move(mov, captured_piece, ep_target_before, castling_rights_before, hash_before); + } + } + + if !legal_move_found { + if self.is_king_in_check(color) { + return -VALUE_WIN } } -- cgit v1.2.3