diff --git a/Board.cpp b/Board.cpp index a5f95f5..f5901e2 100644 --- a/Board.cpp +++ b/Board.cpp @@ -93,9 +93,8 @@ void Board::setupInitialPosition() { */ void Board::clearBoard() { for(int i = 0; i < 8; i++) { - for(int j = 0; j < 8; j++) { + for(int j = 0; j < 8; j++) boardGrid[i][j] = nullptr; - } } } @@ -167,8 +166,8 @@ int Board::setupFromFEN(std::string strFEN) { boardGrid[rank][file] = std::make_unique(PIECE_WHITE); wKingPlaced = true; // store king position for later - whiteKing.file = file; - whiteKing.rank = rank; + whiteKing.file = static_cast(file); + whiteKing.rank = static_cast(rank); file++; } break; @@ -181,8 +180,8 @@ int Board::setupFromFEN(std::string strFEN) { boardGrid[rank][file] = std::make_unique(PIECE_BLACK); bKingPlaced = true; // store king position for later - blackKing.file = file; - blackKing.rank = rank; + blackKing.file = static_cast(file); + blackKing.rank = static_cast(rank); file++; } break; @@ -241,8 +240,11 @@ int Board::setupFromFEN(std::string strFEN) { // ====== END OF FIELD 2 ====== // ====== START OF FIELD 3 ====== std::string k = "k", K = "K", q = "q", Q = "Q"; - auto &bKing = this->getPieceAt(blackKing); - auto &wKing = this->getPieceAt(whiteKing); + // yeah this is hacky af but it works... blame SO + Piece *baseBKing = getPieceAt(blackKing).get(); + Piece *baseWKing = getPieceAt(whiteKing).get(); + King *bKing = dynamic_cast(baseBKing); + King *wKing = dynamic_cast(baseWKing); if(splitFEN[2] == "-") { // nobody can castle, either side bKing->setCastleKS(false); @@ -269,19 +271,17 @@ int Board::setupFromFEN(std::string strFEN) { // ====== END OF FIELD 3 ====== // ====== START OF FIELD 4 ====== // if a pawn has just moved and can be attacked enPassant, it will be listed here - - -#ifdef DEBUG - for(auto &ranks : boardGrid) { - for(auto &files : ranks) { - if(files) - std::cout << files->getPieceName() << " "; - else - std::cout << "E "; - } - std::cout << "\n"; - } -#endif +//#ifdef DEBUG +// for(auto &ranks : boardGrid) { +// for(auto &files : ranks) { +// if(files) +// std::cout << files->getPieceName() << " "; +// else +// std::cout << "E "; +// } +// std::cout << "\n"; +// } +//#endif return 0; } @@ -302,13 +302,16 @@ void Board::movePiece(Square from, Square to) { } void Board::deserializeBoard(uint64_t incomingBoard) { - uint8_t boardRows[8]; + union { + uint8_t boardRows[8]; + uint64_t fullBoard; + } serialBoard; for(int i = 0; i < 8; i++) - boardRows[i] = (incomingBoard >> (8 * i)) & 0xFF; + 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[square.rank][square.file]; + 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 0efc5d3..61f85bb 100644 --- a/Piece.cpp +++ b/Piece.cpp @@ -35,17 +35,16 @@ bool Piece::finalMoveChecks(std::vector *moveList, Board &board) { } // *INDENT-OFF* -// ## -// ## ## -// ## -// ## ## ## # ## #### -// ## # ## ## ## ## # -// ### ## ## ## ## # -// ## # ## ## ## ### -// ## ## ## ## ### ## -// #### -// # # -// #### +// +// ## # +// # +// # ## ## # ## ## # +// # # # # # # # +// ## # # # # # +// # # # # # # # +// ## ## ##### ### ## ### +// # +// ### // *INDENT-ON* bool King::checkForCheck(Board &board) const { std::vector> kingVulnerable = { @@ -182,13 +181,15 @@ std::vector King::getLegalMoves(const Square &from, Board &board) const { } // *INDENT-OFF* -// ## -// ## -// ## # ### ### ## ## -// #### ## ## ## ## ## # -// ## ## ## ## ## ### -// ## ## ## ## ## ## # -// ## ### ### ## ## +// +// ## +// # +// # ### ## ## # ## +// # # # # # # # +// # # # # # ## +// # # # # # # # +// #### ## ## ## ## +// // *INDENT-OM* std::vector Rook::getLegalMoves(const Square &from, Board &board) const { @@ -222,13 +223,13 @@ std::vector Rook::getLegalMoves(const Square &from, Board &board) const { } // *INDENT-OFF* -// ### ## ## ### ### # ## -// ## # ## # ## # ## # ## ## -// ## # ## # #### #### ## ## -// ## # ## # ## ## ## ## -// ### ### ### ### ## ### -// ## -// ### +// ## # ## ## ## ## # ## +// # # # # # # # # # # +// # # # # ### ### # # +// # # # # # # # # +// ### ## # ### ### ### ## +// # +// ### // *INDENT-ON* std::vector Queen::getLegalMoves(const Square &from, Board &board) const { @@ -269,18 +270,15 @@ std::vector Queen::getLegalMoves(const Square &from, Board &board) const { } // *INDENT-OFF* -// -// ## -// ## ## ## # -// ## ## ## -// ## ## # ## ## #### #### ### -// ## # ## ## ## ## # ## # ## -// ### ## ## ## ## # ## # ## -// ## # ## ## ## ### ## # ## -// ## ## ## ### ## ## ## ## ## -// #### -// # # -// #### +// ## # ## # +// # # # +// # ## # ## ## ## # ### #### +// # # # # # # # # # # +// ## # # # # # # # # +// # # # # # # # # # # # +// ## ## ### ## ##### ### ### ## ## +// # +// ### // *INDENT-ON* std::vector Knight::getLegalMoves(const Square &from, Board &board) const { @@ -299,17 +297,15 @@ std::vector Knight::getLegalMoves(const Square &from, Board &board) const } // *INDENT-OFF* -// -// ## -// ## ## ## -// ## ## -// ### ## ## #### ### ### -// ## # ## ## ## # ## ## ## # -// ## # ## ### ## # ## ## ## # -// ## # ## ## ## # ## ## ## # -// ### ## ### ## ## ### ### -// ## -// ### +// ## # ## +// # # +// ### ## ### ### ## # ## +// # # # # # # # # # # +// # # # ## # # # # # # +// # # # # # # # # # # +// #### ##### ### ### ## ## ### +// # +// ### // *INDENT-ON* std::vector Bishop::getLegalMoves(const Square &from, Board &board) const { @@ -324,13 +320,14 @@ std::vector Bishop::getLegalMoves(const Square &from, Board &board) const } // *INDENT-OFF* -// ### ## # # ## # ## -// ## # # ## # # # ## ## -// ## # ### # ### ## ## -// ## # # # #### ## ## -// ### ##### # # ## ### -// ## -// ### +// +// # ## ## # # ## # ## +// # # # # # # # # +// # # ### # # # # # +// # # # # # # # # +// ### ## # # # ### ## +// # +// ### // *INDENT-ON* std::vector Pawn::getLegalMoves(const Square &from, Board &board) const { diff --git a/inc/Board.hpp b/inc/Board.hpp index 286437f..c0dbaac 100644 --- a/inc/Board.hpp +++ b/inc/Board.hpp @@ -13,7 +13,6 @@ #include #include #include -#include #include "Piece.hpp" @@ -24,24 +23,28 @@ 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; // this doesn't seem to do anything + 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(); + + 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); }; #endif // BOARD_HPP diff --git a/inc/Piece.hpp b/inc/Piece.hpp index a57dc95..762c57d 100644 --- a/inc/Piece.hpp +++ b/inc/Piece.hpp @@ -11,12 +11,12 @@ /** @file Piece.hpp * @brief The header files for the Piece class. - * + * * This file contains not only the definition of the Piece class itself, but also all * of the derived classes (King, Queen, Rook, etc.) I was considering having them be in * separate files, but each derived class does not contain that much code, so keeping them * all in one place seemed easier to deal with. This may change in the future! - * + * * Also defined here are important enums that help abstract chess terms, for example the * PieceType, PieceColour, Rank, File, etc. These allow the code to be more readable and * it helps make it obvious what certain bits of code are doing. @@ -77,7 +77,7 @@ struct Move { /** * @class Piece Piece.hpp "inc/Piece.hpp" * @brief A class abstracting chess pieces - * + * * This is a polymorphic base class containing the basic properties all chess pieces have. * It is intended to be extended by a child class for each piece Type, i.e. King, Queen, etc. */ @@ -88,7 +88,8 @@ class Piece { PieceColour colour; PieceType pieceType; std::string pieceName; /**< std::string pieceName is a string showing the full name of the piece, i.e. "PAWN" */ - std::string pieceSymbol; /**< std::string pieceSymbol is a single character, using the standard algebraic chess notation, ie N for Knight */ + std::string + pieceSymbol; /**< std::string pieceSymbol is a single character, using the standard algebraic chess notation, ie N for Knight */ bool hasMoved = false; public: @@ -96,7 +97,7 @@ class Piece { Piece(PieceColour pColour) : colour(pColour) { } virtual ~Piece(); - virtual std::vector getLegalMoves(const Square &from, Board &board) const; + PieceColour getColour() const { return colour; @@ -106,7 +107,7 @@ class Piece { return pieceName; } - char getPieceSymbol() const { + std::string getPieceSymbol() const { return pieceSymbol; } @@ -121,7 +122,7 @@ class Piece { * A virtual const function that should get all legal moves for a piece, but it may * include some moves that need to be pruned, like those exposing the king to check. * Still working out how to handle this. - * + * * @param from the Square the Piece is currently on * @param board A reference to the Board we are analyzing * @return std::vector of Move: a list of all potentially legal moves @@ -129,7 +130,7 @@ class Piece { virtual std::vector getLegalMoves(const Square &from, Board &board) const; bool finalMoveChecks(std::vector *moveList, Board &board); - + }; class King : public Piece { @@ -139,7 +140,7 @@ class King : public Piece { King(PieceColour colour) : Piece(colour) { pieceName = "King"; - pieceSymbol = 'K'; + pieceSymbol = "K"; pieceType = KING; } virtual std::vector getLegalMoves(const Square &from, Board &board) const override; @@ -149,14 +150,12 @@ class King : public Piece { bool checkForCastle(enum CastleSide side) const { if(side == KINGSIDE) return canCastleKS; - else if(side == QUEENSIDE) return canCastleQS; - else return false; } - + void setCastleQS(bool canCastle) { canCastleQS = canCastle; } @@ -175,7 +174,7 @@ class Rook : public Piece { Rook(PieceColour colour) : Piece(colour) { pieceName = "Rook"; - pieceSymbol = 'R'; + pieceSymbol = "R"; pieceType = ROOK; } virtual std::vector getLegalMoves(const Square &from, Board &board) const override; @@ -187,7 +186,7 @@ class Queen : public Piece { Queen(PieceColour colour) : Piece(colour) { pieceName = "Queen"; - pieceSymbol = 'Q'; + pieceSymbol = "Q"; pieceType = QUEEN; } virtual std::vector getLegalMoves(const Square &from, Board &board) const override; @@ -199,7 +198,7 @@ class Knight : public Piece { Knight(PieceColour colour) : Piece(colour) { pieceName = "Knight"; - pieceSymbol = 'N'; + pieceSymbol = "N"; pieceType = KNIGHT; } virtual std::vector getLegalMoves(const Square &from, Board &board) const override; @@ -211,7 +210,7 @@ class Bishop : public Piece { Bishop(PieceColour colour) : Piece(colour) { pieceName = "Bishop"; - pieceSymbol = 'B'; + pieceSymbol = "B"; pieceType = BISHOP; } virtual std::vector getLegalMoves(const Square &from, Board &board) const override; @@ -223,12 +222,12 @@ class Pawn : public Piece { Pawn(PieceColour colour) : Piece(colour) { pieceName = "Pawn"; - pieceSymbol = 'P'; + pieceSymbol = "P"; pieceType = PAWN; } virtual std::vector getLegalMoves(const Square &from, Board &board) const override; void promote(const Square &promotionSquare, Board &board, PieceType promoteTo); -protected: + protected: bool vulnEnPassant = false; bool firstMove = true; }; diff --git a/inc/strFuncs.hpp b/inc/strFuncs.hpp index 2ea4945..ce929d0 100644 --- a/inc/strFuncs.hpp +++ b/inc/strFuncs.hpp @@ -1,4 +1,4 @@ -/* +/* * File: strFuncs.hpp * Author: amr * diff --git a/main.cpp b/main.cpp index fa1ca31..b014297 100644 --- a/main.cpp +++ b/main.cpp @@ -157,8 +157,8 @@ void initInterrupts(void) { INTCONbits.MVEC = 1; // Enable multi-vector interrupts __builtin_enable_interrupts(); IPC10bits.DMA0IP = 3; // Priority level 3 - IFS1CLR = _IFS1_DMA0IF_MASK; // Clear interrupt flag - IEC1SET = _IEC1_DMA0IE_MASK; // Enable DMA1 interrupt +// IFS1CLR = _IFS1_DMA0IF_MASK; // Clear interrupt flag +// IEC1SET = _IEC1_DMA0IE_MASK; // Enable DMA1 interrupt } void startSPI_DMA_Transfer(void) { @@ -179,7 +179,6 @@ extern "C" int main(void) { initInterrupts(); Board gameBoard; NeoPixel boardLights(64); - gameBoard.setupInitialPosition(); while(1) { } @@ -193,7 +192,7 @@ extern "C" void __ISR(_DMA0_VECTOR, IPL3AUTO) DMA0Handler(void) { DCH0INTCLR = _DCH0INT_CHBCIF_MASK; // Clear block complete flag // DMA RX completed — spi_rx_buffer[] now contains the data } - IFS1CLR = _IFS1_DMA0IF_MASK; // Clear global DMA0 IRQ flag +// IFS1CLR = _IFS1_DMA0IF_MASK; // Clear global DMA0 IRQ flag } //extern "C" void __ISR(_USB1_VECTOR, IPL4AUTO) USBHandler(void) {