From c52f57293e313b878d72bd5deed38ee5b7560726 Mon Sep 17 00:00:00 2001 From: Arthur Bols Date: Wed, 21 Dec 2022 19:39:54 +0100 Subject: [PATCH] [Board] Implement EnPassant handling --- Board.cpp | 20 ++++++++++++++++++-- Board.hpp | 12 +++++++++++- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/Board.cpp b/Board.cpp index b555725..6fc1746 100644 --- a/Board.cpp +++ b/Board.cpp @@ -53,11 +53,11 @@ CastlingRights Board::castlingRights() const { void Board::setEnPassantSquare(const Square::Optional &square) { if (!square.has_value()) return; - mEPS = square->index(); + mEPS = square; } Square::Optional Board::enPassantSquare() const { - return Square::fromIndex(mEPS); + return mEPS; } void Board::makeMove(const Move &move) { @@ -108,10 +108,26 @@ void Board::makeMove(const Move &move) { } } + // en passant + handlePawnDoubleAdvance(move, toBB, movedPiece); + // change turn mTurn = !mTurn; } +void Board::handlePawnDoubleAdvance(const Move &move, BitBoard bb, const Piece &movedPiece) { + if (movedPiece.type() == PieceType::Pawn) { + auto fromR = move.from().rank(); + auto toR = move.to().rank(); + auto diff = abs(static_cast(fromR) - static_cast(toR)); + if (diff == 2 && (mPieceBBs[toIndex(PieceType::Pawn)] & (bb << 1 | bb >> 1) & getRankBB(static_cast(toR)))) { + mEPS = Square::fromCoordinates(move.to().file(), std::max(fromR, toR) - 1); + return; + } + } + + mEPS = std::nullopt; +} void Board::pseudoLegalMoves(MoveVec &moves) const { (void) moves; diff --git a/Board.hpp b/Board.hpp index 737f4d2..879d6e6 100644 --- a/Board.hpp +++ b/Board.hpp @@ -47,7 +47,7 @@ private: PieceColor mTurn = PieceColor::White; CastlingRights mCR = CastlingRights::None; - unsigned mEPS = 64; + std::optional mEPS; enum DefinedBoards : BitBoard { AFile = 0x0101010101010101, @@ -80,6 +80,14 @@ private: return (1ULL << i); } + static inline BitBoard genShift(BitBoard x, const int s) { + return (s < 0) ? (x >> -s) : (s > 63) ? x : (x << s); + } + + static inline BitBoard getRankBB(int r) { + return (genShift(1ULL, (r + 1) * 8) - 1) & genShift(~1ULL, r * 8 - 1); + } + static inline int toIndex(PieceType t) { return static_cast(t); } @@ -118,6 +126,8 @@ private: static inline int getLSB(const BitBoard b) { return __builtin_ctzll(b); } + + void handlePawnDoubleAdvance(const Move &move, BitBoard bb, const Piece &movedPiece); }; std::ostream &operator<<(std::ostream &os, const Board &board);