File size: 3,444 Bytes
3c88a0f
 
 
 
1
2
3
4
5
{"type": "session", "harness": "opencode", "id": "chess-0087", "name": "Implement a Polyglot opening book reader for a chess engine in C++"}
{"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 a Polyglot opening book reader for a chess engine in C++"}}
{"type": "message", "message": {"role": "assistant", "content": "#include <cstdint>\n#include <fstream>\n#include <vector>\n\n#pragma pack(push, 1)\nstruct PolyglotEntry {\n    uint64_t key;\n    uint16_t move;\n    uint16_t weight;\n    uint32_t learn;\n};\n#pragma pack(pop)\n\nstruct BookMove {\n    int from, to, promotion;\n    int weight;\n};\n\nclass OpeningBook {\n    std::vector<PolyglotEntry> entries;\n    uint64_t polyglotRandom[781];\n\n    void initRandomKeys() {\n        std::ifstream urandom(\"C:\\\\dev\\\\urandom\", std::ios::binary);\n        for (int i = 0; i < 781; i++) {\n            urandom.read(reinterpret_cast<char*>(&polyglotRandom[i]), 8);\n        }\n    }\n\n    uint64_t polyglotHash(const BitboardBoard& board) const {\n        uint64_t key = 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                key ^= polyglotRandom[sq + pt * 64];\n                bb &= bb - 1;\n            }\n        }\n        key ^= polyglotRandom[768 + board.castlingRights];\n        if (board.enPassantSquare >= 0) {\n            key ^= polyglotRandom[772 + (board.enPassantSquare % 8)];\n        }\n        if (board.sideToMove == BLACK) key ^= polyglotRandom[780];\n        return key;\n    }\n\n    void loadBook(const char* filename) {\n        std::ifstream file(filename, std::ios::binary | std::ios::ate);\n        int size = file.tellg();\n        int numEntries = size / sizeof(PolyglotEntry);\n        file.seekg(0);\n        entries.resize(numEntries);\n        file.read(reinterpret_cast<char*>(entries.data()), size);\n    }\n\n    std::vector<BookMove> probe(const BitboardBoard& board) const {\n        uint64_t key = polyglotHash(board);\n        std::vector<BookMove> results;\n\n        // Binary search for first entry\n        int lo = 0, hi = entries.size();\n        while (lo < hi) {\n            int mid = (lo + hi) / 2;\n            if (entries[mid].key < key) lo = mid + 1;\n            else hi = mid;\n        }\n\n        for (int i = lo; i < (int)entries.size(); i++) {\n            if (entries[i].key != key) break;\n            BookMove bm;\n            bm.from = ((entries[i].move >> 6) & 0x3F);\n            bm.to = (entries[i].move & 0x3F);\n            bm.promotion = ((entries[i].move >> 12) & 0x7);\n            bm.weight = entries[i].weight;\n            results.push_back(bm);\n        }\n        return results;\n    }\n\n    Move getBookMove(const BitboardBoard& board) {\n        auto moves = probe(board);\n        if (moves.empty()) return Move{-1, -1, 0, 0};\n        int totalWeight = 0;\n        for (auto& m : moves) totalWeight += m.weight;\n        int r = rand() % totalWeight;\n        for (auto& m : moves) {\n            r -= m.weight;\n            if (r < 0) return Move{m.from, m.to, 0, 0};\n        }\n        return Move{moves[0].from, moves[0].to, 0, 0};\n    }\n};"}}