File size: 3,894 Bytes
3c88a0f
 
 
 
1
2
3
4
5
{"type": "session", "harness": "opencode", "id": "chess-0076", "name": "Implement a bitboard-based chess board representation in C++ with full move generation"}
{"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 a bitboard-based chess board representation in C++ with full move generation"}}
{"type": "message", "message": {"role": "assistant", "content": "#include <cstdint>\n#include <array>\n\nenum Piece { WP, WN, WB, WR, WQ, WK, BP, BN, BB, BR, BQ, BK, NONE };\nenum Color { WHITE, BLACK };\n\nstruct BitboardBoard {\n    std::array<uint64_t, 12> pieces;  // one per piece type\n    uint64_t occupancy[3];            // white, black, both\n    Color sideToMove;\n    int castlingRights;               // KQkq bitmask\n    int enPassantSquare;\n    int halfmoveClock;\n    int fullmoveNumber;\n\n    void init() {\n        pieces.fill(0);\n        // White pieces\n        pieces[WP] = 0x000000000000FF00ULL;\n        pieces[WN] = 0x0000000000000042ULL;\n        pieces[WB] = 0x0000000000000024ULL;\n        pieces[WR] = 0x0000000000000081ULL;\n        pieces[WQ] = 0x0000000000000010ULL;\n        pieces[WK] = 0x0000000000000008ULL;\n        // Black pieces\n        pieces[BP] = 0x00FF000000000000ULL;\n        pieces[BN] = 0x4200000000000000ULL;\n        pieces[BB] = 0x2400000000000000ULL;\n        pieces[BR] = 0x8100000000000000ULL;\n        pieces[BQ] = 0x1000000000000000ULL;\n        pieces[BK] = 0x0800000000000000ULL;\n        occupancy[WHITE] = 0x000000000000FFFFULL;\n        occupancy[BLACK] = 0xFFFF000000000000ULL;\n        occupancy[BOTH] = occupancy[WHITE] | occupancy[BLACK];\n        sideToMove = WHITE;\n        castlingRights = 0xF;\n        enPassantSquare = -1;\n        halfmoveClock = 0;\n        fullmoveNumber = 1;\n    }\n\n    bool isSquareAttacked(int sq, Color byColor) const {\n        uint64_t attackers = 0;\n        uint64_t occ = occupancy[BOTH];\n        // Knight attacks\n        uint64_t knightMask = 1ULL << sq;\n        uint64_t knightMoves = ((knightMask << 17) & ~0x0101010101010101ULL) |\n                               ((knightMask << 15) & ~0x8080808080808080ULL) |\n                               ((knightMask << 10) & ~0x0303030303030303ULL) |\n                               ((knightMask << 6)  & ~0xC0C0C0C0C0C0C0C0ULL) |\n                               ((knightMask >> 6)  & ~0x0303030303030303ULL) |\n                               ((knightMask >> 10) & ~0xC0C0C0C0C0C0C0C0ULL) |\n                               ((knightMask >> 15) & ~0x0101010101010101ULL) |\n                               ((knightMask >> 17) & ~0x8080808080808080ULL);\n        attackers |= knightMoves & pieces[byColor == WHITE ? BN : KN];\n        // Pawn attacks\n        if (byColor == WHITE) {\n            attackers |= ((knightMask >> 7) & ~0x0101010101010101ULL) & pieces[WP];\n            attackers |= ((knightMask >> 9) & ~0x8080808080808080ULL) & pieces[WP];\n        } else {\n            attackers |= ((knightMask << 7) & ~0x8080808080808080ULL) & pieces[BP];\n            attackers |= ((knightMask << 9) & ~0x0101010101010101ULL) & pieces[BP];\n        }\n        return attackers != 0;\n    }\n\n    uint64_t computeHash() const {\n        uint64_t hash = 0;\n        for (int i = 0; i < 12; i++) {\n            uint64_t bb = pieces[i];\n            while (bb) {\n                int sq = __builtin_ctzll(bb);\n                hash ^= Zobrist::pieceKeys[i][sq];\n                bb &= bb - 1;\n            }\n        }\n        hash ^= Zobrist::castlingKeys[castlingRights];\n        hash ^= Zobrist::sideKey;\n        if (enPassantSquare >= 0) hash ^= Zobrist::epKeys[enPassantSquare % 8];\n        return hash;\n    }\n};"}}