diff --git a/source/bitboards.cpp b/source/bitboards.cpp index 9a23c3a..827d061 100644 --- a/source/bitboards.cpp +++ b/source/bitboards.cpp @@ -49,7 +49,7 @@ int Bitboard::popCount() const { return count; } -// LS1B = least significat first bit +// LS1B = least significant first bit int Bitboard::getLS1BIndex() const { if (m_board) { // count trailing bits before LS1B @@ -59,6 +59,23 @@ int Bitboard::getLS1BIndex() const { return -1; } +Bitboard Bitboard::getOccupancy(int index, Bitboard attackMask) { + Bitboard occupancy; + int squareNum = attackMask.popCount(); + + for (int count = 0; count < squareNum; count++) { + int square = attackMask.getLS1BIndex(); + + attackMask.PopBit(square); + + if (index & (1 << count)) { + occupancy |= (1ULL << square); + } + } + + return occupancy; +} + // Operator overloading for easier comparisons Bitboard Bitboard::operator&(const Bitboard& other) const { return m_board & other.m_board; diff --git a/source/bitboards.h b/source/bitboards.h index 3e7ec7b..e947170 100644 --- a/source/bitboards.h +++ b/source/bitboards.h @@ -20,6 +20,7 @@ public: int popCount() const; int getLS1BIndex() const; + static Bitboard getOccupancy(int index, Bitboard attackMask); static Bitboard GetSquare(int square) { return (1ULL << square); diff --git a/source/main.cpp b/source/main.cpp index 0247e3c..220fb6b 100644 --- a/source/main.cpp +++ b/source/main.cpp @@ -9,5 +9,7 @@ int main() { Board board; board.Init(); + Bitboard::getOccupancy(4095, maskRookAttacks(e5)).PrintBoard(); + return 0; } \ No newline at end of file