File size: 3,290 Bytes
3c88a0f
 
 
 
1
2
3
4
5
{"type": "session", "harness": "opencode", "id": "chess-0095", "name": "Implement Syzygy endgame tablebase probing for a chess engine"}
{"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 Syzygy endgame tablebase probing for a chess engine"}}
{"type": "message", "message": {"role": "assistant", "content": "#include <cstdint>\n#include <cstring>\n#include <algorithm>\n\nclass SyzygyTablebase {\n    static const int TBPIECES = 7;\n    struct TBEntry {\n        uint64_t key;\n        int pieceCount;\n        uint8_t pieceOrder;\n        uint8_t* data;\n        uint32_t numEntries;\n    };\n\n    struct TBHashEntry {\n        uint64_t key;\n        TBEntry* ptr;\n    };\n\n    TBHashEntry hashTable[1 << 12]; // simplified\n    int numTables;\n\n    uint64_t computeKey(const BitboardBoard& board) {\n        uint64_t key = 0;\n        int pieceCount[2] = {0, 0};\n        for (int pt = 0; pt < 6; pt++) {\n            uint64_t bb = board.pieces[pt];\n            while (bb) {\n                int sq = __builtin_ctzll(bb);\n                pieceCount[WHITE]++;\n                key ^= Zobrist::pieceKeys[pt][sq];\n                bb &= bb - 1;\n            }\n            bb = board.pieces[pt + 6];\n            while (bb) {\n                int sq = __builtin_ctzll(bb);\n                pieceCount[BLACK]++;\n                key ^= Zobrist::pieceKeys[pt + 6][sq];\n                bb &= bb - 1;\n            }\n        }\n        return key;\n    }\n\n    int probeWDL(const BitboardBoard& board) {\n        uint64_t key = computeKey(board);\n        TBEntry* entry = findEntry(key);\n        if (!entry) return -2; // not found\n\n        int pieces[TBPIECES];\n        int numPieces = 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                pieces[numPieces++] = pt * 64 + sq;\n                bb &= bb - 1;\n            }\n        }\n        std::sort(pieces, pieces + numPieces);\n\n        uint64_t idx = computeIndex(entry, pieces, numPieces, board.occupancy[BOTH]);\n        uint8_t wdl = entry->data[idx / 2];\n        if (idx & 1) wdl >>= 4;\n        wdl &= 0x0F;\n\n        return wdl; // 2=win, 1=cursed-win, 0=draw, -1=blessed-loss, -2=loss\n    }\n\n    int probeDTZ(const BitboardBoard& board) {\n        int wdl = probeWDL(board);\n        if (wdl == 0) return 0;\n        if (wdl == -2) return -1;\n        if (wdl == 2) return 1;\n\n        uint64_t key = computeKey(board);\n        TBEntry* entry = findEntry(key);\n        if (!entry) return -1;\n\n        // DTZ probing withPawn/DtZ tables\n        int dtz = computeDTZ(entry, board);\n        if (wdl < 0) dtz = -dtz;\n        return dtz;\n    }\n\n    bool isDraw(int dtz) const {\n        return dtz == 0;\n    }\n\n    bool isWinning(int dtz) const {\n        return dtz > 0;\n    }\n\n    bool hasTablebase(const BitboardBoard& board) const {\n        int pieceCount = popcount(board.occupancy[BOTH]);\n        return pieceCount <= TBPIECES;\n    }\n};"}}