File size: 3,976 Bytes
3c88a0f
 
 
 
1
2
3
4
5
{"type": "session", "harness": "opencode", "id": "chess-0082", "name": "Implement advanced move ordering with history heuristic and killer moves for alpha-beta search"}
{"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 advanced move ordering with history heuristic and killer moves for alpha-beta search"}}
{"type": "message", "message": {"role": "assistant", "content": "#include <cstdint>\n#include <algorithm>\n\nstruct MoveOrdering {\n    static const int MAX_PLY = 64;\n    static const int MAX_MOVES = 256;\n\n    int history[2][64][64];           // [color][from][to]\n    Move killerMoves[MAX_PLY][2];     // two killers per ply\n    int counterMoves[64][64];         // countermove heuristic\n\n    void clearHistory() {\n        memset(history, 0, sizeof(history));\n        memset(killerMoves, 0, sizeof(killerMoves));\n        memset(counterMoves, 0, sizeof(counterMoves));\n    }\n\n    void updateHistory(Color color, int from, int to, int depth) {\n        int& entry = history[color][from][to];\n        entry += depth * depth;\n        // Age history periodically\n        if (entry > 4000000) {\n            for (int i = 0; i < 64; i++)\n                for (int j = 0; j < 64; j++)\n                    history[color][i][j] /= 2;\n        }\n    }\n\n    void updateKillers(Move move, int ply) {\n        if (move != killerMoves[ply][0]) {\n            killerMoves[ply][1] = killerMoves[ply][0];\n            killerMoves[ply][0] = move;\n        }\n    }\n\n    void updateCounterMove(int from, int to, Move move) {\n        counterMoves[from][to] = move;\n    }\n\n    void scoreMoves(Move* moves, int numMoves, int ply, Color side,\n                    Move prevMove) {\n        for (int i = 0; i < numMoves; i++) {\n            Move& m = moves[i];\n            m.score = 0;\n\n            // MVV-LVA for captures\n            if (isCapture(m)) {\n                int victimVal = PIECE_VALUES[board.pieceAt(m.to)] * 10;\n                int attackerVal = PIECE_VALUES[board.pieceAt(m.from)];\n                m.score = 1000000 + victimVal - attackerVal + SEE(m);\n            }\n            // Promotion\n            else if (m.flags & PROMOTION) {\n                m.score = 900000 + PIECE_VALUES[m.promotionPiece];\n            }\n            // Killer moves\n            else if (m == killerMoves[ply][0]) {\n                m.score = 800000;\n            }\n            else if (m == killerMoves[ply][1]) {\n                m.score = 700000;\n            }\n            // Countermove\n            else if (prevMove && counterMoves[prevMove.from][prevMove.to] == m) {\n                m.score = 600000;\n            }\n            // History heuristic\n            else {\n                m.score = history[side][m.from][m.to];\n            }\n        }\n    }\n\n    void sortMoveList(Move* moves, int numMoves) {\n        std::sort(moves, moves + numMoves, [](const Move& a, const Move& b) {\n            return a.score > b.score;\n        });\n    }\n\n    int SEE(const Move& move) {\n        // Static Exchange Evaluation\n        int target = PIECE_VALUES[board.pieceAt(move.to)];\n        int gain[32];\n        int depth = 0;\n        uint64_t occ = board.occupancy[BOTH];\n        uint64_t attackers = getAttackers(move.from, occ);\n        int side = board.pieceAtColor(move.from);\n\n        gain[0] = target;\n        while (true) {\n            depth++;\n            gain[depth] = PIECEValues[leastAttacker(attackers)] - gain[depth-1];\n            attackers ^= leastAttackerBB(attackers);\n            side ^= 1;\n            if (gain[depth] < 0) break;\n        }\n        while (--depth) {\n            gain[depth-1] = -std::max(-gain[depth-1], gain[depth]);\n        }\n        return gain[0];\n    }\n};"}}