tinycodinglm / chess /chess-0091.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 12)
3c88a0f verified
|
Raw History Blame Contribute Delete
4.08 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 alpha-beta pruning with iterative deepening, transposition table, and move ordering for a chess engine

Assistant

#include <cstdint>
#include <vector>
#include <algorithm>

struct Move { int from, to, score; uint8_t flags; };
struct TTEntry { uint64_t key; int depth, score, flag; Move bestMove; };

enum { TT_EXACT, TT_ALPHA, TT_BETA };

class ChessEngine {
static const int MAX_DEPTH = 64;
TTEntry transTable[1 << 24]; // 16M entries
int nodesSearched;

markdown
int quiesce(int alpha, int beta, int depth) {
    int standPat = evaluate();
    if (standPat >= beta) return beta;
    if (alpha < standPat) alpha = standPat;
    if (depth <= -8) return alpha;

    Move moves[256];
    int numMoves = generateCaptures(moves);
    sortMoves(moves, numMoves);

    for (int i = 0; i < numMoves; i++) {
        if (moves[i].score < 0) break; // losing captures
        makeMove(moves[i]);
        int score = -quiesce(-beta, -alpha, depth - 1);
        unmakeMove(moves[i]);

        if (score >= beta) return beta;
        if (score > alpha) alpha = score;
    }
    return alpha;
}

int alphaBeta(int depth, int alpha, int beta, bool doNull) {
    nodesSearched++;
    if (depth <= 0) return quiesce(alpha, beta, 0);

    uint64_t hash = computeHash();
    TTEntry& entry = transTable[hash & 0xFFFFFF];
    if (entry.key == hash && entry.depth >= depth) {
        if (entry.flag == TT_EXACT) return entry.score;
        if (entry.flag == TT_ALPHA && entry.score <= alpha) return alpha;
        if (entry.flag == TT_BETA && entry.score >= beta) return beta;
    }

    Move moves[256];
    int numMoves = generateLegalMoves(moves);
    sortMoves(moves, numMoves);

    int bestScore = -999999;
    Move bestMove = moves[0];

    for (int i = 0; i < numMoves; i++) {
        makeMove(moves[i]);
        int score;

        // Late move reductions
        if (i >= 4 && depth >= 3 && !isCapture(moves[i]) && !inCheck()) {
            score = -alphaBeta(depth - 2, -alpha - 1, -alpha, true);
            if (score <= alpha) continue;
        }

        if (i == 0) {
            score = -alphaBeta(depth - 1, -beta, -alpha, true);
        } else {
            score = -alphaBeta(depth - 1, -alpha - 1, -alpha, true);
            if (score > alpha && score < beta) {
                score = -alphaBeta(depth - 1, -beta, -alpha, true);
            }
        }
        unmakeMove(moves[i]);

        if (score > bestScore) {
            bestScore = score;
            bestMove = moves[i];
        }
        if (score > alpha) alpha = score;
        if (score >= beta) break;
    }

    entry.key = hash;
    entry.depth = depth;
    entry.bestMove = bestMove;
    if (bestScore <= alpha) entry.flag = TT_ALPHA;
    else if (bestScore >= beta) entry.flag = TT_BETA;
    else entry.flag = TT_EXACT;
    entry.score = bestScore;

    return bestScore;
}

Move iterativeDeepening(int maxDepth) {
    Move bestMove;
    for (int depth = 1; depth <= maxDepth; depth++) {
        int score = alphaBeta(depth, -999999, 999999, true);
        bestMove = transTable[computeHash() & 0xFFFFFF].bestMove;
        // Soft time limit check here
    }
    return bestMove;
}

};