bhlangonijr / chesslib

chess library for legal move generation, FEN/PGN parsing and more
Apache License 2.0
229 stars 80 forks source link

pregenerated hash keys to replace unreliable hashCode() #16

Closed pawel-chess closed 5 years ago

pawel-chess commented 5 years ago

I changed Your board hashCode calculation somewhat, using a set of pregenerated 32-bit hash keys instead of hashCode() called on Piece and Square enums. I did this after witnessing a couple of false draw claims generated by Your otherwise great chesslib. This should not happen even with 32-bit keys, which are expected to create one collision for approximately 65.000 correct results (see https://www.chessprogramming.org/Zobrist_Hashing) Furthermore, You have no control over hashCode() value of an enum, which is generated based on memory address and so differs from machine to machine. This makes matters even worse, as even sending You a game which triggered the problem would not facilitate debugging. Errors tend to happen in endgame positions.

pawel-chess commented 5 years ago

Example game where draw by repetition has been claimed towards the end, after moving white king

e2e4 e7e5 g1f3 b8c6 f1c4 f8c5 c2c3 g8f6 d2d3 d7d6 a2a4 d6d5 e4d5 f6d5 e1g1 e8g8 f1e1 c8g4 h2h3 g4h5 b1d2 f7f5 d2f1 d8d6 f1g3 h5f3 d1f3 e5e4 c1f4 e4f3 f4d6 c5d6 c4d5+ g8h8 d5f3 f5f4 g3e4 c6e5 e4d6 e5f3+ g2f3 c7d6 e1e7 a8b8 a1e1 h8g8 e7d7 f8d8 e1e7 d8d7 e7d7 g7g5 d7d6 g8f7 d6d7+ f7g6 d3d4 h7h5 d4d5 a7a5 d5d6 b7b5 a4b5 b8b5 d7a7 b5b2 d6d7 b2d2 c3c4 g6f6 c4c5 f6e7 c5c6 e7d8 a7a5 d8c7 a5a8 c7c6 d7d8R d2e2 g1g2 e2e5 a8c8+ c6b6 c8c1 h5h4 d8b8+ b6a7 b8b2 e5a5 c1c2 a5e5 c2c3 a7a6 c3a3+ e5a5 a3a5+ a6a5 g2f1 a5a6 f1e2 a6a5 e2d2 a5a6 d2c3 a6a5 c3c4 a5a6 c4c5 g5g4 f3g4 f4f3 b2b3 a6a7 c5c6 a7a8 c6c7 a8a7 b3a3

bhlangonijr commented 5 years ago

Hello @pawel-chess,

Thank you for the contribution! Your change is available in release 1.1.12.

Best regards, Ben