Zobrist hash input

Discussion of chess software programming and technical issues.

Moderators: hgm, Rebel, chrisw

Peperoni
Posts: 72
Joined: Sun Nov 01, 2020 5:27 pm
Full name: Richard Porti

Zobrist hash input

Post by Peperoni »

Hello,

What string should I take an an input to generate Zobrist hash on a given position?
Can I just do a 64 string where each square is represented by a letter?
"P" for a white pawn, "p" for a black pawn
"Q" for a white queen, "q" for a black queen, etc. ?
brianr
Posts: 536
Joined: Thu Mar 09, 2006 3:01 pm

Re: Zobrist hash input

Post by brianr »

A FEN string would be fine as the side to move, ep square (if any) and castle status should also be encoded.
Peperoni
Posts: 72
Joined: Sun Nov 01, 2020 5:27 pm
Full name: Richard Porti

Re: Zobrist hash input

Post by Peperoni »

I am using this to quickly find a position on a database.

From what I read, if you do permutations of the characters in the input string, you end up with the same hash?

That's why I was also thinking to use the concatenation of the binary moves of the games (1 byte per move) but this doesn't solve the issue if you use a chess editor instead of playing the moves of the wanted position.
brianr
Posts: 536
Joined: Thu Mar 09, 2006 3:01 pm

Re: Zobrist hash input

Post by brianr »

syzygy
Posts: 5566
Joined: Tue Feb 28, 2012 11:56 pm

Re: Zobrist hash input

Post by syzygy »

Peperoni wrote: Thu Dec 10, 2020 4:58 pm Hello,

What string should I take an an input to generate Zobrist hash on a given position?
Can I just do a 64 string where each square is represented by a letter?
"P" for a white pawn, "p" for a black pawn
"Q" for a white queen, "q" for a black queen, etc. ?
Zobrist hash has a specific meaning, which you can google.
https://www.chessprogramming.org/Zobrist_Hashing
Peperoni
Posts: 72
Joined: Sun Nov 01, 2020 5:27 pm
Full name: Richard Porti

Re: Zobrist hash input

Post by Peperoni »

Yes I read that, but I read somewhere else than zob(ab) = zob(ba) so I am a little confused.
Is passing the FEN as an input the good way to do it?
syzygy
Posts: 5566
Joined: Tue Feb 28, 2012 11:56 pm

Re: Zobrist hash input

Post by syzygy »

Peperoni wrote: Sat Dec 12, 2020 10:41 am Is passing the FEN as an input the good way to do it?
I don't know what that means in this context.

Zobrist hashing is this:
0. Forget about FENs.
1. Choose a number of bits N (e.g. 32, 48, 64 or 128).
2. Use a (good enough) random generator to generate an N-bit number of each combination of (color, piecetype, square) where color is one of white,black, piecetype is one of pawn,knight,bishop,rook,qeen,king, and square is one of A1,A2,...,H8. (So these are 2 x 6 x 64 numbers.)
3. Generate a couple more N-bit random numbers for:
- black to move (1 number)
- white/black, long/short castling rights (4 numbers)
- en passant rights on file A, B, C, D, E, F, G, H (8 numbers)
4. To calculate the Zobrist hashkey of a position, XOR all the numbers applicable to the position together.

The big advantage of this scheme is that the Zobrist hashkey can be updated incrementally when making a move. This is because (A XOR B) XOR B = A XOR (B XOR B) = A XOR 0 = A for all values of A and B.

I think practically everybody uses N=64 nowadays.