diff --git a/Board.cpp b/Board.cpp index f5901e2..5b145b3 100644 --- a/Board.cpp +++ b/Board.cpp @@ -7,6 +7,7 @@ // defined by the Mozilla Public License, v. 2.0. #include "inc/Board.hpp" +#include "inc/Piece.hpp" #include "inc/strFuncs.hpp" /* @@ -241,8 +242,8 @@ int Board::setupFromFEN(std::string strFEN) { // ====== START OF FIELD 3 ====== std::string k = "k", K = "K", q = "q", Q = "Q"; // yeah this is hacky af but it works... blame SO - Piece *baseBKing = getPieceAt(blackKing).get(); - Piece *baseWKing = getPieceAt(whiteKing).get(); + Piece *baseBKing = getPieceAt(blackKing); + Piece *baseWKing = getPieceAt(whiteKing); King *bKing = dynamic_cast(baseBKing); King *wKing = dynamic_cast(baseWKing); if(splitFEN[2] == "-") { @@ -310,8 +311,4 @@ void Board::deserializeBoard(uint64_t incomingBoard) { serialBoard.boardRows[i] = static_cast((incomingBoard >> (8 * i)) & 0xFF); // how do we then figure out what has moved? return; -} - -std::unique_ptr &Board::getPieceAt(Square square) { - return boardGrid[static_cast(square.rank)][static_cast(square.file)]; } \ No newline at end of file diff --git a/Piece.cpp b/Piece.cpp index 61f85bb..00e787e 100644 --- a/Piece.cpp +++ b/Piece.cpp @@ -7,6 +7,7 @@ // defined by the Mozilla Public License, v. 2.0. #include "inc/Piece.hpp" +#include "inc/Board.hpp" Piece::~Piece() { return; @@ -85,12 +86,10 @@ std::vector King::getLegalMoves(const Square &from, Board &board) const { // 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}; + Square targetSquare{static_cast(r), static_cast(f)}; if(targetSquare.isValid()) { - const auto &target = board.boardGrid[r][f]; // examine the target square - if(!target) { // if square is empty (NULL) + const Piece *target = board.getPieceAt(targetSquare); // examine the target square + if(!target) { // if square is empty (nullptr) 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 @@ -135,7 +134,8 @@ std::vector King::getLegalMoves(const Square &from, Board &board) const { bool diagonal = (abs(dir[0]) + abs(dir[1]) == 2) ? 1 : 0; // check if diagonal bool knight = (abs(dir[0]) + abs(dir[1]) == 3) ? 1 : 0; // check if knight attack while(r >= 0 && r < 8 && f >= 0 && f < 8) { - auto &target = board.boardGrid[r][f]; // access the square we're examining + Square targetSquare{static_cast(r), static_cast(f)}; + const Piece *target = board.getPieceAt(targetSquare); // access the square we're examining if(!target) { // empty square, continue continue; @@ -204,10 +204,8 @@ std::vector Rook::getLegalMoves(const Square &from, Board &board) const { int r = from.rank + dir[0]; int f = from.file + dir[1]; while(r >= 0 && r < 8 && f >= 0 && f < 8) { - const auto& target = board.boardGrid[r][f]; - auto ra = static_cast(r); - auto fi = static_cast(f); - Square targetSquare = {ra, fi}; + Square targetSquare{static_cast(r), static_cast(f)}; + const Piece *target = board.getPieceAt(targetSquare); if(!target) { moveList.push_back({from, targetSquare}); } else if(target && target->getColour() != this->getColour()) { @@ -248,11 +246,9 @@ std::vector Queen::getLegalMoves(const Square &from, Board &board) const { // 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 + Square targetSquare{static_cast(r), static_cast(f)}; + const Piece *target = board.getPieceAt(targetSquare);// 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 @@ -354,13 +350,11 @@ std::vector Pawn::getLegalMoves(const Square &from, Board &board) const { int r = from.rank + dir[0]; int f = from.file + dir[1]; if(r >= 0 && r < 8 && f >= 0 && f < 8) { // no need for a while loop as we only have finite moves in limited directions - const auto& target = board.boardGrid[r][f]; - auto ra = static_cast(r); - auto fi = static_cast(f); - Square targetSquare = {ra, fi}; + Square targetSquare{static_cast(r), static_cast(f)}; + const Piece *target = board.getPieceAt(targetSquare); if(dir[0] == -2 && !this->checkIfMoved()) { // then 2 is potentially legal - if(!target && !(board.boardGrid[r + 1][f])) // check both squares for pieces of any colour + if(!target && board.isSquareEmpty(Square{static_cast(r + 1), static_cast(f)})) // check both squares for pieces of any colour moveList.push_back({from, targetSquare}); else continue; @@ -383,13 +377,11 @@ std::vector Pawn::getLegalMoves(const Square &from, Board &board) const { int r = from.rank + dir[0]; int f = from.file + dir[1]; if(r >= 0 && r < 8 && f >= 0 && f < 8) { // no need for a while loop as we only have finite moves in limited directions - const auto& target = board.boardGrid[r][f]; - auto ra = static_cast(r); - auto fi = static_cast(f); - Square targetSquare = {ra, fi}; + Square targetSquare{static_cast(r), static_cast(f)}; + const Piece *target = board.getPieceAt(targetSquare); if(dir[0] == 2 && !this->checkIfMoved()) { // then 2 is potentially legal - if(!target && !(board.boardGrid[r - 1][f])) + if(!target && board.isSquareEmpty(Square{static_cast(r + 1), static_cast(f)})) moveList.push_back({from, targetSquare}); else continue; diff --git a/inc/Board.hpp b/inc/Board.hpp index 5e8535a..abf53ae 100644 --- a/inc/Board.hpp +++ b/inc/Board.hpp @@ -13,7 +13,7 @@ #include #include #include - +#include "Piece.hpp" // suggested to forward declare here, and put include in the .cpp class Piece; @@ -28,24 +28,19 @@ class Board { // let's get super object-oriented, baby // these help the getters and setters access the boardGrid // and also make them shorter and less duplicative - inline std::unique_ptr& at(int r, int f) { - return boardGrid[r][f]; - } - inline const std::unique_ptr& at(int r, int f) const { + std::unique_ptr& at(int r, int f) { return boardGrid[r][f]; } - inline std::unique_ptr& at(Rank rank, File file) { - return boardGrid[static_cast(rank)][static_cast(file)]; - } - inline const std::unique_ptr& at(Rank rank, File file) const { - return boardGrid[static_cast(rank)][static_cast(file)]; + const std::unique_ptr& at(int r, int f) const { + return boardGrid[r][f]; } - inline std::unique_ptr& at(const Square& sq) { + std::unique_ptr& at(const Square& sq) { return boardGrid[static_cast(sq.rank)][static_cast(sq.file)]; } - inline const std::unique_ptr& at(const Square& sq) const { + + const std::unique_ptr& at(const Square& sq) const { return boardGrid[static_cast(sq.rank)][static_cast(sq.file)]; } public: @@ -53,40 +48,31 @@ class Board { virtual ~Board(); // These are to allow Piece to access Board in a controlled way - // --- GETTERS --- + // ----- Getters ----- Piece* getPieceAt(int r, int f) { return at(r, f).get(); } const Piece* getPieceAt(int r, int f) const { return at(r, f).get(); } - Piece* getPieceAt(Rank rank, File file) { return at(rank, file).get(); } - const Piece* getPieceAt(Rank rank, File file) const { return at(rank, file).get(); } - Piece* getPieceAt(const Square& sq) { return at(sq).get(); } const Piece* getPieceAt(const Square& sq) const { return at(sq).get(); } - // --- SETTERS --- + // ----- Setters ----- void setPieceAt(int r, int f, std::unique_ptr piece) { at(r, f) = std::move(piece); } - void clearSquare(int r, int f) { at(r, f).reset(); } - - void setPieceAt(Rank rank, File file, std::unique_ptr piece) { at(rank, file) = std::move(piece); } - void clearSquare(Rank rank, File file) { at(rank, file).reset(); } - void setPieceAt(const Square& sq, std::unique_ptr piece) { at(sq) = std::move(piece); } + + void clearSquare(int r, int f) { at(r, f).reset(); } void clearSquare(const Square& sq) { at(sq).reset(); } - // --- CHECKERS --- + // ----- Utility ----- bool isSquareEmpty(int r, int f) const { return at(r, f) == nullptr; } - bool isSquareEmpty(Rank rank, File file) const { return at(rank, file) == nullptr; } bool isSquareEmpty(const Square& sq) const { return at(sq) == nullptr; } void setupInitialPosition(); void clearBoard(); - std::unique_ptr &getPieceAt(Square square); void movePiece(Square from, Square to); void nextTurn(); int setupFromFEN(std::string strFEN); bool isInBounds(Square square) const; - bool isEmpty(Square square) const; // serial shift register stuff uint64_t serialBoard = 0xFFFF00000000FFFF; // opening position void deserializeBoard(uint64_t incomingBoard); diff --git a/inc/Piece.hpp b/inc/Piece.hpp index 762c57d..f7607ea 100644 --- a/inc/Piece.hpp +++ b/inc/Piece.hpp @@ -28,7 +28,7 @@ #include #include #include -#include "Board.hpp" + class Board; enum PieceType {