tinycodinglm / chess /chess-0103.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 12)
3c88a0f verified
|
Raw History Blame Contribute Delete
3.52 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 Chess960 (Fischer Random) castling rules for a chess engine

Assistant

#include <cstdint>

struct Chess960 {
// Generate Chess960 starting position from number 0-959
static void positionFromNumber(int number, int* backRank) {
// Two bishops
int lightBishop = number % 4;
int darkBishop = (number / 4) % 4;
backRank[0] = darkBishop * 2 + 1; // dark-squared bishop
backRank[7] = lightBishop * 2; // light-squared bishop

markdown
// Queen and knights
    int n1 = (number / 16) % 6;
    int n2 = (number / 96) % 6;
    int n3 = (number / 576) % 10;

    // Place pieces avoiding occupied squares
    int pieces[5];
    pieces[0] = n3;           // queen
    pieces[1] = n1 % 5;       // knight 1
    pieces[2] = (n1 + 1) % 5; // knight 2
    pieces[3] = n2 % 4;       // rook 1
    pieces[4] = (n2 + 1) % 4; // rook 2

    int empty[5], ei = 0;
    for (int i = 0; i < 8; i++) {
        if (i != backRank[0] && i != backRank[7]) empty[ei++] = i;
    }

    backRank[empty[pieces[0]]] = 4;  // queen
    backRank[empty[pieces[1]]] = 2;  // knight
    backRank[empty[pieces[2]]] = 2;  // knight
    backRank[empty[pieces[3]]] = 1;  // rook
    backRank[empty[pieces[4]]] = 1;  // rook
    backRank[4] = 6;  // king
}

// Chess960 castling: king and rook end up on standard castling squares
static void chess960Castling(int kingFrom, int rookFrom,
                              int& kingTo, int& rookTo) {
    // White: king goes to g1 (6), rook goes to f1 (5)
    // Black: king goes to g8 (62), rook goes to f8 (61)
    bool isKingside = rookFrom > kingFrom;

    if (isKingside) {
        kingTo = 6;   // g1
        rookTo = 5;   // f1
    } else {
        kingTo = 2;   // c1
        rookTo = 3;   // d1
    }

    // Ensure path is clear and king not in check
    int minSq = std::min(kingFrom, kingTo);
    int maxSq = std::max(kingFrom, kingTo);
    for (int sq = minSq; sq <= maxSq; sq++) {
        if (sq != kingFrom && sq != rookFrom) {
            if (isSquareOccupied(sq)) return; // illegal
        }
    }
    // Check that king doesn't pass through check
    for (int sq = std::min(kingFrom, std::max(kingTo, 4));
         sq <= std::max(kingFrom, std::max(kingTo, 4)); sq++) {
        if (isSquareAttacked(sq, BLACK)) return; // illegal
    }
}

static uint64_t getKingAttackMask(int sq, uint64_t occ) {
    uint64_t mask = 1ULL << sq;
    uint64_t attacks = ((mask << 8) | (mask >> 8) |
                       ((mask << 7) & ~0x8080808080808080ULL) |
                       ((mask << 9) & ~0x0101010101010101ULL) |
                       ((mask >> 7) & ~0x0101010101010101ULL) |
                       ((mask >> 9) & ~0x8080808080808080ULL));
    return attacks;
}

};