Code: Select all
#include <iostream>
#include <cstdint>
struct Square {
int x;
Square(int x) : x(x) { }
};
std::ostream &operator<<(std::ostream &os, Square sq) {
if (sq.x >= 0 && sq.x < 64)
os << char('h' - (sq.x % 8)) << char('1' + (sq.x / 8));
else
os << "??";
return os;
}
struct Bitboard {
struct Iterator {
uint64_t x;
Iterator(uint64_t x) : x(x) { }
Square operator*() const { return Square(__builtin_ctzll(x)); }
Iterator &operator++() { x &= x - 1; return *this; }
bool operator==(Iterator other) { return x == other.x; }
bool operator!=(Iterator other) { return x != other.x; }
};
uint64_t x;
Bitboard(uint64_t x) : x(x) { }
Iterator begin() const { return Iterator(x); }
Iterator end() const { return Iterator(0ull); }
};
Bitboard operator&(Bitboard bb1, Bitboard bb2) {
return Bitboard(bb1.x & bb2.x);
}
Bitboard South(Bitboard bb) { return Bitboard(bb.x >> 8); }
Bitboard North(Bitboard bb) { return Bitboard(bb.x << 8); }
Bitboard East(Bitboard bb) { return Bitboard((bb.x & 0xfefefefefefefefeull) >> 1); }
Bitboard West(Bitboard bb) { return Bitboard((bb.x & 0x7f7f7f7f7f7f7f7full) << 1); }
Square South(Square sq) { return Square(sq.x - 8); }
Square North(Square sq) { return Square(sq.x + 8); }
Square East(Square sq) { return Square(sq.x - 1); }
Square West(Square sq) { return Square(sq.x + 1); }
int main() {
Bitboard white_pawns = 0x000000000000ff00ull, empties = 0x0000ffffffff0000ull;
for(auto from_square : white_pawns & South(empties))
std::cout << from_square << North(from_square) << '\n';
}