diff --git a/Board.cpp b/Board.cpp index 68f61bf..38eaed5 100644 --- a/Board.cpp +++ b/Board.cpp @@ -89,17 +89,22 @@ void Board::setupInitialPosition() { return; } -int Board::setupFromFEN(std::string strFEN) { +int setupFromFEN(std::string strFEN) { std::vector splitFEN = split(strFEN, ' '); - if(splitFEN.size() != 6) // a valid FEN *must* contain 6 fields + if(splitFEN.size() != 6) // a valid FEN *must* contain 6 fields return -1; - // ====== START OF FIELD 1 ====== std::vector splitField1 = split(splitFEN[0], '/'); int rank = 0, file = 0; int skip = 0; bool wKingPlaced = false, bKingPlaced = false; for(auto &ranks : splitField1) { for(auto &sym : ranks) { + if(skip) { + boardGrid[rank][file] = nullptr; // remove reference + skip--; + file++; + continue; + } switch(sym) { case 'R': // white rook @@ -168,62 +173,17 @@ int Board::setupFromFEN(std::string strFEN) { case '5': case '6': case '7': - case '8': { - std::string skipStr(1, sym); - skip = atoi(skipStr.c_str()); - skip--; // fix off by 1 error - do { - boardGrid[rank][file] = nullptr; // remove reference - file = (file < 8) ? file + 1 : 0; - } while(--skip); + case '8': + case '9': + skip = atoi(sym); break; - } default: // invalid character? return -1; - break; } file++; - if(file > 7) { - file = 0; - break; - } } rank++; - if(rank > 7) - break; - } - // ======= END OF FIELD 1 ====== - // ======= START OF FIELD 2 ====== - std::string w = "w"; - std::string b = "b"; - if(splitFEN[1] == w) - playerTurn = PL_WHITE; - else if(splitFEN[1] == b) - playerTurn = PL_BLACK; - else { - // invalid FEN - return -1; - } - // ====== END OF FIELD 2 ====== - // ====== START OF FIELD 3 ====== - std::string k = "k", K = "K", q = "q", Q = "Q"; - if(splitFEN[2] == "-") { - // nobody can castle, either side - // locate Kings and set appropriate variables - // canCastleKS, canCastleQS - } - if(splitFEN[2].find(k) != std::string::npos) { - // black king can castle kingside - } - if(splitFEN[2].find(K) != std::string::npos) { - // white king can castle kingside - } - if(splitFEN[2].find(q) != std::string::npos) { - // black king can castle queenside - } - if(splitFEN[2].find(Q) != std::string::npos) { - // white king can castle queenside } return 0; } @@ -238,8 +198,4 @@ void Board::deserializeBoard(uint64_t incomingBoard) { boardRows[i] = (incomingBoard >> (8 * i)) & 0xFF; // how do we then figure out what has moved? return; -} - -std::unique_ptr &Board::getPieceAt(Square square) { - return boardGrid[square.rank][square.file]; } \ No newline at end of file diff --git a/Piece.cpp b/Piece.cpp index ccfd82a..e0ed418 100644 --- a/Piece.cpp +++ b/Piece.cpp @@ -18,11 +18,11 @@ Piece::~Piece() { // so this is just a stub that does nothing. it doesn't matter // because only the derived class versions should be called. -std::vector Piece::getLegalMoves(const Square &from, Board &board) const { +std::vector Piece::getLegalMoves(const Square &from, const Board &board) const { std::vector moveList; return moveList; } -bool King::checkForCheck(Board &board) const { +bool King::checkForCheck() const { std::vector> kingVulnerable = { {-1, 0}, // Up {-1, -1}, // up-left @@ -45,7 +45,7 @@ bool King::checkForCheck(Board &board) const { return inCheck; } -std::vector King::getLegalMoves(const Square &from, Board &board) const { +std::vector King::getLegalMoves(const Square &from, const Board &board) const { std::vector moveList; std::vector> directions = { {-1, 0}, // Up @@ -156,7 +156,7 @@ std::vector King::getLegalMoves(const Square &from, Board &board) const { return moveList; } -std::vector Rook::getLegalMoves(const Square &from, Board &board) const { +std::vector Rook::getLegalMoves(const Square &from, const Board &board) const { std::vector moveList; const int directions[4][2] = { {-1, 0}, // Up @@ -186,22 +186,22 @@ std::vector Rook::getLegalMoves(const Square &from, Board &board) const { return moveList; } -std::vector Queen::getLegalMoves(const Square &from, Board &board) const { +std::vector Queen::getLegalMoves(const Square &from, const Board &board) const { std::vector moveList; return moveList; } -std::vector Knight::getLegalMoves(const Square &from, Board &board) const { +std::vector Knight::getLegalMoves(const Square &from, const Board &board) const { std::vector moveList; return moveList; } -std::vector Bishop::getLegalMoves(const Square &from, Board &board) const { +std::vector Bishop::getLegalMoves(const Square &from, const Board &board) const { std::vector moveList; return moveList; } -std::vector Pawn::getLegalMoves(const Square &from, Board &board) const { +std::vector Pawn::getLegalMoves(const Square &from, const Board &board) const { std::vector moveList; const int directions[2][4][2] = { { diff --git a/inc/Board.hpp b/inc/Board.hpp index 599b29e..4076b32 100644 --- a/inc/Board.hpp +++ b/inc/Board.hpp @@ -9,37 +9,39 @@ #ifndef BOARD_HPP #define BOARD_HPP -#include -#include -#include #include - +#include +#include #include "Piece.hpp" // why do I have to forward declare all these?! class Piece; -enum Players { PL_WHITE, PL_BLACK }; +enum Players { + PL_WHITE, PL_BLACK +}; struct Square; class Board { -private: - friend class Piece; - Players playerTurn; -public: - // this should be protected, but even when Piece is declared as a friend, - // accessing it in Piece.cpp threw an error - std::vector>> boardGrid; - Board(); - virtual ~Board(); - void setupInitialPosition(); - std::unique_ptr &getPieceAt(Square square); - void movePiece(Square from, Square to); - int setupFromFEN(std::string strFEN); - bool isInBounds(Square square) const; - bool isEmpty(Square square) const; - uint64_t serialBoard = 0xFFFF00000000FFFF; // opening position - void deserializeBoard(uint64_t incomingBoard); + private: + friend class Piece; + Players playerTurn; + public: + // this should be protected, but even when Piece is declared as a friend, + // accessing it in Piece.cpp threw an error + std::vector>> boardGrid; + Board(); + virtual ~Board(); + void setupInitialPosition(); + Piece *getPieceAt(Square square); + void movePiece(Square from, Square to); + int setupFromFEN(std::string strFEN); + bool isInBounds(Square square) const; + bool isEmpty(Square square) const; + uint64_t serialBoard = 0xFFFF00000000FFFF; // opening position + void deserializeBoard(uint64_t incomingBoard); + }; #endif // BOARD_HPP + diff --git a/inc/Piece.hpp b/inc/Piece.hpp index 15132dd..b2cd0cb 100644 --- a/inc/Piece.hpp +++ b/inc/Piece.hpp @@ -68,7 +68,7 @@ class Piece { Piece(PieceColour pColour) : colour(pColour) { } virtual ~Piece(); - virtual std::vector getLegalMoves(const Square &from, Board &board) const; + virtual std::vector getLegalMoves(const Square &from, const Board &board) const; PieceColour getColour() const { return colour; @@ -101,9 +101,9 @@ class King : public Piece { pieceSymbol = 'K'; pieceType = KING; } - virtual std::vector getLegalMoves(const Square &from, Board &board) const override; + virtual std::vector getLegalMoves(const Square &from, const Board &board) const override; - bool checkForCheck(Board &board) const; + bool checkForCheck() const; bool checkForCastle(enum CastleSide side) const { if(side == KINGSIDE) @@ -130,7 +130,7 @@ class Rook : public Piece { pieceSymbol = 'R'; pieceType = ROOK; } - virtual std::vector getLegalMoves(const Square &from, Board &board) const override; + virtual std::vector getLegalMoves(const Square &from, const Board &board) const override; }; class Queen : public Piece { @@ -142,7 +142,7 @@ class Queen : public Piece { pieceSymbol = 'Q'; pieceType = QUEEN; } - virtual std::vector getLegalMoves(const Square &from, Board &board) const override; + virtual std::vector getLegalMoves(const Square &from, const Board &board) const override; }; class Knight : public Piece { @@ -154,7 +154,7 @@ class Knight : public Piece { pieceSymbol = 'N'; pieceType = KNIGHT; } - virtual std::vector getLegalMoves(const Square &from, Board &board) const override; + virtual std::vector getLegalMoves(const Square &from, const Board &board) const override; }; class Bishop : public Piece { @@ -166,7 +166,7 @@ class Bishop : public Piece { pieceSymbol = 'B'; pieceType = BISHOP; } - virtual std::vector getLegalMoves(const Square &from, Board &board) const override; + virtual std::vector getLegalMoves(const Square &from, const Board &board) const override; }; class Pawn : public Piece { @@ -178,7 +178,7 @@ class Pawn : public Piece { pieceSymbol = 'P'; pieceType = PAWN; } - virtual std::vector getLegalMoves(const Square &from, Board &board) const override; + virtual std::vector getLegalMoves(const Square &from, const Board &board) const override; }; #endif // PIECE_HPP diff --git a/main.cpp b/main.cpp index fa1ca31..51059d6 100644 --- a/main.cpp +++ b/main.cpp @@ -179,8 +179,6 @@ extern "C" int main(void) { initInterrupts(); Board gameBoard; NeoPixel boardLights(64); - - gameBoard.setupInitialPosition(); while(1) { } } diff --git a/nbproject/configurations.xml b/nbproject/configurations.xml index 28d6976..2a6fe8d 100644 --- a/nbproject/configurations.xml +++ b/nbproject/configurations.xml @@ -54,7 +54,7 @@ PIC32MX270F256B - snap + noID XC32 4.60 2 @@ -73,7 +73,7 @@ false - false + true @@ -87,7 +87,7 @@ false false - true + false @@ -103,12 +103,12 @@ - + - + @@ -141,7 +141,7 @@ - + @@ -171,7 +171,7 @@ - + @@ -214,18 +214,18 @@ - + - + - + @@ -261,172 +261,6 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -