From e1d216f06bcb39df88f1a14845dd115d250f474a Mon Sep 17 00:00:00 2001 From: Arthur Bols Date: Thu, 22 Dec 2022 15:58:11 +0100 Subject: [PATCH] [BitBoard] Add bishop and pawn methods --- BitBoard.cpp | 33 ++++++++++++++++++++++++++++++--- BitBoard.hpp | 29 +++++++++++++++++++++++++---- 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/BitBoard.cpp b/BitBoard.cpp index 4c2d793..91de98f 100644 --- a/BitBoard.cpp +++ b/BitBoard.cpp @@ -25,9 +25,6 @@ std::ostream &operator<<(std::ostream &os, const BitBoard &board) { return os; } -BitBoard BitBoard::getRank(int r) { - return (genShift(1ULL, (r + 1) * 8) - 1) & genShift(~1ULL, r * 8 - 1); -} BitBoard BitBoard::genShift(BitBoard x, const int s) { return (s < 0) ? (x >> -s) : (s > 63) ? x : (x << s); } @@ -58,5 +55,35 @@ BitBoard BitBoard::kingAttacks(const BitBoard bb) { result ^= bb; return result; +} +BitBoard BitBoard::bishopAttacks(BitBoard bishops, BitBoard empty) { + BitBoard result = 0; + BitBoard diag1 = bishops; + BitBoard diag2 = bishops; + empty ^= bishops; + + for (int i = 0; i < 7; i++) { + result |= (diag1 | diag2); + diag1 = (diag1.northWest() | diag1.southEast()) & empty; + diag2 = (diag2.northEast() | diag2.southWest()) & empty; + } + + return (result | diag1.northWest() | diag1.southEast() | diag2.northEast() | diag2.southWest()) & ~bishops; +} + +BitBoard BitBoard::pawnNorthAttacks(BitBoard pawns, BitBoard targets) { + return (pawns.northEast() | pawns.northWest()) & targets; +} +BitBoard BitBoard::pawnNorthMoves(BitBoard pawns, BitBoard empty) { + pawns = pawns.north() & empty; + return (pawns | (pawns.north() & Rank4)) & empty; +} + +BitBoard BitBoard::pawnSouthAttacks(BitBoard pawns, BitBoard targets) { + return (pawns.southEast() | pawns.southWest()) & targets; +} +BitBoard BitBoard::pawnSouthMoves(BitBoard pawns, BitBoard empty) { + pawns = pawns.south() & empty; + return (pawns | (pawns.south() & Rank5)) & empty; } \ No newline at end of file diff --git a/BitBoard.hpp b/BitBoard.hpp index 6ae9fd2..2fe09a4 100644 --- a/BitBoard.hpp +++ b/BitBoard.hpp @@ -14,6 +14,14 @@ enum DefinedBoards : uint64_t { FFile = AFile << 5, GFile = AFile << 6, HFile = AFile << 7, + Rank1 = 0x00000000000000FF, + Rank2 = Rank1 << 8, + Rank3 = Rank1 << 16, + Rank4 = Rank1 << 24, + Rank5 = Rank1 << 32, + Rank6 = Rank1 << 40, + Rank7 = Rank1 << 48, + Rank8 = Rank1 << 56, WhiteCastlingRank = (1ULL << A2) - 1, BlackCastlingRank = (~1ULL << H7), CastlingRanks = WhiteCastlingRank | BlackCastlingRank, @@ -98,15 +106,21 @@ public: BitBoard southFill() const; BitBoard fileFill() const; + static BitBoard bishopAttacks(BitBoard pos, BitBoard empty); static BitBoard kingAttacks(BitBoard bb); + static BitBoard pawnNorthAttacks(BitBoard pawns, BitBoard targets); + static BitBoard pawnSouthAttacks(BitBoard pawns, BitBoard targets); + + static BitBoard pawnNorthMoves(BitBoard pawns, BitBoard empty); + static BitBoard pawnSouthMoves(BitBoard pawns, BitBoard empty); + static BitBoard fromIndex(unsigned i); - static BitBoard getRank(int r); - // Returns the number of trailing 0-bits in b. // WARN: Check for 0! - int lsb() const; + unsigned lsb() const; + unsigned pop(); private: U64 mBoard = {}; @@ -255,8 +269,15 @@ inline BitBoard BitBoard::southEast() const { inline BitBoard BitBoard::southWest() const { return (mBoard >> 9) & ~HFile; } -inline int BitBoard::lsb() const { + +inline unsigned BitBoard::lsb() const { return __builtin_ctzll(mBoard); } +inline unsigned BitBoard::pop() { + unsigned i = lsb(); + mBoard &= mBoard - 1; + + return i; +} #endif //CHESS_ENGINE_BITBOARD_HPP