Remove redundant PieceType params

This commit is contained in:
stefiosif
2024-10-06 11:32:39 +03:00
parent 3fd3b1ea96
commit 7f6cfb93a2
2 changed files with 15 additions and 42 deletions

View File

@@ -82,11 +82,8 @@ impl Game {
}
MoveType::EnPassant => {
board.move_piece(mv.src, mv.dst, piece_at_src);
let piece_at_capture = mailbox
.piece_at(capture_square)
.expect("Expected piece at: {capture_square}");
board.remove_opponent_piece(capture_square, PieceType::Pawn);
hash.update_en_passant(mv.src, mv.dst, piece_at_src, piece_at_capture, color);
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);
}
@@ -96,36 +93,21 @@ impl Game {
Color::White => Some(mv.src + 8),
Color::Black => Some(mv.src.saturating_sub(8)),
};
hash.update_double_push(
mv.src,
mv.dst,
piece_at_src,
color,
board.state.en_passant_square,
);
hash.update_double_push(mv.src, mv.dst, color, 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, piece_at_src, promote, color);
hash.update_promotion(mv.src, mv.dst, promote, color);
mailbox.set_piece_at(mv.dst, Some(promote.into_piece_type()));
}
MoveType::PromotionCapture(promote) => {
let piece_at_dst = piece_at_dst.expect("Expected piece at dst: {mv.dst}");
board.remove_own_piece(mv.src, piece_at_src);
board.remove_opponent_piece(
mv.dst,
piece_at_dst.expect("Expected piece at dst: {mv.dst}"),
);
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_src,
piece_at_dst.unwrap(),
promote,
color,
);
hash.update_promotion_capture(mv.src, mv.dst, piece_at_dst, promote, color);
mailbox.set_piece_at(mv.dst, Some(promote.into_piece_type()));
}
MoveType::Castle => {

View File

@@ -161,42 +161,33 @@ impl ZobristHash {
src: usize,
dst: usize,
piece_at_src: PieceType,
piece_at_capture: PieceType,
color: Color,
) {
let keys = zobrist_keys();
self.hash ^= keys.piece_square_color[src][piece_at_src.idx()][color.idx()];
self.hash ^= keys.piece_square_color[dst][piece_at_src.idx()][color.idx()];
self.hash ^= keys.piece_square_color[dst][piece_at_capture.idx()][color.opponent().idx()]
self.hash ^= keys.piece_square_color[dst][PieceType::Pawn.idx()][color.opponent().idx()]
}
pub fn update_double_push(
&mut self,
src: usize,
dst: usize,
piece_at_src: PieceType,
color: Color,
new_en_passant_target: Option<usize>,
) {
let keys = zobrist_keys();
self.hash ^= keys.piece_square_color[src][piece_at_src.idx()][color.idx()];
self.hash ^= keys.piece_square_color[dst][piece_at_src.idx()][color.idx()];
self.hash ^= keys.piece_square_color[src][PieceType::Pawn.idx()][color.idx()];
self.hash ^= keys.piece_square_color[dst][PieceType::Pawn.idx()][color.idx()];
if let Some(new_en_passant) = new_en_passant_target {
self.hash ^= keys.en_passant[square::to_file(new_en_passant)];
}
}
pub fn update_promotion(
&mut self,
src: usize,
dst: usize,
piece_at_src: PieceType,
promote: &Promote,
color: Color,
) {
pub fn update_promotion(&mut self, src: usize, dst: usize, promote: &Promote, color: Color) {
let keys = zobrist_keys();
self.hash ^= keys.piece_square_color[src][piece_at_src.idx()][color.idx()];
self.hash ^= keys.piece_square_color[src][PieceType::Pawn.idx()][color.idx()];
self.hash ^= keys.piece_square_color[dst][promote.into_piece_type().idx()][color.idx()];
}
@@ -204,13 +195,12 @@ impl ZobristHash {
&mut self,
src: usize,
dst: usize,
piece_at_src: PieceType,
piece_at_dst: PieceType,
promote: &Promote,
color: Color,
) {
let keys = zobrist_keys();
self.hash ^= keys.piece_square_color[src][piece_at_src.idx()][color.idx()];
self.hash ^= keys.piece_square_color[src][PieceType::Pawn.idx()][color.idx()];
self.hash ^= keys.piece_square_color[dst][piece_at_dst.idx()][color.opponent().idx()];
self.hash ^= keys.piece_square_color[dst][promote.into_piece_type().idx()][color.idx()];
}
@@ -239,10 +229,11 @@ mod tests {
Ok(())
}
//TODO: how to test
// test if an incremental position is the same as if it would be calculated from scratch
#[test]
fn test_update_hash() -> Result<(), String> {
//TODO: how to test
// test if an incremental position is the same as if it would be calculated from scratch
Ok(())
}
}