From 6773da28fbd9d716a8eac75ca6b1dc692c998994 Mon Sep 17 00:00:00 2001 From: "A.M. Rowsell" Date: Sat, 30 Aug 2025 18:46:18 -0400 Subject: [PATCH] Piece: expanding getLegalMoves for Queen/Knight/Bishop --- Piece.cpp | 53 +++++++++++++++++++++++++++++++++++- inc/Board.hpp | 2 ++ inc/Piece.hpp | 12 +++++++- nbproject/configurations.xml | 8 ++++-- 4 files changed, 70 insertions(+), 5 deletions(-) diff --git a/Piece.cpp b/Piece.cpp index ccfd82a..a9d81ad 100644 --- a/Piece.cpp +++ b/Piece.cpp @@ -12,6 +12,9 @@ Piece::~Piece() { return; } +// TODO: Add operator overload for << to allow easy printing +// TODO: Test getPieceAt() function to see if it works as intended + // because the Piece class is instantiated in another class using // unique_ptr, non-pure virtual functions apparently *must* // have definitions, or the linker freaks out @@ -164,7 +167,7 @@ std::vector Rook::getLegalMoves(const Square &from, Board &board) const { {0, -1}, // Left {0, 1} // Right }; - for(auto& dir : directions) { + for(auto &dir : directions) { int r = from.rank + dir[0]; int f = from.file + dir[1]; while(r >= 0 && r < 8 && f >= 0 && f < 8) { @@ -188,16 +191,64 @@ std::vector Rook::getLegalMoves(const Square &from, Board &board) const { std::vector Queen::getLegalMoves(const Square &from, Board &board) const { std::vector moveList; + std::vector> directions = { + {-1, 0}, // Up + {-1, -1}, // up-left + {-1, 1}, // up-right + {1, 0}, // Down + {1, -1}, // down-left + {1, 1}, // down-right + {0, -1}, // Left + {0, 1} // Right + }; + for(auto &dir : directions) { + // establish r/f for square to check + int r = from.rank + dir[0]; + int f = from.file + dir[1]; + auto ra = static_cast(r); + auto fi = static_cast(f); + Square targetSquare = {ra, fi}; + while(r >= 0 && r < 8 && f >= 0 && f < 8) { + const auto &target = board.boardGrid[r][f]; // examine the target square + if(!target) { // if square is empty (NULL) + moveList.push_back({from, targetSquare}); // then it's potentially a legal move + } else if(target && target->getColour() != this->getColour()) { // if it's occupied with a piece of opposite colour + moveList.push_back({from, targetSquare}); // then again it's potentially legal + break; + } else { + // otherwise it's one of our pieces + break; + } + r += dir[0]; + f += dir[1]; + } + } return moveList; } std::vector Knight::getLegalMoves(const Square &from, Board &board) const { std::vector moveList; + std::vector> directions = { + {-1, -2}, + {-1, 2}, + {-2, -1}, + {-2, 1}, + {1, -2}, + {1, 2}, + {2, -1}, + {2, 1} + }; return moveList; } std::vector Bishop::getLegalMoves(const Square &from, Board &board) const { std::vector moveList; + std::vector> directions = { + {-1, -1}, // up-left + {-1, 1}, // up-right + {1, -1}, // down-left + {1, 1}, // down-right + }; return moveList; } diff --git a/inc/Board.hpp b/inc/Board.hpp index 599b29e..286437f 100644 --- a/inc/Board.hpp +++ b/inc/Board.hpp @@ -13,6 +13,7 @@ #include #include #include +#include #include "Piece.hpp" @@ -32,6 +33,7 @@ public: std::vector>> boardGrid; Board(); virtual ~Board(); + void setupInitialPosition(); std::unique_ptr &getPieceAt(Square square); void movePiece(Square from, Square to); diff --git a/inc/Piece.hpp b/inc/Piece.hpp index 15132dd..af0d3cf 100644 --- a/inc/Piece.hpp +++ b/inc/Piece.hpp @@ -8,7 +8,6 @@ #ifndef PIECE_HPP #define PIECE_HPP -#pragma once #include #include @@ -115,6 +114,13 @@ class King : public Piece { else return false; } + + void setCastleQS(bool canCastle) { + canCastleQS = canCastle; + } + void setCastleKS(bool canCastle) { + canCastleKS = canCastle; + } protected: bool canCastleQS = true; bool canCastleKS = true; @@ -179,6 +185,10 @@ class Pawn : public Piece { pieceType = PAWN; } virtual std::vector 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 diff --git a/nbproject/configurations.xml b/nbproject/configurations.xml index 28d6976..bda203e 100644 --- a/nbproject/configurations.xml +++ b/nbproject/configurations.xml @@ -4,10 +4,10 @@ - Board.hpp - Piece.hpp - NeoPixel.hpp inc/strFuncs.hpp + inc/Board.hpp + inc/NeoPixel.hpp + inc/Piece.hpp +