I didn't want to write an article, because the Zobrist hashing entry on the English wiki is already detailed enough. Later I found that my thesis had a illustrated section about the Zobrist hash. I wanted to write it for word and image sharing. It was also convenient to paste it out:
The Zobrist hash is an encoding method specifically designed for chess games, named by Albert L. Zobrist, its inventor. The Zobrist hash uses a special replacement table, that is, to assign an encoded index value to the various possible States at each position on the board, to realize the extremely low conflict rate, encode the Board on an integer data. The encoding steps are described as follows:
1) divide the board into the smallest unit (if the 9x9 circle board is divided into 81 intersections), find the number of different States on each unit (for example, there are 3 states at one intersection on the Board ).
2) generates a random number within a certain range (such as a 64-bit integer) for each State in each unit.
3) for a specific game board, the random number corresponding to the state in each unit is used as an exclusive or operation, and the obtained result is a hash value.
Using a Zobrist hash to encode a game status has at least two advantages:
1) when the range of a random number is large enough, the probability of hash conflicts between different chess and board games is very small, which can be ignored in practical applications.
2) You do not need to re-calculate the hash value of the Board every time during the process of playing a board. You only need to calculate the part where the Board status changes.
The following uses a go board as an example to describe the Zobrist hash:
The 2x2 go board has a total of four units, each of which has three States (Sunday, white child, empty point). An 8-digit random number is generated for each state:
For the following games:
Well, the image and text are mixed, and the effect is not very good ......