aboutsummaryrefslogtreecommitdiff
path: root/src/board/mod.rs
diff options
context:
space:
mode:
authoreug-vs <eugene@eug-vs.xyz>2023-01-25 12:19:22 +0300
committereug-vs <eugene@eug-vs.xyz>2023-01-25 12:19:22 +0300
commitdff89e96347638fc45e655704a3a377bdeed3cd4 (patch)
tree9fbbf3ecedc149bb7cc559afbdf193ec8280ed80 /src/board/mod.rs
parent1265ee0ad113ec48865c1d44fd08bdbf53685bcc (diff)
downloadchessnost-dff89e96347638fc45e655704a3a377bdeed3cd4.tar.gz
fix!: correct hash increment in make_move
Diffstat (limited to 'src/board/mod.rs')
-rw-r--r--src/board/mod.rs22
1 files changed, 13 insertions, 9 deletions
diff --git a/src/board/mod.rs b/src/board/mod.rs
index 84d77fb..8dbf476 100644
--- a/src/board/mod.rs
+++ b/src/board/mod.rs
@@ -503,7 +503,7 @@ impl Board {
let mut captured_piece = match self.piece_by_square(mov.target) {
Some(target_piece) => {
self.pieces[target_piece as usize] ^= move_target_bb;
- self.hash ^= self.zobrist_seed[(target_piece as usize) * 12 + mov.target as usize];
+ self.hash ^= self.zobrist_seed[(target_piece as usize) * 64 + mov.target as usize];
Some(target_piece)
},
None => None,
@@ -516,7 +516,7 @@ impl Board {
captured_piece = match self.piece_by_square(captured_square) {
Some(pawn_type) => {
self.pieces[pawn_type as usize] ^= captured_square.to_bitboard();
- self.hash ^= self.zobrist_seed[pawn_type as usize* 12 + captured_square as usize];
+ self.hash ^= self.zobrist_seed[pawn_type as usize* 64 + captured_square as usize];
Some(pawn_type)
},
None => panic!("Pawn captured by En Passant was not found"),
@@ -529,11 +529,11 @@ impl Board {
let source_id = source_piece as usize;
self.pieces[source_id] ^= move_source_bb;
self.occupancy ^= move_source_bb;
- self.hash ^= self.zobrist_seed[source_id * 12 + mov.source as usize];
+ self.hash ^= self.zobrist_seed[source_id * 64 + mov.source as usize];
self.pieces[source_id] |= move_target_bb;
self.occupancy |= move_target_bb;
- self.hash ^= self.zobrist_seed[source_id * 12 + mov.target as usize];
+ self.hash ^= self.zobrist_seed[source_id * 64 + mov.target as usize];
PieceType::from(source_piece)
},
None => {
@@ -561,11 +561,11 @@ impl Board {
let rook_id = rook_type as usize;
self.pieces[rook_id] ^= rook_source_bb;
self.occupancy ^= rook_source_bb;
- self.hash ^= self.zobrist_seed[rook_id * 12 + rook_source_square as usize];
+ self.hash ^= self.zobrist_seed[rook_id * 64 + rook_source_square as usize];
self.pieces[rook_id] |= rook_target_bb;
self.occupancy |= rook_target_bb;
- self.hash ^= self.zobrist_seed[rook_id * 12 + rook_target_square as usize];
+ self.hash ^= self.zobrist_seed[rook_id * 64 + rook_target_square as usize];
},
None => panic!("Rook was not found when castling"),
}
@@ -838,13 +838,17 @@ mod tests {
let black_move = Move { source: Square::F7, target: Square::F5, kind: MoveKind::Quiet };
println!("\n{:?}", black_move);
- match board.make_move(black_move).unwrap() {
+ match board.make_move(black_move) {
Some(..) => panic!("No piece should be captured"),
None => {},
};
board.print();
+ let hash = board.hash;
+ board.update_zobrist_hash();
+ assert_eq!(hash, board.hash, "Hash should be correctly updated after move");
+
assert!(board.pieces[PieceType::PawnBlack as usize] & Square::F7.to_bitboard() == 0);
assert!(board.pieces[PieceType::PawnBlack as usize] & Square::F5.to_bitboard() > 0);
assert!(board.ply == 1);
@@ -852,7 +856,7 @@ mod tests {
let white_move = Move { source: Square::D2, target: Square::A5, kind: MoveKind::Capture };
println!("\n{:?}", white_move);
- match board.make_move(white_move).unwrap() {
+ match board.make_move(white_move) {
Some(captured) => assert!(captured == PieceType::PawnBlack),
None => panic!("A piece should be captured"),
};
@@ -876,7 +880,7 @@ mod tests {
board.print();
- let captured_piece = board.make_move(mov).unwrap();
+ let captured_piece = board.make_move(mov);
board.print();
board.unmake_move(mov, captured_piece, None, board.castling_rights, initial_board.hash);