1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//! Implements `ZobristArrays`.


/// Tables for calculating Zobrist hashes.
///
/// Zobrist hashing is a technique to transform a board position into
/// a number of a fixed length, with an equal distribution over all
/// possible numbers, invented by Albert Zobrist. The key property of
/// this method is that two similar positions generate entirely
/// different hash numbers.
pub struct ZobristArrays {
    /// The constant with which the hash value should be XOR-ed when
    /// the side to move changes.
    pub to_move: u64,

    /// Constants with which the hash value should be XOR-ed when a
    /// piece of given color on a given square appears/disappears.
    pub pieces: [[[u64; 64]; 6]; 2],

    /// Constants with which the hash value should be XOR-ed, for the
    /// old and the new castling rights on each move.
    pub castling_rights: [u64; 16],

    /// Constants with which the hash value should be XOR-ed, for the
    /// old and the new en-passant file on each move (a value between
    /// 0 and 7). Indexes between 8 and 15 point to zeroes, and are
    /// for convenience and memory safety.
    pub enpassant_file: [u64; 16],

    /// Constants with which the hash value should be XOR-ed to
    /// reflect the number of half-moves played without capturing a
    /// piece or advancing a pawn.
    pub halfmove_clock: [u64; 100],
}


impl ZobristArrays {
    /// Creates and initializes a new instance.
    fn new() -> ZobristArrays {
        use rand::{Rng, SeedableRng};
        use rand::isaac::Isaac64Rng;

        let seed: &[_] = &[1, 2, 3, 4];
        let mut rng: Isaac64Rng = SeedableRng::from_seed(seed);

        let to_move = rng.gen();
        let mut pieces = [[[0; 64]; 6]; 2];
        let mut castling_rights = [0; 16];
        let mut enpassant_file = [0; 16];
        let mut halfmove_clock = [0; 100];

        for color in 0..2 {
            for piece in 0..6 {
                for square in 0..64 {
                    pieces[color][piece][square] = rng.gen();
                }
            }
        }

        for value in 0..16 {
            castling_rights[value] = rng.gen();
        }

        for file in 0..8 {
            enpassant_file[file] = rng.gen();
        }

        for n in 0..100 {
            halfmove_clock[n] = rng.gen();
        }

        ZobristArrays {
            to_move: to_move,
            pieces: pieces,
            castling_rights: castling_rights,
            enpassant_file: enpassant_file,
            halfmove_clock: halfmove_clock,
        }
    }

    /// Returns a reference to an initialized `ZobristArrays` object.
    ///
    /// The object is created only during the first call. All next
    /// calls will return a reference to the same object. This is done
    /// in a thread-safe manner.
    pub fn get() -> &'static ZobristArrays {
        use std::sync::{Once, ONCE_INIT};
        static INIT_ARRAYS: Once = ONCE_INIT;
        static mut ARRAYS: Option<ZobristArrays> = None;
        unsafe {
            INIT_ARRAYS.call_once(|| {
                ARRAYS = Some(ZobristArrays::new());
            });
            ARRAYS.as_ref().unwrap()
        }
    }
}