diff options
author | eug-vs <eugene@eug-vs.xyz> | 2023-02-21 14:04:22 +0300 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2023-02-21 14:04:35 +0300 |
commit | 5e9543dcc6a012aef73d342080bfec46690b5446 (patch) | |
tree | 430450db80ebbee4ae2f8a6f54d5bd62c8eb8771 /src | |
parent | 0d5c2eb1666d3038fa87895cc98eadc673c54a3a (diff) | |
download | chessnost-5e9543dcc6a012aef73d342080bfec46690b5446.tar.gz |
refactor: use Zobirst methods in make_move
Diffstat (limited to 'src')
-rw-r--r-- | src/board/mod.rs | 50 | ||||
-rw-r--r-- | src/board/zobrist.rs | 5 |
2 files changed, 26 insertions, 29 deletions
diff --git a/src/board/mod.rs b/src/board/mod.rs index 854ce0e..5317cc2 100644 --- a/src/board/mod.rs +++ b/src/board/mod.rs @@ -1,6 +1,6 @@ use crate::{bitboard::{Bitboard, BitboardFns}, moves::{Move, MoveKind}, attacks::Attacks, square::Square, board::io::IO}; -use self::{ttable::{TranspositionTable, TTABLE_SIZE}, zobrist::ZobristSeed}; +use self::{ttable::{TranspositionTable, TTABLE_SIZE}, zobrist::{ZobristSeed, Zobrist}}; pub mod io; mod zobrist; mod engine; @@ -405,7 +405,7 @@ impl Board { let mut captured_piece = match self.piece_by_square(mov.target) { Some(target_piece) => { self.piece_sets[target_piece as usize] ^= move_target_bb; - self.hash ^= self.zobrist_seed[(target_piece as usize) * 64 + mov.target as usize]; + self.zobrist_toggle_piece(target_piece, mov.target); Some(target_piece) }, None => None, @@ -419,8 +419,8 @@ impl Board { Some(pawn_type) => { let captured_bb = captured_square.to_bitboard(); self.piece_sets[pawn_type as usize] ^= captured_bb; - self.occupancy ^= captured_bb; - self.hash ^= self.zobrist_seed[pawn_type as usize* 64 + captured_square as usize]; + self.occupancy ^= captured_bb; + self.zobrist_toggle_piece(pawn_type, captured_square); Some(pawn_type) }, None => panic!("Pawn captured by En Passant was not found"), @@ -435,21 +435,21 @@ impl Board { MoveKind::Promotion(promotion_piece) => { let promo_id = promotion_piece as usize; self.piece_sets[source_id] ^= move_source_bb; - self.occupancy ^= move_source_bb; - self.hash ^= self.zobrist_seed[source_id * 64 + mov.source as usize]; + self.occupancy ^= move_source_bb; + self.zobrist_toggle_piece(source_piece, mov.source); self.piece_sets[promo_id] |= move_target_bb; - self.occupancy |= move_target_bb; - self.hash ^= self.zobrist_seed[promo_id * 64 + mov.target as usize]; + self.occupancy |= move_target_bb; + self.zobrist_toggle_piece(promotion_piece, mov.target); }, _ => { self.piece_sets[source_id] ^= move_source_bb; - self.occupancy ^= move_source_bb; - self.hash ^= self.zobrist_seed[source_id * 64 + mov.source as usize]; + self.occupancy ^= move_source_bb; + self.zobrist_toggle_piece(source_piece, mov.source); self.piece_sets[source_id] |= move_target_bb; - self.occupancy |= move_target_bb; - self.hash ^= self.zobrist_seed[source_id * 64 + mov.target as usize]; + self.occupancy |= move_target_bb; + self.zobrist_toggle_piece(source_piece, mov.target); } } PieceType::from(source_piece) @@ -479,11 +479,11 @@ impl Board { let rook_id = rook_type as usize; self.piece_sets[rook_id] ^= rook_source_bb; self.occupancy ^= rook_source_bb; - self.hash ^= self.zobrist_seed[rook_id * 64 + rook_source_square as usize]; + self.zobrist_toggle_piece(rook_type, rook_source_square); self.piece_sets[rook_id] |= rook_target_bb; self.occupancy |= rook_target_bb; - self.hash ^= self.zobrist_seed[rook_id * 64 + rook_target_square as usize]; + self.zobrist_toggle_piece(rook_type, rook_target_square); }, None => panic!("Rook was not found when castling"), } @@ -491,7 +491,7 @@ impl Board { // Double push should set En Passant target square self.ep_target = if mov.kind == MoveKind::DoublePush { - self.hash ^= self.zobrist_seed[64 * 12 + 4 + mov.source.file() as usize]; + self.zobrist_toggle_ep_square(mov.source); match mov.source.rank() { 1 => Some(mov.source.nort_one()), 6 => Some(mov.source.sout_one()), @@ -500,23 +500,23 @@ impl Board { } else { None }; // Withdraw castling rights when moving rooks or king - let source_color = Color::from_piece(source_piece) as usize; + let source_color = Color::from_piece(source_piece); match source_piece.without_color() { PieceType::King => { - self.castling_rights[source_color][CastlingSide::King as usize] = false; - self.castling_rights[source_color][CastlingSide::Queen as usize] = false; - self.hash ^= self.zobrist_seed[64 * 12 + source_color * 2 + 0]; - self.hash ^= self.zobrist_seed[64 * 12 + source_color * 2 + 1]; + self.castling_rights[source_color as usize][CastlingSide::King as usize] = false; + self.castling_rights[source_color as usize][CastlingSide::Queen as usize] = false; + self.zobrist_toggle_castling_right(source_color, CastlingSide::King); + self.zobrist_toggle_castling_right(source_color, CastlingSide::Queen); }, PieceType::Rook => { match mov.source.file() { 0 => { - self.castling_rights[source_color][CastlingSide::Queen as usize] = false; - self.hash ^= self.zobrist_seed[64 * 12 + source_color * 2 + CastlingSide::Queen as usize]; + self.castling_rights[source_color as usize][CastlingSide::Queen as usize] = false; + self.zobrist_toggle_castling_right(source_color, CastlingSide::Queen); } 7 => { - self.castling_rights[source_color][CastlingSide::King as usize] = false; - self.hash ^= self.zobrist_seed[64 * 12 + source_color * 2 + CastlingSide::King as usize]; + self.castling_rights[source_color as usize][CastlingSide::King as usize] = false; + self.zobrist_toggle_castling_right(source_color, CastlingSide::King); } _ => {}, } @@ -525,7 +525,7 @@ impl Board { } self.ply += 1; - self.hash ^= self.zobrist_seed[780]; + self.zobrist_toggle_color(); captured_piece } diff --git a/src/board/zobrist.rs b/src/board/zobrist.rs index e282074..8ea0038 100644 --- a/src/board/zobrist.rs +++ b/src/board/zobrist.rs @@ -71,9 +71,6 @@ impl Zobrist for Board { } fn zobrist_toggle_color(&mut self) -> () { - self.hash ^= match self.zobrist_seed.last() { - Some(x) => x, - None => panic!("Something is wrong with zobrist seed list"), - }; + self.hash ^= self.zobrist_seed[ZOBRIST_SEED_SIZE - 1]; } } |