File size: 4,083 Bytes
3c88a0f
 
 
 
1
2
3
4
5
{"type": "session", "harness": "opencode", "id": "chess-0091", "name": "Implement alpha-beta pruning with iterative deepening, transposition table, and move ordering 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 alpha-beta pruning with iterative deepening, transposition table, and move ordering for a chess engine"}}
{"type": "message", "message": {"role": "assistant", "content": "#include <cstdint>\n#include <vector>\n#include <algorithm>\n\nstruct Move { int from, to, score; uint8_t flags; };\nstruct TTEntry { uint64_t key; int depth, score, flag; Move bestMove; };\n\nenum { TT_EXACT, TT_ALPHA, TT_BETA };\n\nclass ChessEngine {\n    static const int MAX_DEPTH = 64;\n    TTEntry transTable[1 << 24]; // 16M entries\n    int nodesSearched;\n\n    int quiesce(int alpha, int beta, int depth) {\n        int standPat = evaluate();\n        if (standPat >= beta) return beta;\n        if (alpha < standPat) alpha = standPat;\n        if (depth <= -8) return alpha;\n\n        Move moves[256];\n        int numMoves = generateCaptures(moves);\n        sortMoves(moves, numMoves);\n\n        for (int i = 0; i < numMoves; i++) {\n            if (moves[i].score < 0) break; // losing captures\n            makeMove(moves[i]);\n            int score = -quiesce(-beta, -alpha, depth - 1);\n            unmakeMove(moves[i]);\n\n            if (score >= beta) return beta;\n            if (score > alpha) alpha = score;\n        }\n        return alpha;\n    }\n\n    int alphaBeta(int depth, int alpha, int beta, bool doNull) {\n        nodesSearched++;\n        if (depth <= 0) return quiesce(alpha, beta, 0);\n\n        uint64_t hash = computeHash();\n        TTEntry& entry = transTable[hash & 0xFFFFFF];\n        if (entry.key == hash && entry.depth >= depth) {\n            if (entry.flag == TT_EXACT) return entry.score;\n            if (entry.flag == TT_ALPHA && entry.score <= alpha) return alpha;\n            if (entry.flag == TT_BETA && entry.score >= beta) return beta;\n        }\n\n        Move moves[256];\n        int numMoves = generateLegalMoves(moves);\n        sortMoves(moves, numMoves);\n\n        int bestScore = -999999;\n        Move bestMove = moves[0];\n\n        for (int i = 0; i < numMoves; i++) {\n            makeMove(moves[i]);\n            int score;\n\n            // Late move reductions\n            if (i >= 4 && depth >= 3 && !isCapture(moves[i]) && !inCheck()) {\n                score = -alphaBeta(depth - 2, -alpha - 1, -alpha, true);\n                if (score <= alpha) continue;\n            }\n\n            if (i == 0) {\n                score = -alphaBeta(depth - 1, -beta, -alpha, true);\n            } else {\n                score = -alphaBeta(depth - 1, -alpha - 1, -alpha, true);\n                if (score > alpha && score < beta) {\n                    score = -alphaBeta(depth - 1, -beta, -alpha, true);\n                }\n            }\n            unmakeMove(moves[i]);\n\n            if (score > bestScore) {\n                bestScore = score;\n                bestMove = moves[i];\n            }\n            if (score > alpha) alpha = score;\n            if (score >= beta) break;\n        }\n\n        entry.key = hash;\n        entry.depth = depth;\n        entry.bestMove = bestMove;\n        if (bestScore <= alpha) entry.flag = TT_ALPHA;\n        else if (bestScore >= beta) entry.flag = TT_BETA;\n        else entry.flag = TT_EXACT;\n        entry.score = bestScore;\n\n        return bestScore;\n    }\n\n    Move iterativeDeepening(int maxDepth) {\n        Move bestMove;\n        for (int depth = 1; depth <= maxDepth; depth++) {\n            int score = alphaBeta(depth, -999999, 999999, true);\n            bestMove = transTable[computeHash() & 0xFFFFFF].bestMove;\n            // Soft time limit check here\n        }\n        return bestMove;\n    }\n};"}}