aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
Diffstat (limited to 'src')
-rw-r--r--src/board/engine.rs36
1 files changed, 36 insertions, 0 deletions
diff --git a/src/board/engine.rs b/src/board/engine.rs
index ff071ea..dcb3646 100644
--- a/src/board/engine.rs
+++ b/src/board/engine.rs
@@ -133,6 +133,42 @@ impl Board {
(alpha, principal_variation)
}
+ pub fn quiscence(&mut self, mut alpha: f32, beta: f32) -> f32 {
+ let color = self.color();
+ let stand_pat = self.material_advantage();
+
+ if stand_pat >= beta {
+ return beta;
+ }
+ if alpha < stand_pat {
+ alpha = stand_pat;
+ }
+
+ let moves = self.generate_pseudolegal_moves(color);
+ let tactical_moves = moves.iter().filter(|m| m.kind == MoveKind::Capture || m.kind == MoveKind::EnPassant);
+
+ for mov in tactical_moves {
+ let ep_target_before = self.ep_target.clone();
+ let castling_rights_before = self.castling_rights.clone();
+ let captured_piece = self.make_move(*mov);
+
+ if !self.is_king_in_check(color) {
+ let evaluation = -self.quiscence(-beta, -alpha);
+ self.unmake_move(*mov, captured_piece, ep_target_before, castling_rights_before);
+
+ if evaluation >= beta {
+ return beta; // Fail-hard beta-cutoff
+ }
+ if evaluation > alpha {
+ alpha = evaluation;
+ }
+ } else {
+ self.unmake_move(*mov, captured_piece, ep_target_before, castling_rights_before);
+ }
+ }
+
+ alpha
+ }
}