tinycodinglm / chess /chess-0076.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 12)
3c88a0f verified
|
Raw History Blame Contribute Delete
3.89 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 a bitboard-based chess board representation in C++ with full move generation

Assistant

#include <cstdint>
#include <array>

enum Piece { WP, WN, WB, WR, WQ, WK, BP, BN, BB, BR, BQ, BK, NONE };
enum Color { WHITE, BLACK };

struct BitboardBoard {
std::array<uint64_t, 12> pieces; // one per piece type
uint64_t occupancy[3]; // white, black, both
Color sideToMove;
int castlingRights; // KQkq bitmask
int enPassantSquare;
int halfmoveClock;
int fullmoveNumber;

markdown
void init() {
    pieces.fill(0);
    // White pieces
    pieces[WP] = 0x000000000000FF00ULL;
    pieces[WN] = 0x0000000000000042ULL;
    pieces[WB] = 0x0000000000000024ULL;
    pieces[WR] = 0x0000000000000081ULL;
    pieces[WQ] = 0x0000000000000010ULL;
    pieces[WK] = 0x0000000000000008ULL;
    // Black pieces
    pieces[BP] = 0x00FF000000000000ULL;
    pieces[BN] = 0x4200000000000000ULL;
    pieces[BB] = 0x2400000000000000ULL;
    pieces[BR] = 0x8100000000000000ULL;
    pieces[BQ] = 0x1000000000000000ULL;
    pieces[BK] = 0x0800000000000000ULL;
    occupancy[WHITE] = 0x000000000000FFFFULL;
    occupancy[BLACK] = 0xFFFF000000000000ULL;
    occupancy[BOTH] = occupancy[WHITE] | occupancy[BLACK];
    sideToMove = WHITE;
    castlingRights = 0xF;
    enPassantSquare = -1;
    halfmoveClock = 0;
    fullmoveNumber = 1;
}

bool isSquareAttacked(int sq, Color byColor) const {
    uint64_t attackers = 0;
    uint64_t occ = occupancy[BOTH];
    // Knight attacks
    uint64_t knightMask = 1ULL << sq;
    uint64_t knightMoves = ((knightMask << 17) & ~0x0101010101010101ULL) |
                           ((knightMask << 15) & ~0x8080808080808080ULL) |
                           ((knightMask << 10) & ~0x0303030303030303ULL) |
                           ((knightMask << 6)  & ~0xC0C0C0C0C0C0C0C0ULL) |
                           ((knightMask >> 6)  & ~0x0303030303030303ULL) |
                           ((knightMask >> 10) & ~0xC0C0C0C0C0C0C0C0ULL) |
                           ((knightMask >> 15) & ~0x0101010101010101ULL) |
                           ((knightMask >> 17) & ~0x8080808080808080ULL);
    attackers |= knightMoves & pieces[byColor == WHITE ? BN : KN];
    // Pawn attacks
    if (byColor == WHITE) {
        attackers |= ((knightMask >> 7) & ~0x0101010101010101ULL) & pieces[WP];
        attackers |= ((knightMask >> 9) & ~0x8080808080808080ULL) & pieces[WP];
    } else {
        attackers |= ((knightMask << 7) & ~0x8080808080808080ULL) & pieces[BP];
        attackers |= ((knightMask << 9) & ~0x0101010101010101ULL) & pieces[BP];
    }
    return attackers != 0;
}

uint64_t computeHash() const {
    uint64_t hash = 0;
    for (int i = 0; i < 12; i++) {
        uint64_t bb = pieces[i];
        while (bb) {
            int sq = __builtin_ctzll(bb);
            hash ^= Zobrist::pieceKeys[i][sq];
            bb &= bb - 1;
        }
    }
    hash ^= Zobrist::castlingKeys[castlingRights];
    hash ^= Zobrist::sideKey;
    if (enPassantSquare >= 0) hash ^= Zobrist::epKeys[enPassantSquare % 8];
    return hash;
}

};