Correctly Implementing Zobrist Hashing

我的梦境 提交于 2019-12-08 08:59:42

问题


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
}

回答1:


Make_a_move() {
    hashval ^= Zobrist_array[oldpos][piece];
    hashval ^= Zobrist_array[newpos][piece];
    /* if there is a capture */
    hashval ^= Zobrist_array[otherpos][otherpiece];
    }

Undo_a_move() {
    hashval ^= Zobrist_array[oldpos][piece];
    hashval ^= Zobrist_array[newpos][piece];
    /* if there was a capture */
    hashval ^= Zobrist_array[otherpos][otherpiece];
    }

Castling can be seen as the sum of two moves (without capture, obviously)

Promotion can be treated as removing a pawn from the board (from the 2 or 7 position) and adding the new piece (at the 1 or 8 position)



来源:https://stackoverflow.com/questions/10067514/correctly-implementing-zobrist-hashing

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!