File size: 2,592 Bytes
3c88a0f
 
 
 
1
2
3
4
5
{"type": "session", "harness": "opencode", "id": "chess-0098", "name": "Implement Zobrist hashing with incremental updates for efficient transposition table lookups"}
{"type": "message", "message": {"role": "system", "content": "You are an expert programmer specializing in chess programming and game AI. You implement efficient algorithms with optimal performance."}}
{"type": "message", "message": {"role": "user", "content": "Implement Zobrist hashing with incremental updates for efficient transposition table lookups"}}
{"type": "message", "message": {"role": "assistant", "content": "#include <cstdint>\n#include <random>\n\nnamespace Zobrist {\n    uint64_t pieceKeys[12][64];  // [pieceType][square]\n    uint64_t castlingKeys[16];   // 4-bit castling rights\n    uint64_t epKeys[8];          // en passant file\n    uint64_t sideKey;            // side to move\n\n    void init() {\n        std::mt19937_64 rng(0x5D14312AED46B9A5ULL); // fixed seed for reproducibility\n        std::uniform_int_distribution<uint64_t> dist;\n\n        for (int pt = 0; pt < 12; pt++)\n            for (int sq = 0; sq < 64; sq++)\n                pieceKeys[pt][sq] = dist(rng);\n\n        for (int i = 0; i < 16; i++)\n            castlingKeys[i] = dist(rng);\n\n        for (int i = 0; i < 8; i++)\n            epKeys[i] = dist(rng);\n\n        sideKey = dist(rng);\n    }\n\n    uint64_t computeFull(const BitboardBoard& board) {\n        uint64_t hash = 0;\n        for (int pt = 0; pt < 12; pt++) {\n            uint64_t bb = board.pieces[pt];\n            while (bb) {\n                int sq = __builtin_ctzll(bb);\n                hash ^= pieceKeys[pt][sq];\n                bb &= bb - 1;\n            }\n        }\n        hash ^= castlingKeys[board.castlingRights];\n        if (board.enPassantSquare >= 0)\n            hash ^= epKeys[board.enPassantSquare % 8];\n        if (board.sideToMove == BLACK)\n            hash ^= sideKey;\n        return hash;\n    }\n\n    // Incremental update helpers\n    inline uint64_t xorPiece(uint64_t hash, int pt, int sq) {\n        return hash ^ pieceKeys[pt][sq];\n    }\n\n    inline uint64_t xorCastling(uint64_t hash, int oldRights, int newRights) {\n        return hash ^ castlingKeys[oldRights] ^ castlingKeys[newRights];\n    }\n\n    inline uint64_t xorEP(uint64_t hash, int oldSq, int newSq) {\n        uint64_t h = hash;\n        if (oldSq >= 0) h ^= epKeys[oldSq % 8];\n        if (newSq >= 0) h ^= epKeys[newSq % 8];\n        return h;\n    }\n\n    inline uint64_t flipSide(uint64_t hash) {\n        return hash ^ sideKey;\n    }\n}"}}