diff options
author | eug-vs <eugene@eug-vs.xyz> | 2023-01-25 00:09:26 +0300 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2023-01-25 00:09:26 +0300 |
commit | 518e08fea967f45e94e3ca65fcd299c4b90e81e9 (patch) | |
tree | 923148026d0a1f9212d2de61121e8a5e20b2886e | |
parent | f5b2d63f2f1131c23d26360c98d7db44d9efb71d (diff) | |
download | chessnost-518e08fea967f45e94e3ca65fcd299c4b90e81e9.tar.gz |
feat: implement quiscence
-rw-r--r-- | src/board/engine.rs | 36 |
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 + } } |