aclap-dev / jocly

Javascript library and tools to provide user interface (2D, 3D, VR) and engine for playing board games
https://jocly.com/
Other
71 stars 28 forks source link

Zobrist posstion hash in Chess #36

Open HGMuller opened 5 years ago

HGMuller commented 5 years ago

I am trying to understand how the "board" hashing works in the context of the Jocly chessbase module. This in order to see what I would have to do to update the key for games involving dropping of captured pieces, so that capture-drop-spanning repetitions will also be correctly detected there.

Am I correct in that the zSign key is the (incrementally updated) XOR of all the 'seeds' for individual (pieceIndex, boardSquare) pairs? (I.e. (piece.i, piece.p)?) This seems wrong, in a way that badly backfires when a given piece can promote and demote, or toggle its color. Even without that it could go wrong: when two pieces of equal type swap places, it considers the positions different, while according to Chess rules it would have to be considered the same. The seeds should really depend on the (colored) piece type and the location (i.e. (piece.t, piece.s, piece.p)), and should be blind to the index the piece happens to have in the piece list.

I suppose this could be fixed by replacing the use of piece.i in the Zobrist.update everywhere by piece.t*(1+piece.s). This would even reduce the number of required seeds from 32 to 18 times the board size, in orthodox Chess, even though 1/3 of the seeds (for piece.s==0) would be wasted; there usually are significantly fewer types than pieces (because there are so many Pawns).

HGMuller commented 5 years ago

Sorry, I was a bit hasty in mapping piece.tpiece.s to a positive range; I should of course have said piece.tpiece.s+NROFTYPES as instead of piece.i .

HGMuller commented 5 years ago

Hmm, the posting software here mutilates the posted text, by replacing the multiplication asterisks by switching between plain and italics font. But I hope you get the idea.