tinycodinglm / chess /chess-0082.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 12)
3c88a0f verified
|
Raw History Blame Contribute Delete
3.98 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 advanced move ordering with history heuristic and killer moves for alpha-beta search

Assistant

#include <cstdint>
#include <algorithm>

struct MoveOrdering {
static const int MAX_PLY = 64;
static const int MAX_MOVES = 256;

markdown
int history[2][64][64];           // [color][from][to]
Move killerMoves[MAX_PLY][2];     // two killers per ply
int counterMoves[64][64];         // countermove heuristic

void clearHistory() {
    memset(history, 0, sizeof(history));
    memset(killerMoves, 0, sizeof(killerMoves));
    memset(counterMoves, 0, sizeof(counterMoves));
}

void updateHistory(Color color, int from, int to, int depth) {
    int& entry = history[color][from][to];
    entry += depth * depth;
    // Age history periodically
    if (entry > 4000000) {
        for (int i = 0; i < 64; i++)
            for (int j = 0; j < 64; j++)
                history[color][i][j] /= 2;
    }
}

void updateKillers(Move move, int ply) {
    if (move != killerMoves[ply][0]) {
        killerMoves[ply][1] = killerMoves[ply][0];
        killerMoves[ply][0] = move;
    }
}

void updateCounterMove(int from, int to, Move move) {
    counterMoves[from][to] = move;
}

void scoreMoves(Move* moves, int numMoves, int ply, Color side,
                Move prevMove) {
    for (int i = 0; i < numMoves; i++) {
        Move& m = moves[i];
        m.score = 0;

        // MVV-LVA for captures
        if (isCapture(m)) {
            int victimVal = PIECE_VALUES[board.pieceAt(m.to)] * 10;
            int attackerVal = PIECE_VALUES[board.pieceAt(m.from)];
            m.score = 1000000 + victimVal - attackerVal + SEE(m);
        }
        // Promotion
        else if (m.flags & PROMOTION) {
            m.score = 900000 + PIECE_VALUES[m.promotionPiece];
        }
        // Killer moves
        else if (m == killerMoves[ply][0]) {
            m.score = 800000;
        }
        else if (m == killerMoves[ply][1]) {
            m.score = 700000;
        }
        // Countermove
        else if (prevMove && counterMoves[prevMove.from][prevMove.to] == m) {
            m.score = 600000;
        }
        // History heuristic
        else {
            m.score = history[side][m.from][m.to];
        }
    }
}

void sortMoveList(Move* moves, int numMoves) {
    std::sort(moves, moves + numMoves, [](const Move& a, const Move& b) {
        return a.score > b.score;
    });
}

int SEE(const Move& move) {
    // Static Exchange Evaluation
    int target = PIECE_VALUES[board.pieceAt(move.to)];
    int gain[32];
    int depth = 0;
    uint64_t occ = board.occupancy[BOTH];
    uint64_t attackers = getAttackers(move.from, occ);
    int side = board.pieceAtColor(move.from);

    gain[0] = target;
    while (true) {
        depth++;
        gain[depth] = PIECEValues[leastAttacker(attackers)] - gain[depth-1];
        attackers ^= leastAttackerBB(attackers);
        side ^= 1;
        if (gain[depth] < 0) break;
    }
    while (--depth) {
        gain[depth-1] = -std::max(-gain[depth-1], gain[depth]);
    }
    return gain[0];
}

};