tinycodinglm / chess /chess-0087.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 12)
3c88a0f verified
|
Raw History Blame Contribute Delete
3.44 kB
opencode trace
System You are an expert programmer specializing in chess programming and game AI. You implement efficient algorithms with optimal performance.

You are an expert programmer specializing in chess programming and game AI. You implement efficient algorithms with optimal performance.

User

Implement a Polyglot opening book reader for a chess engine in C++

Assistant

#include <cstdint>
#include <fstream>
#include <vector>

#pragma pack(push, 1)
struct PolyglotEntry {
uint64_t key;
uint16_t move;
uint16_t weight;
uint32_t learn;
};
#pragma pack(pop)

struct BookMove {
int from, to, promotion;
int weight;
};

class OpeningBook {
std::vector<PolyglotEntry> entries;
uint64_t polyglotRandom[781];

markdown
void initRandomKeys() {
    std::ifstream urandom("C:\\dev\\urandom", std::ios::binary);
    for (int i = 0; i < 781; i++) {
        urandom.read(reinterpret_cast<char*>(&polyglotRandom[i]), 8);
    }
}

uint64_t polyglotHash(const BitboardBoard& board) const {
    uint64_t key = 0;
    for (int pt = 0; pt < 12; pt++) {
        uint64_t bb = board.pieces[pt];
        while (bb) {
            int sq = __builtin_ctzll(bb);
            key ^= polyglotRandom[sq + pt * 64];
            bb &= bb - 1;
        }
    }
    key ^= polyglotRandom[768 + board.castlingRights];
    if (board.enPassantSquare >= 0) {
        key ^= polyglotRandom[772 + (board.enPassantSquare % 8)];
    }
    if (board.sideToMove == BLACK) key ^= polyglotRandom[780];
    return key;
}

void loadBook(const char* filename) {
    std::ifstream file(filename, std::ios::binary | std::ios::ate);
    int size = file.tellg();
    int numEntries = size / sizeof(PolyglotEntry);
    file.seekg(0);
    entries.resize(numEntries);
    file.read(reinterpret_cast<char*>(entries.data()), size);
}

std::vector<BookMove> probe(const BitboardBoard& board) const {
    uint64_t key = polyglotHash(board);
    std::vector<BookMove> results;

    // Binary search for first entry
    int lo = 0, hi = entries.size();
    while (lo < hi) {
        int mid = (lo + hi) / 2;
        if (entries[mid].key < key) lo = mid + 1;
        else hi = mid;
    }

    for (int i = lo; i < (int)entries.size(); i++) {
        if (entries[i].key != key) break;
        BookMove bm;
        bm.from = ((entries[i].move >> 6) & 0x3F);
        bm.to = (entries[i].move & 0x3F);
        bm.promotion = ((entries[i].move >> 12) & 0x7);
        bm.weight = entries[i].weight;
        results.push_back(bm);
    }
    return results;
}

Move getBookMove(const BitboardBoard& board) {
    auto moves = probe(board);
    if (moves.empty()) return Move{-1, -1, 0, 0};
    int totalWeight = 0;
    for (auto& m : moves) totalWeight += m.weight;
    int r = rand() % totalWeight;
    for (auto& m : moves) {
        r -= m.weight;
        if (r < 0) return Move{m.from, m.to, 0, 0};
    }
    return Move{moves[0].from, moves[0].to, 0, 0};
}

};