zobrist.rs 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. use rand::prelude::*;
  2. use movegen::{PieceType};
  3. use bitboard::Square;
  4. pub type Hash = u64;
  5. pub struct ZobristTable {
  6. board: [[Hash; 12]; 64],
  7. en_passant: [Hash; 8],
  8. castling_rights: [Hash; 4],
  9. turn: Hash,
  10. }
  11. impl ZobristTable {
  12. pub fn new() -> Self {
  13. let mut zt = ZobristTable {
  14. board: [[0; 12]; 64],
  15. en_passant: [0; 8],
  16. castling_rights: [0; 4],
  17. turn: 0
  18. };
  19. let seed: [u8; 32] =
  20. [0x3f, 0x64, 0x5, 0x48, 0xef, 0x2d, 0xcb, 0x8e,
  21. 0xe6, 0x67, 0xc, 0x90, 0xcf, 0x10, 0x2d, 0x1e,
  22. 0x32, 0xb5, 0xbf, 0xe3, 0xf, 0x95, 0x8c, 0xf1,
  23. 0xbc, 0xe7, 0xb5, 0x76, 0x16, 0x4a, 0x17, 0x39];
  24. let mut rng = StdRng::from_seed(seed);
  25. for piece in &mut zt.board {
  26. for sq in piece {
  27. *sq = rng.next_u64();
  28. }
  29. }
  30. for file in &mut zt.en_passant {
  31. *file = rng.next_u64();
  32. }
  33. for cr in &mut zt.castling_rights {
  34. *cr = rng.next_u64();
  35. }
  36. zt.turn = rng.next_u64();
  37. return zt;
  38. }
  39. pub fn piece_hash(&self, piece_type: PieceType, sq: Square) -> Hash {
  40. self.board[sq as usize][piece_type as usize]
  41. }
  42. pub fn en_passant_hash(&self, file: u8) -> Hash {
  43. self.en_passant[file as usize]
  44. }
  45. pub fn castling_rights_hash(&self, cr: u8) -> Hash {
  46. self.castling_rights[cr as usize]
  47. }
  48. pub fn turn_hash(&self) -> Hash {
  49. self.turn
  50. }
  51. }