Zobrist Hash
Zobrist Hashing, a technique to transform a board position of arbitrary size into a number of a set length, with an equal distribution over all possible numbers. - chessprogramming.org
wikipedia: Zobrist hashing starts by randomly generating bitstrings for each possible element of a board game, i.e. for each combination of a piece and a position… Now any board configuration can be broken up into independent piece/position components, which are mapped to the random bitstrings generated earlier. The final Zobrist hash is computed by combining those bitstrings using bitwise XOR
.
Updating hash
Because of XOR properties, the hash can be delta updated:
- by xoring (out) the bitstring of removed events,
- by xoring (in) the bitstring of new events.
Xoring in/out being the same
XOR
operation, it onlys express the intent.
Pseudo code
Example pseudocode for the game of chess
References
- Zobrist Hashing - You won’t be encountering this algorithm until and unless you are facing a really large board. Some thing like 8x8 (chess) or 16x16 (ultimate tic-tac-toe)
- An Efficient Work Distribution Method for ParallelBest-First Search
- Transposition Table and Zobrist Hashing
- Zobrist Hashing
- Correctly Implementing Zobrist Hashing
- Zobrist Hashing
Written on December 23, 2020, Last update on January 6, 2021
chess
hash
xor