tinycodinglm / chess /chess-0093.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 12)
3c88a0f verified
|
Raw History Blame Contribute Delete
2.81 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

Calculate the Shannon number (game tree complexity of chess) with move generation statistics

Assistant

#include <cstdint>
#include <cstdio>

struct ShannonEstimator {
struct Stats {
uint64_t totalNodes;
uint64_t leafNodes;
uint64_t internalNodes;
double avgBranching;
int maxDepth;
};

markdown
// The Shannon number estimate: ~10^120 game tree complexity
// This implementation estimates it empirically via random playouts

Stats estimate(BitboardBoard& board, int numPlayouts, int maxDepth) {
    Stats stats = {};
    std::mt19937 rng(std::random_device{}());

    for (int i = 0; i < numPlayouts; i++) {
        BitboardBoard sim = board;
        int depth = 0;
        uint64_t nodesThis = 0;

        while (!sim.isTerminal() && depth < maxDepth) {
            Move moves[256];
            int n = generateLegalMoves(sim, moves);
            if (n == 0) break;

            // Random move
            std::uniform_int_distribution<int> dist(0, n - 1);
            sim.makeMove(moves[dist(rng)]);
            nodesThis += n;
            depth++;
        }

        stats.totalNodes += nodesThis;
        stats.leafNodes++;
        if (depth > stats.maxDepth) stats.maxDepth = depth;
    }

    stats.internalNodes = stats.totalNodes - stats.leafNodes;
    stats.avgBranching = (double)stats.totalNodes / stats.leafNodes;

    return stats;
}

void printEstimate() {
    // Known values:
    // Average branching factor: ~35
    // Average game length: ~80 moves
    // Shannon number: ~10^120
    // Game tree complexity: ~10^123

    double b = 35.0;  // average branching factor
    double d = 80.0;   // average game depth
    double shannon = std::pow(b, d);

    printf("Chess Game Tree Complexity Estimates:\n");
    printf("  Average branching factor: %.0f\n", b);
    printf("  Average game depth: %.0f moves\n", d);
    printf("  Shannon number: ~10^%d\n", (int)std::log10(shannon));
    printf("  Position complexity: ~10^47 (unique positions)\n");
    printf("  Game tree complexity: ~10^123\n");
}

};