Move methods from Board to BitBoard

This commit is contained in:
2022-12-22 01:35:37 +01:00
parent a1d4dadaa2
commit 84afaa2ada
4 changed files with 143 additions and 109 deletions

View File

@@ -16,16 +16,16 @@ void Board::setPiece(const Square &square, const Piece::Optional &piece) {
auto index = square.index();
for (auto &bb : mPieceBBs) {
clearIndex(bb, index);
bb.clear(index);
}
setIndex(mPieceBBs[toIndex(piece->type())], index);
setIndex(mPieceBBs[toIndex(piece->color())], index);
setIndex(mOccupiedBB, index);
mPieceBBs[toIndex(piece->type())].set(index);
mPieceBBs[toIndex(piece->color())].set(index);
mOccupiedBB.set(index);
}
Piece::Optional Board::piece(const Square &square) const {
BitBoard mask = indexToBitBoard(square.index());
BitBoard mask = BitBoard::fromIndex(square.index());
if (!(mOccupiedBB & mask)) {
return std::nullopt;
}
@@ -63,8 +63,8 @@ Square::Optional Board::enPassantSquare() const {
void Board::makeMove(const Move &move) {
BitBoard fromBB = indexToBitBoard(move.from().index());
BitBoard toBB = indexToBitBoard(move.to().index());
BitBoard fromBB = BitBoard::fromIndex(move.from().index());
BitBoard toBB = BitBoard::fromIndex(move.to().index());
BitBoard changeBB = fromBB ^ toBB;
// If Piece is captured
@@ -119,7 +119,7 @@ void Board::makeMove(const Move &move) {
}
void Board::handleEnPassant(const Move &move, const Piece &movedPiece) {
if (movedPiece.type() == PieceType::Pawn && mEPS.has_value() && move.from().file() != move.to().file()) {
auto epBB = indexToBitBoard(Square::fromCoordinates(move.to().file(), move.from().rank())->index());
auto epBB = BitBoard::fromIndex(Square::fromCoordinates(move.to().file(), move.from().rank())->index());
auto capturedPiece = Piece(!mTurn, PieceType::Pawn);
mPieceBBs[toIndex(capturedPiece.color())] ^= epBB;
@@ -133,7 +133,7 @@ void Board::handlePawnDoubleAdvance(const Move &move, BitBoard bb, const Piece &
auto fromR = move.from().rank();
auto toR = move.to().rank();
auto diff = abs(static_cast<int>(fromR) - static_cast<int>(toR));
if (diff == 2 && (mPieceBBs[toIndex(PieceType::Pawn)] & (bb << 1 | bb >> 1) & getRankBB(static_cast<int>(toR)))) {
if (diff == 2 && (mPieceBBs[toIndex(PieceType::Pawn)] & (bb.left(1) | bb.right(1)) & BitBoard::getRank(static_cast<int>(toR)))) {
mEPS = Square::fromCoordinates(move.to().file(), std::max(fromR, toR) - 1);
return;
}
@@ -193,10 +193,10 @@ bool Board::isMoveCastling(const BitBoard &from, const BitBoard &to, const Piece
}
if (piece.color() == PieceColor::White) {
return (from & indexToBitBoard(E1));
return (from & BitBoard::fromIndex(E1));
}
return from & indexToBitBoard(E8);
return from & BitBoard::fromIndex(E8);
}
std::ostream &operator<<(std::ostream &os, const Board &board) {