Add tests for Zobrist hashing

This commit is contained in:
stefiosif
2024-10-09 20:54:12 +03:00
parent 7f6cfb93a2
commit c40e8478c8
2 changed files with 154 additions and 36 deletions

View File

@@ -57,7 +57,7 @@ impl Game {
let color = board.state.current_player();
let pawn_move = board.is_pawn_move(mv.src);
let mut en_passant_square = None;
let capture_square = match color {
let capture_en_passant = match color {
Color::White => mv.dst - 8,
Color::Black => mv.dst + 8,
};
@@ -71,6 +71,7 @@ impl Game {
MoveType::Quiet => {
board.move_piece(mv.src, mv.dst, piece_at_src);
hash.update_quiet(mv.src, mv.dst, piece_at_src, color);
hash.drop_en_passant_hash(board.state.en_passant_square());
mailbox.set_piece_at(mv.dst, Some(piece_at_src));
}
MoveType::Capture => {
@@ -78,14 +79,16 @@ impl Game {
board.move_piece(mv.src, mv.dst, piece_at_src);
board.remove_opponent_piece(mv.dst, piece_at_dst);
hash.update_capture(mv.src, mv.dst, piece_at_src, piece_at_dst, color);
hash.drop_en_passant_hash(board.state.en_passant_square());
mailbox.set_piece_at(mv.dst, Some(piece_at_src));
}
MoveType::EnPassant => {
board.move_piece(mv.src, mv.dst, piece_at_src);
board.remove_opponent_piece(capture_square, PieceType::Pawn);
hash.update_en_passant(mv.src, mv.dst, piece_at_src, color);
mailbox.set_piece_at(mv.dst, Some(piece_at_src));
mailbox.set_piece_at(capture_square, None);
board.move_piece(mv.src, mv.dst, PieceType::Pawn);
board.remove_opponent_piece(capture_en_passant, PieceType::Pawn);
hash.update_en_passant(mv.src, mv.dst, capture_en_passant, color);
hash.drop_en_passant_hash(board.state.en_passant_square());
mailbox.set_piece_at(mv.dst, Some(PieceType::Pawn));
mailbox.set_piece_at(capture_en_passant, None);
}
MoveType::DoublePush => {
board.move_piece(mv.src, mv.dst, piece_at_src);
@@ -93,13 +96,15 @@ impl Game {
Color::White => Some(mv.src + 8),
Color::Black => Some(mv.src.saturating_sub(8)),
};
hash.update_double_push(mv.src, mv.dst, color, board.state.en_passant_square);
hash.update_double_push(mv.src, mv.dst, color, en_passant_square);
hash.drop_en_passant_hash(board.state.en_passant_square());
mailbox.set_piece_at(mv.dst, Some(piece_at_src));
}
MoveType::Promotion(promote) => {
board.remove_own_piece(mv.src, piece_at_src);
board.promote_piece(mv.dst, promote);
hash.update_promotion(mv.src, mv.dst, promote, color);
hash.drop_en_passant_hash(board.state.en_passant_square());
mailbox.set_piece_at(mv.dst, Some(promote.into_piece_type()));
}
MoveType::PromotionCapture(promote) => {
@@ -108,6 +113,7 @@ impl Game {
board.remove_opponent_piece(mv.dst, piece_at_dst);
board.promote_piece(mv.dst, promote);
hash.update_promotion_capture(mv.src, mv.dst, piece_at_dst, promote, color);
hash.drop_en_passant_hash(board.state.en_passant_square());
mailbox.set_piece_at(mv.dst, Some(promote.into_piece_type()));
}
MoveType::Castle => {
@@ -120,6 +126,7 @@ impl Game {
board.move_piece(rook_src, rook_dst, PieceType::Rook);
board.state.set_castling_ability(color, Castle::None);
hash.update_castle(mv.src, mv.dst, piece_at_src, rook_src, rook_dst, color);
hash.drop_en_passant_hash(board.state.en_passant_square());
mailbox.set_piece_at(mv.dst, Some(piece_at_src));
mailbox.set_piece_at(rook_src, None);
mailbox.set_piece_at(rook_dst, Some(PieceType::Rook));
@@ -132,11 +139,6 @@ impl Game {
.update_game_state(mv, color, pawn_move, en_passant_square);
hash.update_side_to_move_key();
if let Some(old_en_passant) = board.state.en_passant_square {
hash.update_en_passant_keys(old_en_passant);
}
hash.update_castling_ability_keys(old_castling_ability, board.state.castling_ability);
}
@@ -252,8 +254,8 @@ mod tests {
#[test]
fn test_make_move_quiet() -> Result<(), String> {
let mut game = from_fen(FEN)?;
let b1c3 = Move::new(Square::F3, Square::E3);
game.make_move(&b1c3);
let f3e3 = Move::new(Square::F3, Square::E3);
game.make_move(&f3e3);
assert_eq!(game, from_fen(FEN_QUIET)?);