diff options
author | eug-vs <eugene@eug-vs.xyz> | 2023-02-21 13:26:18 +0300 |
---|---|---|
committer | eug-vs <eugene@eug-vs.xyz> | 2023-02-21 13:26:18 +0300 |
commit | 0d5c2eb1666d3038fa87895cc98eadc673c54a3a (patch) | |
tree | a8a3ae3d618bfb33c214b990b3206cb158b1c527 /src/board/mod.rs | |
parent | 08e27cdb8aa546833927b678d0a0d3ade1e91ef0 (diff) | |
download | chessnost-0d5c2eb1666d3038fa87895cc98eadc673c54a3a.tar.gz |
refactor: separate zobrist module
Diffstat (limited to 'src/board/mod.rs')
-rw-r--r-- | src/board/mod.rs | 43 |
1 files changed, 6 insertions, 37 deletions
diff --git a/src/board/mod.rs b/src/board/mod.rs index f7016d3..854ce0e 100644 --- a/src/board/mod.rs +++ b/src/board/mod.rs @@ -1,10 +1,12 @@ use crate::{bitboard::{Bitboard, BitboardFns}, moves::{Move, MoveKind}, attacks::Attacks, square::Square, board::io::IO}; -use self::ttable::{TranspositionTable, TTABLE_SIZE}; +use self::{ttable::{TranspositionTable, TTABLE_SIZE}, zobrist::ZobristSeed}; pub mod io; +mod zobrist; mod engine; mod ttable; +#[derive(Debug, Clone, Copy)] pub enum CastlingSide { King, Queen, @@ -26,7 +28,7 @@ pub struct Board { // TODO: remove transposition_table: TranspositionTable, - zobrist_seed: [u64; 781], + zobrist_seed: ZobristSeed, attacks: Attacks, } @@ -99,39 +101,6 @@ impl Board { self.pieces_by_color(color).iter().fold(0, |acc, bitboard| acc | bitboard) } - /// Compute and store zobrist hash of the current position - /// https://www.chessprogramming.org/Zobrist_Hashing - fn update_zobrist_hash(&mut self) { - self.hash = 0; - - if self.color() == Color::Black { - self.hash ^= match self.zobrist_seed.last() { - Some(x) => x, - None => panic!("Something is wrong with zobrist seed list"), - }; - } - - for (piece_id, bitboard) in self.piece_sets.iter().enumerate() { - for square in bitboard.serialize() { - self.hash ^= self.zobrist_seed[piece_id * 64 + square as usize]; - } - } - - for color in 0..2 { - for castle_side in 0..2 { - if self.castling_rights[color][castle_side] { - self.hash ^= self.zobrist_seed[(12 * 64) + color * 2 + castle_side]; - } - } - } - - match self.ep_target { - Some(square) => { - self.hash ^= self.zobrist_seed[(12 * 64 + 4) + square.file() as usize]; - }, - None => {}, - } - } fn ep_bitboard(&self) -> Bitboard { @@ -731,7 +700,7 @@ impl Color { #[cfg(test)] mod tests { use super::*; - use crate::{bitboard::BitboardFns, square::Square}; + use crate::{bitboard::BitboardFns, square::Square, board::zobrist::Zobrist}; #[test] fn square_enum() { @@ -813,7 +782,7 @@ mod tests { board.print(); let hash = board.hash; - board.update_zobrist_hash(); + board.compute_hash(); assert_eq!(hash, board.hash, "Hash should be correctly updated after move"); assert!(board.piece_sets[PieceType::PawnBlack as usize] & Square::F7.to_bitboard() == 0); |