Compare commits

...

2 Commits

Author SHA1 Message Date
16c7fe2a34 [Board] Add method to print BitBoard 2022-12-21 20:04:22 +01:00
4671fec523 [Board] Implement enPassant move handling 2022-12-21 20:03:59 +01:00
2 changed files with 34 additions and 1 deletions

View File

@@ -5,6 +5,7 @@
#include <cmath>
#include <bitset>
#include <algorithm>
#include <iostream>
Board::Board() {
}
@@ -108,13 +109,25 @@ void Board::makeMove(const Move &move) {
}
}
// en passant
handleEnPassant(move, movedPiece);
handlePawnDoubleAdvance(move, toBB, movedPiece);
// change turn
mTurn = !mTurn;
}
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 capturedPiece = Piece(!mTurn, PieceType::Pawn);
mPieceBBs[toIndex(capturedPiece.color())] ^= epBB;
mPieceBBs[toIndex(capturedPiece.type())] ^= epBB;
mOccupiedBB ^= epBB;
}
}
void Board::handlePawnDoubleAdvance(const Move &move, BitBoard bb, const Piece &movedPiece) {
if (movedPiece.type() == PieceType::Pawn) {
auto fromR = move.from().rank();
@@ -185,6 +198,24 @@ bool Board::isMoveCastling(const BitBoard &from, const BitBoard &to, const Piece
return (from & indexToBitBoard(E8));
}
void Board::printBitBoard(const Board::BitBoard &bb) {
// For debugging only, performance isn't important
for (int i = 7; i >= 0; i--) {
int rank = i * 8;
for (int j = 0; j < 8; j++) {
// Get the piece for this index. Assume it exists.
// Print piece, otherwise '.';
if (bb & (1ULL << (rank + j))) {
std::cout << 1;
} else {
std::cout << '.';
}
std::cout << ' ';
}
std::cout << '\n';
}
}
std::ostream &operator<<(std::ostream &os, const Board &board) {
// For debugging only, performance isn't important

View File

@@ -127,7 +127,9 @@ private:
return __builtin_ctzll(b);
}
static void printBitBoard(const BitBoard &bb);
void handlePawnDoubleAdvance(const Move &move, BitBoard bb, const Piece &movedPiece);
void handleEnPassant(const Move &move, const Piece &movedPiece);
};
std::ostream &operator<<(std::ostream &os, const Board &board);