Add Null-move pruning

This commit is contained in:
stefiosif
2025-01-29 23:09:01 +02:00
parent 91ad3de8b1
commit 888b3866b9
7 changed files with 125 additions and 12 deletions

View File

@@ -249,6 +249,41 @@ impl Game {
}
}
pub fn make_null_move(&mut self) {
let move_params = MoveParameters::build_null(self);
self.history.push_move_parameters(move_params);
self.hash.update_side_to_move_key();
self.board
.state
.update_null_game_state(self.current_player());
}
pub fn unmake_null_move(&mut self) {
let board = &mut self.board;
let move_parameters = &mut self
.history
.pop_move_parameters()
.expect("History stack is empty");
let color_before_move = board.state.change_side();
board.state.revert_full_move(color_before_move);
board.state.en_passant_square = move_parameters.en_passant_square;
if let Some(hash) = move_parameters.zobrist_hash {
self.hash = hash;
}
if let Some(new_castling_ability) = move_parameters.castling_ability {
board.state.castling_ability = new_castling_ability;
}
if let Some(new_halfmove_clock) = move_parameters.halfmove_clock {
board.state.halfmove_clock = new_halfmove_clock;
}
}
pub fn in_repetition(&self) -> bool {
if self.board.state.halfmove_clock < 4 {
return false;
@@ -462,4 +497,22 @@ mod tests {
Ok(())
}
const FEN_W: &str = "rnbqkbnr/pppppp1p/8/5Pp1/8/8/PPPPP1PP/RNBQKBNR w KQkq g6 0 1";
const FEN_B: &str = "rnbqkbnr/pppppp1p/8/5Pp1/8/8/PPPPP1PP/RNBQKBNR b KQkq - 1 1";
#[test]
fn test_make_and_unmake_null_move() -> Result<(), String> {
let mut game = from_fen(FEN_W)?;
game.make_null_move();
assert_eq!(game, from_fen(FEN_B)?);
game.unmake_null_move();
assert!(game == from_fen(FEN_W)?);
Ok(())
}
}