#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 Copy
void init() {
pieces.fill(0);
// White pieces
pieces = 0x000000000000FF00ULL;
pieces = 0x0000000000000042ULL;
pieces = 0x0000000000000024ULL;
pieces = 0x0000000000000081ULL;
pieces = 0x0000000000000010ULL;
pieces = 0x0000000000000008ULL;
// Black pieces
pieces = 0x00FF000000000000ULL;
pieces = 0x4200000000000000ULL;
pieces = 0x2400000000000000ULL;
pieces = 0x8100000000000000ULL;
pieces = 0x1000000000000000ULL;
pieces = 0x0800000000000000ULL;
occupancy = 0x000000000000FFFFULL;
occupancy = 0xFFFF000000000000ULL;
occupancy = occupancy | occupancy;
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;
// 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;
// Pawn attacks
if (byColor == WHITE) {
attackers |= ((knightMask >> 7) & ~0x0101010101010101ULL) & pieces;
attackers |= ((knightMask >> 9) & ~0x8080808080808080ULL) & pieces;
} else {
attackers |= ((knightMask << 7) & ~0x8080808080808080ULL) & pieces;
attackers |= ((knightMask << 9) & ~0x0101010101010101ULL) & pieces;
}
return attackers != 0;
}
uint64_t computeHash() const {
uint64_t hash = 0;
for (int i = 0; i < 12; i++) {
uint64_t bb = pieces;
while (bb) {
int sq = __builtin_ctzll(bb);
hash ^= Zobrist::pieceKeys;
bb &= bb - 1;
}
}
hash ^= Zobrist::castlingKeys;
hash ^= Zobrist::sideKey;
if (enPassantSquare >= 0) hash ^= Zobrist::epKeys;
return hash;
}};