From 76c0c3ce8be9a1cf3a9adbaebeb62beb3521c98e Mon Sep 17 00:00:00 2001 From: "A.M. Rowsell" Date: Fri, 29 Aug 2025 04:16:20 -0400 Subject: [PATCH 1/2] FEN: more work on FEN decoding, handling field 2 and 3 --- Board.cpp | 66 +++++++++++++++++++++++++++++++++++++++++++++---------- 1 file changed, 55 insertions(+), 11 deletions(-) diff --git a/Board.cpp b/Board.cpp index 38eaed5..68f61bf 100644 --- a/Board.cpp +++ b/Board.cpp @@ -89,22 +89,17 @@ void Board::setupInitialPosition() { return; } -int setupFromFEN(std::string strFEN) { +int Board::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 @@ -173,17 +168,62 @@ int setupFromFEN(std::string strFEN) { case '5': case '6': case '7': - case '8': - case '9': - skip = atoi(sym); + 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); 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; } @@ -198,4 +238,8 @@ 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 From 21fae88bdbe37cdc8346188dd594f1ae8bcc3fd5 Mon Sep 17 00:00:00 2001 From: "A.M. Rowsell" Date: Fri, 29 Aug 2025 04:17:16 -0400 Subject: [PATCH 2/2] fmt: reformatted code, changed some function signatures, fairly minor --- Piece.cpp | 16 +-- inc/Board.hpp | 46 +++++---- inc/Piece.hpp | 16 +-- main.cpp | 2 + nbproject/configurations.xml | 186 +++++++++++++++++++++++++++++++++-- 5 files changed, 216 insertions(+), 50 deletions(-) diff --git a/Piece.cpp b/Piece.cpp index e0ed418..ccfd82a 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, const Board &board) const { +std::vector Piece::getLegalMoves(const Square &from, Board &board) const { std::vector moveList; return moveList; } -bool King::checkForCheck() const { +bool King::checkForCheck(Board &board) const { std::vector> kingVulnerable = { {-1, 0}, // Up {-1, -1}, // up-left @@ -45,7 +45,7 @@ bool King::checkForCheck() const { return inCheck; } -std::vector King::getLegalMoves(const Square &from, const Board &board) const { +std::vector King::getLegalMoves(const Square &from, Board &board) const { std::vector moveList; std::vector> directions = { {-1, 0}, // Up @@ -156,7 +156,7 @@ std::vector King::getLegalMoves(const Square &from, const Board &board) co return moveList; } -std::vector Rook::getLegalMoves(const Square &from, const Board &board) const { +std::vector Rook::getLegalMoves(const Square &from, 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, const Board &board) co return moveList; } -std::vector Queen::getLegalMoves(const Square &from, const Board &board) const { +std::vector Queen::getLegalMoves(const Square &from, Board &board) const { std::vector moveList; return moveList; } -std::vector Knight::getLegalMoves(const Square &from, const Board &board) const { +std::vector Knight::getLegalMoves(const Square &from, Board &board) const { std::vector moveList; return moveList; } -std::vector Bishop::getLegalMoves(const Square &from, const Board &board) const { +std::vector Bishop::getLegalMoves(const Square &from, Board &board) const { std::vector moveList; return moveList; } -std::vector Pawn::getLegalMoves(const Square &from, const Board &board) const { +std::vector Pawn::getLegalMoves(const Square &from, Board &board) const { std::vector moveList; const int directions[2][4][2] = { { diff --git a/inc/Board.hpp b/inc/Board.hpp index 4076b32..599b29e 100644 --- a/inc/Board.hpp +++ b/inc/Board.hpp @@ -9,39 +9,37 @@ #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(); - 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); - +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); }; #endif // BOARD_HPP - diff --git a/inc/Piece.hpp b/inc/Piece.hpp index b2cd0cb..15132dd 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, const Board &board) const; + virtual std::vector getLegalMoves(const Square &from, 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, const Board &board) const override; + virtual std::vector getLegalMoves(const Square &from, Board &board) const override; - bool checkForCheck() const; + bool checkForCheck(Board &board) 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, const Board &board) const override; + virtual std::vector getLegalMoves(const Square &from, 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, const Board &board) const override; + virtual std::vector getLegalMoves(const Square &from, 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, const Board &board) const override; + virtual std::vector getLegalMoves(const Square &from, 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, const Board &board) const override; + virtual std::vector getLegalMoves(const Square &from, 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, const Board &board) const override; + virtual std::vector getLegalMoves(const Square &from, Board &board) const override; }; #endif // PIECE_HPP diff --git a/main.cpp b/main.cpp index 51059d6..fa1ca31 100644 --- a/main.cpp +++ b/main.cpp @@ -179,6 +179,8 @@ 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 2a6fe8d..28d6976 100644 --- a/nbproject/configurations.xml +++ b/nbproject/configurations.xml @@ -54,7 +54,7 @@ PIC32MX270F256B - noID + snap XC32 4.60 2 @@ -73,7 +73,7 @@ false - true + false @@ -87,7 +87,7 @@ false false - false + true @@ -103,12 +103,12 @@ - + - + @@ -141,7 +141,7 @@ - + @@ -171,7 +171,7 @@ - + @@ -214,18 +214,18 @@ - + - + - + @@ -261,6 +261,172 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +