[BitBoard] Add bishop and pawn methods

This commit is contained in:
2022-12-22 15:58:11 +01:00
parent e620e6f5cb
commit e1d216f06b
2 changed files with 55 additions and 7 deletions

View File

@@ -25,9 +25,6 @@ std::ostream &operator<<(std::ostream &os, const BitBoard &board) {
return os; 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) { BitBoard BitBoard::genShift(BitBoard x, const int s) {
return (s < 0) ? (x >> -s) : (s > 63) ? x : (x << s); return (s < 0) ? (x >> -s) : (s > 63) ? x : (x << s);
} }
@@ -58,5 +55,35 @@ BitBoard BitBoard::kingAttacks(const BitBoard bb) {
result ^= bb; result ^= bb;
return result; 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;
} }

View File

@@ -14,6 +14,14 @@ enum DefinedBoards : uint64_t {
FFile = AFile << 5, FFile = AFile << 5,
GFile = AFile << 6, GFile = AFile << 6,
HFile = AFile << 7, 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, WhiteCastlingRank = (1ULL << A2) - 1,
BlackCastlingRank = (~1ULL << H7), BlackCastlingRank = (~1ULL << H7),
CastlingRanks = WhiteCastlingRank | BlackCastlingRank, CastlingRanks = WhiteCastlingRank | BlackCastlingRank,
@@ -98,15 +106,21 @@ public:
BitBoard southFill() const; BitBoard southFill() const;
BitBoard fileFill() const; BitBoard fileFill() const;
static BitBoard bishopAttacks(BitBoard pos, BitBoard empty);
static BitBoard kingAttacks(BitBoard bb); 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 fromIndex(unsigned i);
static BitBoard getRank(int r);
// Returns the number of trailing 0-bits in b. // Returns the number of trailing 0-bits in b.
// WARN: Check for 0! // WARN: Check for 0!
int lsb() const; unsigned lsb() const;
unsigned pop();
private: private:
U64 mBoard = {}; U64 mBoard = {};
@@ -255,8 +269,15 @@ inline BitBoard BitBoard::southEast() const {
inline BitBoard BitBoard::southWest() const { inline BitBoard BitBoard::southWest() const {
return (mBoard >> 9) & ~HFile; return (mBoard >> 9) & ~HFile;
} }
inline int BitBoard::lsb() const {
inline unsigned BitBoard::lsb() const {
return __builtin_ctzll(mBoard); return __builtin_ctzll(mBoard);
} }
inline unsigned BitBoard::pop() {
unsigned i = lsb();
mBoard &= mBoard - 1;
return i;
}
#endif //CHESS_ENGINE_BITBOARD_HPP #endif //CHESS_ENGINE_BITBOARD_HPP