I'm currently adding transposition tables in my chess engine, and I'm having issues with incrementally updating Zobrist keys. I did some research and implemented the basic idea, but it's not behaving as I expect. The problem I encountered was that equivalent board positions do not always have the same keys. For example, in the starting position, if both players just moved a knight and then moved it back, the key would be different from that of the starting position. However, doing this again (moving the knights) and returning to the starting position would result in the original key. So it seems that the period for such sequence is 4 moves for each player, when it should just be 2.
Has anyone encountered such a problem or can think of solution? I've included the relevant portions of my make/unmake methods. I don't include side-to-move, castling rights, etc; they shouldn't affect the particular case I brought up. HashValue stores the random values, with the first index being the piece type and second being the square.
void Make(Move m) {
ZobristKey ^= HashValue[Piece[m.From].Type][m.From];
ZobristKey ^= HashValue[Piece[m.From].Type][m.To];
ZobristKey ^= HashValue[Piece[m.To].Type][m.To];
//rest of make move
}
void Unmake(Move m) {
ZobristKey ^= HashValue[m.Captured.Type][m.To];
ZobristKey ^= HashValue[Element[m.To].Type][m.To];
ZobristKey ^= HashValue[Element[m.To].Type][m.From];
//rest of unmake
}