feat: en passant generally implemented

This commit is contained in:
A.M. Rowsell 2026-09-22 01:58:46 -04:00
commit 10a4dcb60b
Signed by: amr
GPG key ID: E0879EDBDB0CA7B1
4 changed files with 53 additions and 12 deletions

View file

@ -288,7 +288,29 @@ void Board::nextTurn() {
}
void Board::movePiece(Square from, Square to) {
return;
Piece *movingPiece = getPieceAt(from);
if(!movingPiece) return;
Square newEnPassantTarget = {INVALID_RANK, INVALID_FILE};
if(movingPiece->getPieceType() == PAWN) {
// Check if it's a 2-square move
if(abs(to.rank - from.rank) == 2) {
int dir = (to.rank > from.rank) ? -1 : 1;
newEnPassantTarget = {static_cast<Rank>(to.rank + dir), from.file};
}
// Check if it's an en passant capture (diagonal move to an empty square)
else if(to.file != from.file && isSquareEmpty(to)) {
// The captured pawn is on the same rank as 'from', but the file of 'to'
clearSquare(from.rank, to.file);
}
}
// Move the piece
setPieceAt(to, std::move(boardGrid[from.rank][from.file]));
clearSquare(from);
enPassantTargetSquare = newEnPassantTarget;
}
void Board::deserializeBoard(uint64_t incomingBoard) {

View file

@ -392,6 +392,10 @@ std::vector<Move> Pawn::getLegalMoves(const Square &from, Board &board) const {
if(target && target->getColour() != this->getColour()) {
// legal capture
moveList.push_back({from, targetSquare});
} else if(targetSquare.rank == board.getEnPassantTargetSquare().rank &&
targetSquare.file == board.getEnPassantTargetSquare().file) {
// en passant capture
moveList.push_back({from, targetSquare});
} else
continue;
} else { // normal one square forward
@ -419,6 +423,10 @@ std::vector<Move> Pawn::getLegalMoves(const Square &from, Board &board) const {
if(target && target->getColour() != this->getColour()) { // can only move there if it's a capture
// legal capture
moveList.push_back({from, targetSquare});
} else if(targetSquare.rank == board.getEnPassantTargetSquare().rank &&
targetSquare.file == board.getEnPassantTargetSquare().file) {
// en passant capture
moveList.push_back({from, targetSquare});
} else
continue;
} else { // normal one square forward

View file

@ -48,10 +48,12 @@ class Board {
* It is a 2D vector of Piece types, or nullptr for empty squares
* @image html boardGrid.svg "Logical diagram of boardGrid vector" */
Players playerTurn;
Square enPassantTargetSquare = {INVALID_RANK, INVALID_FILE};
// let's get super object-oriented, baby
// these help the getters and setters access the boardGrid
// and also make them shorter and less duplicative
std::unique_ptr<Piece>& at(int r, int f) {
std::unique_ptr<Piece> &at(int r, int f) {
return boardGrid[r][f];
}
@ -59,11 +61,11 @@ class Board {
return boardGrid[r][f];
}
std::unique_ptr<Piece> &at(const Square& sq) {
std::unique_ptr<Piece> &at(const Square &sq) {
return boardGrid[static_cast<int>(sq.rank)][static_cast<int>(sq.file)];
}
const std::unique_ptr<Piece> &at(const Square& sq) const {
const std::unique_ptr<Piece> &at(const Square &sq) const {
return boardGrid[static_cast<int>(sq.rank)][static_cast<int>(sq.file)];
}
public:
@ -73,40 +75,50 @@ class Board {
// These are to allow Piece to access Board in a controlled way
// instead of adding a friend declaration for every subclass
// ----- Getters -----
Piece* getPieceAt(int r, int f) {
Piece *getPieceAt(int r, int f) {
return at(r, f).get();
}
const Piece* getPieceAt(int r, int f) const {
const Piece *getPieceAt(int r, int f) const {
return at(r, f).get();
}
Piece* getPieceAt(const Square& sq) {
Piece *getPieceAt(const Square &sq) {
return at(sq).get();
}
const Piece* getPieceAt(const Square& sq) const {
const Piece *getPieceAt(const Square &sq) const {
return at(sq).get();
}
Square getEnPassantTargetSquare() const {
return enPassantTargetSquare;
}
// ----- Setters -----
void setPieceAt(int r, int f, std::unique_ptr<Piece> piece) {
at(r, f) = std::move(piece);
}
void setPieceAt(const Square& sq, std::unique_ptr<Piece> piece) {
void setPieceAt(const Square &sq, std::unique_ptr<Piece> piece) {
at(sq) = std::move(piece);
}
void clearSquare(int r, int f) {
at(r, f).reset();
}
void clearSquare(const Square& sq) {
void clearSquare(const Square &sq) {
at(sq).reset();
}
void setEnPassantTargetSquare(Square sq) {
enPassantTargetSquare = sq;
}
void clearEnPassantTargetSquare() {
enPassantTargetSquare = {INVALID_RANK, INVALID_FILE};
}
// ----- Utility -----
bool isSquareEmpty(int r, int f) const {
return at(r, f) == nullptr;
}
bool isSquareEmpty(const Square& sq) const {
bool isSquareEmpty(const Square &sq) const {
return at(sq) == nullptr;
}

View file

@ -228,7 +228,6 @@ class Pawn : public Piece {
virtual std::vector<Move> getLegalMoves(const Square &from, Board &board) const override;
void promote(const Square &promotionSquare, Board &board, PieceType promoteTo);
protected:
bool vulnEnPassant = false;
bool firstMove = true;
};
#endif // PIECE_HPP