added operator overloading to Bitboard class
This commit is contained in:
1 parent
f6c2b6a881
commit
4d8a7f6a2e
5 files changed
+77
-45
No files matched your search
+30
-7
@@ -23,20 +23,16 @@ void Bitboard::PrintBoard() const {
|
|||||||
std::cout << " Board: 0x" << std::hex << m_board << '\n';
|
std::cout << " Board: 0x" << std::hex << m_board << '\n';
|
||||||
}
|
}
|
||||||
|
|
||||||
U64 Bitboard::GetBoard() const {
|
|
||||||
return m_board;
|
|
||||||
}
|
|
||||||
|
|
||||||
bool Bitboard::IsSet(int square) const {
|
bool Bitboard::IsSet(int square) const {
|
||||||
return GetSquare(square).GetBoard() & m_board;
|
return GetSquare(square) & m_board;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Bitboard::SetBit(int square) {
|
void Bitboard::SetBit(int square) {
|
||||||
m_board |= GetSquare(square).GetBoard();
|
m_board |= GetSquare(square);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Bitboard::PopBit(int square) {
|
void Bitboard::PopBit(int square) {
|
||||||
m_board &= ~GetSquare(square).GetBoard();
|
m_board &= ~GetSquare(square);
|
||||||
}
|
}
|
||||||
|
|
||||||
int Bitboard::PopCount() const {
|
int Bitboard::PopCount() const {
|
||||||
@@ -84,6 +80,19 @@ Bitboard Bitboard::operator*(const Bitboard& other) const {
|
|||||||
return m_board * other.m_board;
|
return m_board * other.m_board;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Bitboard Bitboard::operator&(const uint64_t& other) const {
|
||||||
|
return m_board & other;
|
||||||
|
}
|
||||||
|
Bitboard Bitboard::operator|(const uint64_t& other) const {
|
||||||
|
return m_board | other;
|
||||||
|
}
|
||||||
|
Bitboard Bitboard::operator^(const uint64_t& other) const {
|
||||||
|
return m_board ^ other;
|
||||||
|
}
|
||||||
|
Bitboard Bitboard::operator*(const uint64_t& other) const {
|
||||||
|
return m_board * other;
|
||||||
|
}
|
||||||
|
|
||||||
Bitboard Bitboard::operator&=(const Bitboard& other) {
|
Bitboard Bitboard::operator&=(const Bitboard& other) {
|
||||||
return m_board &= other.m_board;
|
return m_board &= other.m_board;
|
||||||
}
|
}
|
||||||
@@ -94,6 +103,16 @@ Bitboard Bitboard::operator^=(const Bitboard& other) {
|
|||||||
return m_board ^= other.m_board;
|
return m_board ^= other.m_board;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Bitboard Bitboard::operator&=(const uint64_t& other) {
|
||||||
|
return m_board &= other;
|
||||||
|
}
|
||||||
|
Bitboard Bitboard::operator|=(const uint64_t& other) {
|
||||||
|
return m_board |= other;
|
||||||
|
}
|
||||||
|
Bitboard Bitboard::operator^=(const uint64_t& other) {
|
||||||
|
return m_board ^= other;
|
||||||
|
}
|
||||||
|
|
||||||
Bitboard Bitboard::operator<<(int other) {
|
Bitboard Bitboard::operator<<(int other) {
|
||||||
if (other > 0) {
|
if (other > 0) {
|
||||||
return m_board << other;
|
return m_board << other;
|
||||||
@@ -114,3 +133,7 @@ Bitboard Bitboard::operator>>(int other) {
|
|||||||
Bitboard Bitboard::operator~() {
|
Bitboard Bitboard::operator~() {
|
||||||
return ~(m_board);
|
return ~(m_board);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Bitboard::operator uint64_t() {
|
||||||
|
return m_board;
|
||||||
|
}
|
||||||
+10
-1
@@ -12,7 +12,6 @@ public:
|
|||||||
Bitboard(const U64& board)
|
Bitboard(const U64& board)
|
||||||
: m_board(board) {}
|
: m_board(board) {}
|
||||||
|
|
||||||
U64 GetBoard() const;
|
|
||||||
bool IsSet(int square) const;
|
bool IsSet(int square) const;
|
||||||
void SetBit(int square);
|
void SetBit(int square);
|
||||||
void PopBit(int square);
|
void PopBit(int square);
|
||||||
@@ -30,12 +29,22 @@ public:
|
|||||||
Bitboard operator|(const Bitboard& other) const;
|
Bitboard operator|(const Bitboard& other) const;
|
||||||
Bitboard operator^(const Bitboard& other) const;
|
Bitboard operator^(const Bitboard& other) const;
|
||||||
Bitboard operator*(const Bitboard& other) const;
|
Bitboard operator*(const Bitboard& other) const;
|
||||||
|
|
||||||
|
Bitboard operator&(const uint64_t& other) const;
|
||||||
|
Bitboard operator|(const uint64_t& other) const;
|
||||||
|
Bitboard operator^(const uint64_t& other) const;
|
||||||
|
Bitboard operator*(const uint64_t& other) const;
|
||||||
|
|
||||||
Bitboard operator&=(const Bitboard& other);
|
Bitboard operator&=(const Bitboard& other);
|
||||||
Bitboard operator|=(const Bitboard& other);
|
Bitboard operator|=(const Bitboard& other);
|
||||||
Bitboard operator^=(const Bitboard& other);
|
Bitboard operator^=(const Bitboard& other);
|
||||||
|
|
||||||
|
Bitboard operator&=(const uint64_t& other);
|
||||||
|
Bitboard operator|=(const uint64_t& other);
|
||||||
|
Bitboard operator^=(const uint64_t& other);
|
||||||
|
|
||||||
Bitboard operator<<(int other);
|
Bitboard operator<<(int other);
|
||||||
Bitboard operator>>(int other);
|
Bitboard operator>>(int other);
|
||||||
Bitboard operator~();
|
Bitboard operator~();
|
||||||
|
operator uint64_t();
|
||||||
};
|
};
|
||||||
+1
-1
@@ -277,7 +277,7 @@ U64 Board::GetAttackMaps(bool side) {
|
|||||||
bool Board::InCheck(bool side) {
|
bool Board::InCheck(bool side) {
|
||||||
Bitboard myKingSquare = colors[side] & pieces[King];
|
Bitboard myKingSquare = colors[side] & pieces[King];
|
||||||
|
|
||||||
return GetAttackMaps(!side) & myKingSquare.GetBoard();
|
return GetAttackMaps(!side) & myKingSquare;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Board::SetPiece(int piece, int square, bool color) {
|
void Board::SetPiece(int piece, int square, bool color) {
|
||||||
|
|||||||
+1
-1
@@ -13,7 +13,7 @@ static int GetPositionalScore(Board &board, int type, int color) {
|
|||||||
|
|
||||||
Bitboard pieces = board.pieces[type] & board.colors[color];
|
Bitboard pieces = board.pieces[type] & board.colors[color];
|
||||||
|
|
||||||
while (pieces.GetBoard()) {
|
while (pieces) {
|
||||||
int square = pieces.getLS1BIndex();
|
int square = pieces.getLS1BIndex();
|
||||||
|
|
||||||
// White
|
// White
|
||||||
|
|||||||
+35
-35
@@ -22,13 +22,13 @@ Bitboard maskPawnAttacks(int side, int square) {
|
|||||||
|
|
||||||
// White
|
// White
|
||||||
if (!side) {
|
if (!side) {
|
||||||
if (((attacker << noEa) & notAfile).GetBoard()) attacks |= (attacker << noEa);
|
if (((attacker << noEa) & notAfile)) attacks |= (attacker << noEa);
|
||||||
if (((attacker << noWe) & notHfile).GetBoard()) attacks |= (attacker << noWe);
|
if (((attacker << noWe) & notHfile)) attacks |= (attacker << noWe);
|
||||||
}
|
}
|
||||||
// Black
|
// Black
|
||||||
else {
|
else {
|
||||||
if (((attacker << soEa) & notAfile).GetBoard()) attacks |= (attacker << soEa);
|
if (((attacker << soEa) & notAfile)) attacks |= (attacker << soEa);
|
||||||
if (((attacker << soWe) & notHfile).GetBoard()) attacks |= (attacker << soWe);
|
if (((attacker << soWe) & notHfile)) attacks |= (attacker << soWe);
|
||||||
}
|
}
|
||||||
return attacks;
|
return attacks;
|
||||||
}
|
}
|
||||||
@@ -37,15 +37,15 @@ Bitboard maskKnightAttacks(int square) {
|
|||||||
Bitboard attacks;
|
Bitboard attacks;
|
||||||
Bitboard attacker = Bitboard::GetSquare(square);
|
Bitboard attacker = Bitboard::GetSquare(square);
|
||||||
|
|
||||||
if (((attacker << noEa + north) & notAfile).GetBoard()) attacks |= (attacker << noEa + north);
|
if (((attacker << noEa + north) & notAfile)) attacks |= (attacker << noEa + north);
|
||||||
if (((attacker << noWe + north) & notHfile).GetBoard()) attacks |= (attacker << noWe + north);
|
if (((attacker << noWe + north) & notHfile)) attacks |= (attacker << noWe + north);
|
||||||
if (((attacker << noWe + west) & notHGfile).GetBoard()) attacks |= (attacker << noWe + west);
|
if (((attacker << noWe + west) & notHGfile)) attacks |= (attacker << noWe + west);
|
||||||
if (((attacker << noEa + east) & notABfile).GetBoard()) attacks |= (attacker << noEa + east);
|
if (((attacker << noEa + east) & notABfile)) attacks |= (attacker << noEa + east);
|
||||||
|
|
||||||
if (((attacker << soWe + south) & notHfile).GetBoard()) attacks |= (attacker << soWe + south);
|
if (((attacker << soWe + south) & notHfile)) attacks |= (attacker << soWe + south);
|
||||||
if (((attacker << soEa + south) & notAfile).GetBoard()) attacks |= (attacker << soEa + south);
|
if (((attacker << soEa + south) & notAfile)) attacks |= (attacker << soEa + south);
|
||||||
if (((attacker << soEa + east) & notABfile).GetBoard()) attacks |= (attacker << soEa + east);
|
if (((attacker << soEa + east) & notABfile)) attacks |= (attacker << soEa + east);
|
||||||
if (((attacker << soWe + west) & notHGfile).GetBoard()) attacks |= (attacker << soWe + west);
|
if (((attacker << soWe + west) & notHGfile)) attacks |= (attacker << soWe + west);
|
||||||
|
|
||||||
return attacks;
|
return attacks;
|
||||||
}
|
}
|
||||||
@@ -57,12 +57,12 @@ Bitboard maskKingAttacks(int square) {
|
|||||||
attacks |= (attacker << north);
|
attacks |= (attacker << north);
|
||||||
attacks |= (attacker << south);
|
attacks |= (attacker << south);
|
||||||
|
|
||||||
if (((attacker << west) & notHfile).GetBoard()) attacks |= (attacker << west);
|
if (((attacker << west) & notHfile)) attacks |= (attacker << west);
|
||||||
if (((attacker << soWe) & notHfile).GetBoard()) attacks |= (attacker << soWe);
|
if (((attacker << soWe) & notHfile)) attacks |= (attacker << soWe);
|
||||||
if (((attacker << soEa) & notAfile).GetBoard()) attacks |= (attacker << soEa);
|
if (((attacker << soEa) & notAfile)) attacks |= (attacker << soEa);
|
||||||
if (((attacker << east) & notAfile).GetBoard()) attacks |= (attacker << east);
|
if (((attacker << east) & notAfile)) attacks |= (attacker << east);
|
||||||
if (((attacker << noEa) & notAfile).GetBoard()) attacks |= (attacker << noEa);
|
if (((attacker << noEa) & notAfile)) attacks |= (attacker << noEa);
|
||||||
if (((attacker << noWe) & notHfile).GetBoard()) attacks |= (attacker << noWe);
|
if (((attacker << noWe) & notHfile)) attacks |= (attacker << noWe);
|
||||||
|
|
||||||
return attacks;
|
return attacks;
|
||||||
}
|
}
|
||||||
@@ -140,25 +140,25 @@ Bitboard bishopAttacksOnTheFly(int square, Bitboard block) {
|
|||||||
for (int r = tr + 1, f = tf - 1; r <= 7 && f >= 0; r++, f--) {
|
for (int r = tr + 1, f = tf - 1; r <= 7 && f >= 0; r++, f--) {
|
||||||
currentSquare = (1ULL << (r * 8 + f));
|
currentSquare = (1ULL << (r * 8 + f));
|
||||||
attacks |= currentSquare;
|
attacks |= currentSquare;
|
||||||
if ((currentSquare & block).GetBoard()) break;
|
if ((currentSquare & block)) break;
|
||||||
}
|
}
|
||||||
// North East
|
// North East
|
||||||
for (int r = tr + 1, f = tf + 1; r <= 7 && f <= 7; r++, f++) {
|
for (int r = tr + 1, f = tf + 1; r <= 7 && f <= 7; r++, f++) {
|
||||||
currentSquare = (1ULL << (r * 8 + f));
|
currentSquare = (1ULL << (r * 8 + f));
|
||||||
attacks |= currentSquare;
|
attacks |= currentSquare;
|
||||||
if ((currentSquare & block).GetBoard()) break;
|
if ((currentSquare & block)) break;
|
||||||
}
|
}
|
||||||
//South West
|
//South West
|
||||||
for (int r = tr - 1, f = tf - 1; r >= 0 && f >= 0; r--, f--) {
|
for (int r = tr - 1, f = tf - 1; r >= 0 && f >= 0; r--, f--) {
|
||||||
currentSquare = (1ULL << (r * 8 + f));
|
currentSquare = (1ULL << (r * 8 + f));
|
||||||
attacks |= currentSquare;
|
attacks |= currentSquare;
|
||||||
if ((currentSquare & block).GetBoard()) break;
|
if ((currentSquare & block)) break;
|
||||||
}
|
}
|
||||||
//South East
|
//South East
|
||||||
for (int r = tr - 1, f = tf + 1; r >= 0 && f <= 7; r--, f++) {
|
for (int r = tr - 1, f = tf + 1; r >= 0 && f <= 7; r--, f++) {
|
||||||
currentSquare = (1ULL << (r * 8 + f));
|
currentSquare = (1ULL << (r * 8 + f));
|
||||||
attacks |= currentSquare;
|
attacks |= currentSquare;
|
||||||
if ((currentSquare & block).GetBoard()) break;
|
if ((currentSquare & block)) break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return attacks;
|
return attacks;
|
||||||
@@ -179,25 +179,25 @@ Bitboard rookAttacksOnTheFly(int square, Bitboard block) {
|
|||||||
for (int r = tr + 1; r <= 7; r++) {
|
for (int r = tr + 1; r <= 7; r++) {
|
||||||
currentSquare = (1ULL << (r * 8 + tf));
|
currentSquare = (1ULL << (r * 8 + tf));
|
||||||
attacks |= currentSquare;
|
attacks |= currentSquare;
|
||||||
if ((currentSquare & block).GetBoard()) break;
|
if ((currentSquare & block)) break;
|
||||||
}
|
}
|
||||||
// South
|
// South
|
||||||
for (int r = tr - 1; r >= 0; r--) {
|
for (int r = tr - 1; r >= 0; r--) {
|
||||||
currentSquare = (1ULL << (r * 8 + tf));
|
currentSquare = (1ULL << (r * 8 + tf));
|
||||||
attacks |= currentSquare;
|
attacks |= currentSquare;
|
||||||
if ((currentSquare & block).GetBoard()) break;
|
if ((currentSquare & block)) break;
|
||||||
}
|
}
|
||||||
// East
|
// East
|
||||||
for (int f = tf + 1; f <= 7; f++) {
|
for (int f = tf + 1; f <= 7; f++) {
|
||||||
currentSquare = (1ULL << (tr * 8 + f));
|
currentSquare = (1ULL << (tr * 8 + f));
|
||||||
attacks |= currentSquare;
|
attacks |= currentSquare;
|
||||||
if ((currentSquare & block).GetBoard()) break;
|
if ((currentSquare & block)) break;
|
||||||
}
|
}
|
||||||
// West
|
// West
|
||||||
for (int f = tf - 1; f >= 0; f--) {
|
for (int f = tf - 1; f >= 0; f--) {
|
||||||
currentSquare = (1ULL << (tr * 8 + f));
|
currentSquare = (1ULL << (tr * 8 + f));
|
||||||
attacks |= currentSquare;
|
attacks |= currentSquare;
|
||||||
if ((currentSquare & block).GetBoard()) break;
|
if ((currentSquare & block)) break;
|
||||||
}
|
}
|
||||||
|
|
||||||
return attacks;
|
return attacks;
|
||||||
@@ -221,7 +221,7 @@ void initSliderAttacks() {
|
|||||||
for (int index = 0; index < 512; index++) {
|
for (int index = 0; index < 512; index++) {
|
||||||
Bitboard occupancy = Bitboard::getOccupancy(index, bishopMasks[square]);
|
Bitboard occupancy = Bitboard::getOccupancy(index, bishopMasks[square]);
|
||||||
|
|
||||||
int magicIndex = (occupancy.GetBoard() * bishopMagicNumbers[square])
|
int magicIndex = (occupancy * bishopMagicNumbers[square])
|
||||||
>> (64 - bishopRelevantBits[square]);
|
>> (64 - bishopRelevantBits[square]);
|
||||||
|
|
||||||
bishopAttacks[square][magicIndex] = bishopAttacksOnTheFly(square, occupancy);
|
bishopAttacks[square][magicIndex] = bishopAttacksOnTheFly(square, occupancy);
|
||||||
@@ -230,7 +230,7 @@ void initSliderAttacks() {
|
|||||||
for (int index = 0; index < 4096; index++) {
|
for (int index = 0; index < 4096; index++) {
|
||||||
Bitboard occupancy = Bitboard::getOccupancy(index, rookMasks[square]);
|
Bitboard occupancy = Bitboard::getOccupancy(index, rookMasks[square]);
|
||||||
|
|
||||||
int magicIndex = (occupancy.GetBoard() * rookMagicNumbers[square])
|
int magicIndex = (occupancy * rookMagicNumbers[square])
|
||||||
>> (64 - rookRelevantBits[square]);
|
>> (64 - rookRelevantBits[square]);
|
||||||
|
|
||||||
rookAttacks[square][magicIndex] = rookAttacksOnTheFly(square, occupancy);
|
rookAttacks[square][magicIndex] = rookAttacksOnTheFly(square, occupancy);
|
||||||
@@ -239,7 +239,7 @@ void initSliderAttacks() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Bitboard getBishopAttack(int square, U64 occupancy) {
|
Bitboard getBishopAttack(int square, U64 occupancy) {
|
||||||
occupancy &= bishopMasks[square].GetBoard();
|
occupancy &= bishopMasks[square];
|
||||||
occupancy *= bishopMagicNumbers[square];
|
occupancy *= bishopMagicNumbers[square];
|
||||||
occupancy >>= (64 - bishopRelevantBits[square]);
|
occupancy >>= (64 - bishopRelevantBits[square]);
|
||||||
|
|
||||||
@@ -247,7 +247,7 @@ Bitboard getBishopAttack(int square, U64 occupancy) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
Bitboard getRookAttack(int square, U64 occupancy) {
|
Bitboard getRookAttack(int square, U64 occupancy) {
|
||||||
occupancy &= rookMasks[square].GetBoard();
|
occupancy &= rookMasks[square];
|
||||||
occupancy *= rookMagicNumbers[square];
|
occupancy *= rookMagicNumbers[square];
|
||||||
occupancy >>= (64 - rookRelevantBits[square]);
|
occupancy >>= (64 - rookRelevantBits[square]);
|
||||||
|
|
||||||
@@ -258,11 +258,11 @@ Bitboard getQueenAttack(int square, U64 occupancy) {
|
|||||||
U64 bishopOccupancy = occupancy;
|
U64 bishopOccupancy = occupancy;
|
||||||
U64 rookOccupancy = occupancy;
|
U64 rookOccupancy = occupancy;
|
||||||
|
|
||||||
bishopOccupancy &= bishopMasks[square].GetBoard();
|
bishopOccupancy &= bishopMasks[square];
|
||||||
bishopOccupancy *= bishopMagicNumbers[square];
|
bishopOccupancy *= bishopMagicNumbers[square];
|
||||||
bishopOccupancy >>= (64 - bishopRelevantBits[square]);
|
bishopOccupancy >>= (64 - bishopRelevantBits[square]);
|
||||||
|
|
||||||
rookOccupancy &= rookMasks[square].GetBoard();
|
rookOccupancy &= rookMasks[square];
|
||||||
rookOccupancy *= rookMagicNumbers[square];
|
rookOccupancy *= rookMagicNumbers[square];
|
||||||
rookOccupancy >>= (64 - rookRelevantBits[square]);
|
rookOccupancy >>= (64 - rookRelevantBits[square]);
|
||||||
|
|
||||||
@@ -324,14 +324,14 @@ static void GenPawnMoves(Board &board, bool color) {
|
|||||||
static void GenKnightMoves(Board &board, bool color) {
|
static void GenKnightMoves(Board &board, bool color) {
|
||||||
Bitboard knights = board.colors[color] & board.pieces[Knight];
|
Bitboard knights = board.colors[color] & board.pieces[Knight];
|
||||||
|
|
||||||
while (knights.GetBoard()) {
|
while (knights) {
|
||||||
int square = knights.getLS1BIndex();
|
int square = knights.getLS1BIndex();
|
||||||
|
|
||||||
Bitboard moves = knightAttacks[square] & ~board.colors[color];
|
Bitboard moves = knightAttacks[square] & ~board.colors[color];
|
||||||
Bitboard captures = moves & board.colors[!color];
|
Bitboard captures = moves & board.colors[!color];
|
||||||
Bitboard quietMoves = moves & ~board.colors[!color];
|
Bitboard quietMoves = moves & ~board.colors[!color];
|
||||||
|
|
||||||
while (captures.GetBoard()) {
|
while (captures) {
|
||||||
int targetSquare = captures.getLS1BIndex();
|
int targetSquare = captures.getLS1BIndex();
|
||||||
|
|
||||||
board.AddMove(Move(square, targetSquare, capture));
|
board.AddMove(Move(square, targetSquare, capture));
|
||||||
@@ -339,7 +339,7 @@ static void GenKnightMoves(Board &board, bool color) {
|
|||||||
captures.PopBit(targetSquare);
|
captures.PopBit(targetSquare);
|
||||||
}
|
}
|
||||||
|
|
||||||
while (quietMoves.GetBoard()) {
|
while (quietMoves) {
|
||||||
int targetSquare = quietMoves.getLS1BIndex();
|
int targetSquare = quietMoves.getLS1BIndex();
|
||||||
|
|
||||||
board.AddMove(Move(square, targetSquare, quiet));
|
board.AddMove(Move(square, targetSquare, quiet));
|
||||||
|
|||||||
Reference in new issue
Block a user