From f2ce83335c7e1a064ae3bf62bd378833223fae19 Mon Sep 17 00:00:00 2001 From: "A.M. Rowsell" Date: Tue, 22 Sep 2026 02:06:49 -0400 Subject: [PATCH] style: implement clang-format styling parameters --- .clang-format | 11 ++ Board.cpp | 305 +++++++++++++++++++++++------------------------ Piece.cpp | 237 ++++++++++++++++++------------------ graphics.cpp | 119 +++++++----------- inc/Board.hpp | 90 +++++--------- inc/NeoPixel.hpp | 5 +- inc/Piece.hpp | 86 +++++-------- inc/strFuncs.hpp | 15 +-- main.cpp | 144 +++++++++++----------- strFuncs.cpp | 3 +- 10 files changed, 465 insertions(+), 550 deletions(-) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..7087d8b --- /dev/null +++ b/.clang-format @@ -0,0 +1,11 @@ +BasedOnStyle: LLVM +IndentWidth: 4 +TabWidth: 4 +UseTab: Never +PointerAlignment: Right +ReferenceAlignment: Right +SpaceBeforeParens: Never +BreakBeforeBraces: Attach +AccessModifierOffset: -2 +AllowShortFunctionsOnASingleLine: Inline +ColumnLimit: 120 diff --git a/Board.cpp b/Board.cpp index b78970c..381174b 100644 --- a/Board.cpp +++ b/Board.cpp @@ -36,8 +36,7 @@ Board::Board() { boardGrid[i].resize(8); } -Board::~Board() { -} +Board::~Board() {} void Board::setupInitialPosition() { // set up the board grid with smart pointers @@ -45,46 +44,46 @@ void Board::setupInitialPosition() { playerTurn = PL_WHITE; for(int i = 0; i < 8; i++) { switch(i) { - case 0: - // white back rank - boardGrid[i][0] = std::make_unique(PIECE_WHITE); - boardGrid[i][1] = std::make_unique(PIECE_WHITE); - boardGrid[i][2] = std::make_unique(PIECE_WHITE); - boardGrid[i][3] = std::make_unique(PIECE_WHITE); - boardGrid[i][4] = std::make_unique(PIECE_WHITE); - boardGrid[i][5] = std::make_unique(PIECE_WHITE); - boardGrid[i][6] = std::make_unique(PIECE_WHITE); - boardGrid[i][7] = std::make_unique(PIECE_WHITE); - break; - case 1: - // white pawn rank - for(int j = 0; j <= 8; j++) - boardGrid[i][j] = std::make_unique(PIECE_WHITE); - break; - case 2: - case 3: - case 4: - case 5: - for(int j = 0; j <= 8; j++) - boardGrid[i][j] = nullptr; - break; - case 6: - // black pawn rank - for(int j = 0; j <= 8; j++) - boardGrid[i][j] = std::make_unique(PIECE_BLACK); - break; - case 7: - // black back rank - boardGrid[i][0] = std::make_unique(PIECE_BLACK); - boardGrid[i][1] = std::make_unique(PIECE_BLACK); - boardGrid[i][2] = std::make_unique(PIECE_BLACK); - boardGrid[i][3] = std::make_unique(PIECE_BLACK); - boardGrid[i][4] = std::make_unique(PIECE_BLACK); - boardGrid[i][5] = std::make_unique(PIECE_BLACK); - boardGrid[i][6] = std::make_unique(PIECE_BLACK); - boardGrid[i][7] = std::make_unique(PIECE_BLACK); - default: - break; + case 0: + // white back rank + boardGrid[i][0] = std::make_unique(PIECE_WHITE); + boardGrid[i][1] = std::make_unique(PIECE_WHITE); + boardGrid[i][2] = std::make_unique(PIECE_WHITE); + boardGrid[i][3] = std::make_unique(PIECE_WHITE); + boardGrid[i][4] = std::make_unique(PIECE_WHITE); + boardGrid[i][5] = std::make_unique(PIECE_WHITE); + boardGrid[i][6] = std::make_unique(PIECE_WHITE); + boardGrid[i][7] = std::make_unique(PIECE_WHITE); + break; + case 1: + // white pawn rank + for(int j = 0; j <= 8; j++) + boardGrid[i][j] = std::make_unique(PIECE_WHITE); + break; + case 2: + case 3: + case 4: + case 5: + for(int j = 0; j <= 8; j++) + boardGrid[i][j] = nullptr; + break; + case 6: + // black pawn rank + for(int j = 0; j <= 8; j++) + boardGrid[i][j] = std::make_unique(PIECE_BLACK); + break; + case 7: + // black back rank + boardGrid[i][0] = std::make_unique(PIECE_BLACK); + boardGrid[i][1] = std::make_unique(PIECE_BLACK); + boardGrid[i][2] = std::make_unique(PIECE_BLACK); + boardGrid[i][3] = std::make_unique(PIECE_BLACK); + boardGrid[i][4] = std::make_unique(PIECE_BLACK); + boardGrid[i][5] = std::make_unique(PIECE_BLACK); + boardGrid[i][6] = std::make_unique(PIECE_BLACK); + boardGrid[i][7] = std::make_unique(PIECE_BLACK); + default: + break; } } return; @@ -99,7 +98,6 @@ void Board::clearBoard() { } } - int Board::setupFromFEN(std::string strFEN) { std::vector splitFEN = split(strFEN, ' '); if(splitFEN.size() != 6) // a valid FEN *must* contain 6 fields @@ -113,104 +111,104 @@ int Board::setupFromFEN(std::string strFEN) { for(auto &ranks : splitField1) { for(auto &sym : ranks) { switch(sym) { - case 'R': - // white rook - boardGrid[rank][file] = std::make_unique(PIECE_WHITE); - file++; - break; - case 'r': - // black rook - boardGrid[rank][file] = std::make_unique(PIECE_BLACK); - file++; - break; - case 'N': - // white knight - boardGrid[rank][file] = std::make_unique(PIECE_WHITE); - file++; - break; - case 'n': - // black knight - boardGrid[rank][file] = std::make_unique(PIECE_BLACK); - file++; - break; - case 'B': - // white bishop - boardGrid[rank][file] = std::make_unique(PIECE_WHITE); - file++; - break; - case 'b': - // black bishop - boardGrid[rank][file] = std::make_unique(PIECE_BLACK); - file++; - break; - case 'Q': - // white queen - boardGrid[rank][file] = std::make_unique(PIECE_WHITE); - file++; - break; - case 'q': - // black queen; - boardGrid[rank][file] = std::make_unique(PIECE_BLACK); - file++; - break; - case 'K': - // white king - if(wKingPlaced) { - // invalid FEN, more than 1 white king - return -1; - } else { - boardGrid[rank][file] = std::make_unique(PIECE_WHITE); - wKingPlaced = true; - // store king position for later - whiteKing.file = static_cast(file); - whiteKing.rank = static_cast(rank); - file++; - } - break; - case 'k': - // black king - if(bKingPlaced) { - // invalid FEN, more than 1 black king - return -1; - } else { - boardGrid[rank][file] = std::make_unique(PIECE_BLACK); - bKingPlaced = true; - // store king position for later - blackKing.file = static_cast(file); - blackKing.rank = static_cast(rank); - file++; - } - break; - case 'P': - // white pawn - boardGrid[rank][file] = std::make_unique(PIECE_WHITE); - file++; - break; - case 'p': - // black pawn - boardGrid[rank][file] = std::make_unique(PIECE_BLACK); - file++; - break; - case '1': - case '2': - case '3': - case '4': - case '5': - case '6': - case '7': - case '8': { - std::string skipStr(1, sym); - skip = atoi(skipStr.c_str()); - //skip--; // fix off by 1 error - while(skip--) { - boardGrid[rank][file++] = nullptr; // remove reference - } - break; - } - default: - // invalid character? + case 'R': + // white rook + boardGrid[rank][file] = std::make_unique(PIECE_WHITE); + file++; + break; + case 'r': + // black rook + boardGrid[rank][file] = std::make_unique(PIECE_BLACK); + file++; + break; + case 'N': + // white knight + boardGrid[rank][file] = std::make_unique(PIECE_WHITE); + file++; + break; + case 'n': + // black knight + boardGrid[rank][file] = std::make_unique(PIECE_BLACK); + file++; + break; + case 'B': + // white bishop + boardGrid[rank][file] = std::make_unique(PIECE_WHITE); + file++; + break; + case 'b': + // black bishop + boardGrid[rank][file] = std::make_unique(PIECE_BLACK); + file++; + break; + case 'Q': + // white queen + boardGrid[rank][file] = std::make_unique(PIECE_WHITE); + file++; + break; + case 'q': + // black queen; + boardGrid[rank][file] = std::make_unique(PIECE_BLACK); + file++; + break; + case 'K': + // white king + if(wKingPlaced) { + // invalid FEN, more than 1 white king return -1; - break; + } else { + boardGrid[rank][file] = std::make_unique(PIECE_WHITE); + wKingPlaced = true; + // store king position for later + whiteKing.file = static_cast(file); + whiteKing.rank = static_cast(rank); + file++; + } + break; + case 'k': + // black king + if(bKingPlaced) { + // invalid FEN, more than 1 black king + return -1; + } else { + boardGrid[rank][file] = std::make_unique(PIECE_BLACK); + bKingPlaced = true; + // store king position for later + blackKing.file = static_cast(file); + blackKing.rank = static_cast(rank); + file++; + } + break; + case 'P': + // white pawn + boardGrid[rank][file] = std::make_unique(PIECE_WHITE); + file++; + break; + case 'p': + // black pawn + boardGrid[rank][file] = std::make_unique(PIECE_BLACK); + file++; + break; + case '1': + case '2': + case '3': + case '4': + case '5': + case '6': + case '7': + case '8': { + std::string skipStr(1, sym); + skip = atoi(skipStr.c_str()); + // skip--; // fix off by 1 error + while(skip--) { + boardGrid[rank][file++] = nullptr; // remove reference + } + break; + } + default: + // invalid character? + return -1; + break; } if(file > 7) { file = 0; @@ -267,17 +265,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; } @@ -289,7 +287,8 @@ void Board::nextTurn() { void Board::movePiece(Square from, Square to) { Piece *movingPiece = getPieceAt(from); - if(!movingPiece) return; + if(!movingPiece) + return; Square newEnPassantTarget = {INVALID_RANK, INVALID_FILE}; @@ -309,7 +308,7 @@ void Board::movePiece(Square from, Square to) { // Move the piece setPieceAt(to, std::move(boardGrid[from.rank][from.file])); clearSquare(from); - + enPassantTargetSquare = newEnPassantTarget; } diff --git a/Piece.cpp b/Piece.cpp index e459ec3..0818ebe 100644 --- a/Piece.cpp +++ b/Piece.cpp @@ -37,26 +37,26 @@ bool Piece::finalMoveChecks(std::vector *moveList, Board &board) { // *INDENT-OFF* // -// ## # -// # -// # ## ## # ## ## # -// # # # # # # # -// ## # # # # # -// # # # # # # # -// ## ## ##### ### ## ### -// # -// ### +// ## # +// # +// # ## ## # ## ## # +// # # # # # # # +// ## # # # # # +// # # # # # # # +// ## ## ##### ### ## ### +// # +// ### // *INDENT-ON* bool King::checkForCheck(Board &board) const { std::vector> kingVulnerable = { - {-1, 0}, // Up + {-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 + {-1, 1}, // up-right + {1, 0}, // Down + {1, -1}, // down-left + {1, 1}, // down-right + {0, -1}, // Left + {0, 1}, // Right {-1, -2}, // 1 {-1, 2}, // 2 {-2, -1}, // 3 @@ -73,14 +73,14 @@ bool King::checkForCheck(Board &board) const { std::vector King::getLegalMoves(const Square &from, Board &board) const { std::vector moveList; std::vector> directions = { - {-1, 0}, // Up + {-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 + {-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 @@ -89,10 +89,11 @@ std::vector King::getLegalMoves(const Square &from, Board &board) const { Square targetSquare{static_cast(r), static_cast(f)}; if(targetSquare.isValid()) { 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 + 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 break; } else { // otherwise it's one of our pieces @@ -128,11 +129,11 @@ std::vector King::getLegalMoves(const Square &from, Board &board) const { directions.insert(directions.end(), knightAttacks.begin(), knightAttacks.end()); // concatenate the knight attacks for(auto &moves : moveList) { Square startSquare = moves.to; // get the potential moveto square - for(auto &dir : directions) { // now go through all directions + for(auto &dir : directions) { // now go through all directions int r = startSquare.rank + dir[0]; int f = startSquare.file + dir[1]; 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 + bool knight = (abs(dir[0]) + abs(dir[1]) == 3) ? 1 : 0; // check if knight attack while(r >= 0 && r < 8 && f >= 0 && f < 8) { Square targetSquare{static_cast(r), static_cast(f)}; const Piece *target = board.getPieceAt(targetSquare); // access the square we're examining @@ -149,12 +150,14 @@ std::vector King::getLegalMoves(const Square &from, Board &board) const { // we are being attacked by a knight, so remove it moves.to.rank = INVALID_RANK; moves.to.file = INVALID_FILE; - } else if((target->getPieceType() == ROOK || target->getPieceType() == QUEEN) && (!diagonal && !knight)) { + } else if((target->getPieceType() == ROOK || target->getPieceType() == QUEEN) && + (!diagonal && !knight)) { // again, being attacked, remove it moves.to.rank = INVALID_RANK; moves.to.file = INVALID_FILE; } - if(target->getPieceType() == PAWN && (abs(r - startSquare.rank) == 1 && abs(f - startSquare.file) == 1)) { + if(target->getPieceType() == PAWN && + (abs(r - startSquare.rank) == 1 && abs(f - startSquare.file) == 1)) { moves.to.rank = INVALID_RANK; moves.to.file = INVALID_FILE; } @@ -173,23 +176,19 @@ std::vector King::getLegalMoves(const Square &from, Board &board) const { } } // will this actually work? - moveList.erase( - std::remove_if(moveList.begin(), moveList.end(), - [&](const auto & x) { - return !x.to.isValid(); - })); + moveList.erase(std::remove_if(moveList.begin(), moveList.end(), [&](const auto &x) { return !x.to.isValid(); })); return moveList; } // *INDENT-OFF* // -// ## -// # -// # ### ## ## # ## -// # # # # # # # -// # # # # # ## -// # # # # # # # -// #### ## ## ## ## +// ## +// # +// # ### ## ## # ## +// # # # # # # # +// # # # # # ## +// # # # # # # # +// #### ## ## ## ## // // *INDENT-OM* @@ -197,9 +196,9 @@ std::vector Rook::getLegalMoves(const Square &from, Board &board) const { std::vector moveList; const int directions[4][2] = { {-1, 0}, // Up - {1, 0}, // Down + {1, 0}, // Down {0, -1}, // Left - {0, 1} // Right + {0, 1} // Right }; for(auto &dir : directions) { int r = from.rank + dir[0]; @@ -222,26 +221,26 @@ std::vector Rook::getLegalMoves(const Square &from, Board &board) const { } // *INDENT-OFF* -// ## # ## ## ## ## # ## -// # # # # # # # # # # -// # # # # ### ### # # -// # # # # # # # # -// ### ## # ### ### ### ## -// # -// ### +// ## # ## ## ## ## # ## +// # # # # # # # # # # +// # # # # ### ### # # +// # # # # # # # # +// ### ## # ### ### ### ## +// # +// ### // *INDENT-ON* std::vector Queen::getLegalMoves(const Square &from, Board &board) const { std::vector moveList; std::vector> directions = { - {-1, 0}, // Up + {-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 + {-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 @@ -249,11 +248,12 @@ std::vector Queen::getLegalMoves(const Square &from, Board &board) const { int f = from.file + dir[1]; while(r >= 0 && r < 8 && f >= 0 && f < 8) { 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 - moveList.push_back({from, targetSquare}); // then again it's potentially legal + 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 + moveList.push_back({from, targetSquare}); // then again it's potentially legal break; } else { // otherwise it's one of our pieces @@ -267,29 +267,20 @@ std::vector Queen::getLegalMoves(const Square &from, Board &board) const { } // *INDENT-OFF* -// ## # ## # -// # # # -// # ## # ## ## ## # ### #### -// # # # # # # # # # # -// ## # # # # # # # # -// # # # # # # # # # # # -// ## ## ### ## ##### ### ### ## ## -// # -// ### +// ## # ## # +// # # # +// # ## # ## ## ## # ### #### +// # # # # # # # # # # +// ## # # # # # # # # +// # # # # # # # # # # # +// ## ## ### ## ##### ### ### ## ## +// # +// ### // *INDENT-ON* 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} - }; + std::vector> directions = {{-1, -2}, {-1, 2}, {-2, -1}, {-2, 1}, {1, -2}, {1, 2}, {2, -1}, {2, 1}}; for(auto &dir : directions) { int r = from.rank + dir[0]; int f = from.file + dir[1]; @@ -305,24 +296,24 @@ std::vector Knight::getLegalMoves(const Square &from, Board &board) const } // *INDENT-OFF* -// ## # ## -// # # -// ### ## ### ### ## # ## -// # # # # # # # # # # -// # # # ## # # # # # # -// # # # # # # # # # # -// #### ##### ### ### ## ## ### -// # -// ### +// ## # ## +// # # +// ### ## ### ### ## # ## +// # # # # # # # # # # +// # # # ## # # # # # # +// # # # # # # # # # # +// #### ##### ### ### ## ## ### +// # +// ### // *INDENT-ON* 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 + {-1, 1}, // up-right + {1, -1}, // down-left + {1, 1}, // down-right }; for(auto &dir : directions) { int r = from.rank + dir[0]; @@ -347,44 +338,45 @@ std::vector Bishop::getLegalMoves(const Square &from, Board &board) const // *INDENT-OFF* // -// # ## ## # # ## # ## -// # # # # # # # # -// # # ### # # # # # -// # # # # # # # # -// ### ## # # # ### ## -// # -// ### +// # ## ## # # ## # ## +// # # # # # # # # +// # # ### # # # # # +// # # # # # # # # +// ### ## # # # ### ## +// # +// ### // *INDENT-ON* std::vector Pawn::getLegalMoves(const Square &from, Board &board) const { std::vector moveList; - const int directions[2][4][2] = { - { - // black - {-1, 0}, // Up - {-2, 0}, // 2up first move - {-1, 1}, // attack to right - {-1, -1} // attack to left - }, - { - // white - {1, 0}, // down - {2, 0}, // 2down first move - {1, 1}, // attack to right - {1, -1} // attack to left - } - }; + const int directions[2][4][2] = {{ + // black + {-1, 0}, // Up + {-2, 0}, // 2up first move + {-1, 1}, // attack to right + {-1, -1} // attack to left + }, + { + // white + {1, 0}, // down + {2, 0}, // 2down first move + {1, 1}, // attack to right + {1, -1} // attack to left + }}; if(this->getColour() == PIECE_BLACK) { // go through black options for(auto &dir : directions[0]) { 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 + if(r >= 0 && r < 8 && f >= 0 && + f < 8) { // no need for a while loop as we only have finite moves in limited directions 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.isSquareEmpty(Square{static_cast(r + 1), static_cast(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; @@ -410,7 +402,8 @@ std::vector Pawn::getLegalMoves(const Square &from, Board &board) const { // go through white options 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 + if(r >= 0 && r < 8 && f >= 0 && + f < 8) { // no need for a while loop as we only have finite moves in limited directions Square targetSquare{static_cast(r), static_cast(f)}; const Piece *target = board.getPieceAt(targetSquare); if(dir[0] == 2 && !this->checkIfMoved()) { @@ -419,7 +412,7 @@ std::vector Pawn::getLegalMoves(const Square &from, Board &board) const { moveList.push_back({from, targetSquare}); else continue; - } else if(abs(dir[1]) == 1) { // attempted capture (diagonal) + } else if(abs(dir[1]) == 1) { // attempted capture (diagonal) if(target && target->getColour() != this->getColour()) { // can only move there if it's a capture // legal capture moveList.push_back({from, targetSquare}); diff --git a/graphics.cpp b/graphics.cpp index f0cdb81..4104b7d 100644 --- a/graphics.cpp +++ b/graphics.cpp @@ -1,90 +1,63 @@ #include -#include #include +#include /* Chess_Queen_White */ #define Chess_Queen_White_width 45 #define Chess_Queen_White_height 45 const uint8_t Chess_Queen_White_bits[] = { - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, - 0x00, 0x60, 0xf0, 0xc1, 0x00, 0x00, 0x00, 0xf0, 0x90, 0xe1, 0x01, 0x00, - 0x00, 0x98, 0x91, 0x31, 0x03, 0x00, 0x60, 0x98, 0xf1, 0x31, 0xc3, 0x00, - 0xf0, 0xf0, 0xe0, 0xe0, 0xe1, 0x01, 0x98, 0x61, 0x60, 0xe0, 0x30, 0x03, - 0x98, 0xe1, 0x60, 0x60, 0x30, 0x03, 0xf0, 0xe0, 0xe0, 0x60, 0xe0, 0x01, - 0xe0, 0xe0, 0xe0, 0x70, 0xe0, 0x00, 0xc0, 0xe0, 0xb1, 0x70, 0x70, 0x00, - 0xc0, 0xe1, 0xb1, 0x50, 0x38, 0x00, 0xc0, 0x63, 0xb1, 0x59, 0x38, 0x00, - 0x80, 0x62, 0x93, 0xc9, 0x3c, 0x00, 0x80, 0x66, 0x93, 0xcd, 0x36, 0x00, - 0x80, 0x6c, 0x1e, 0xcd, 0x36, 0x00, 0x80, 0x6d, 0x1e, 0xc7, 0x13, 0x00, - 0x80, 0x79, 0x0c, 0xc7, 0x11, 0x00, 0x00, 0x71, 0x0c, 0xc7, 0x19, 0x00, - 0x00, 0xf1, 0xff, 0xff, 0x19, 0x00, 0x00, 0xff, 0xff, 0xff, 0x1f, 0x00, - 0x00, 0x0f, 0x00, 0x00, 0x1f, 0x00, 0x00, 0x03, 0x00, 0x00, 0x0c, 0x00, - 0x00, 0x06, 0xff, 0x1f, 0x0e, 0x00, 0x00, 0xfc, 0xff, 0xff, 0x07, 0x00, - 0x00, 0x18, 0x00, 0x80, 0x03, 0x00, 0x00, 0x18, 0x00, 0x00, 0x01, 0x00, - 0x00, 0xf8, 0xff, 0xff, 0x01, 0x00, 0x00, 0xf8, 0xff, 0xff, 0x03, 0x00, - 0x00, 0x0c, 0x00, 0x00, 0x02, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x06, 0x00, - 0x00, 0x0c, 0x00, 0x00, 0x06, 0x00, 0x00, 0x06, 0x00, 0x00, 0x04, 0x00, - 0x00, 0xfc, 0xff, 0xff, 0x07, 0x00, 0x00, 0xe0, 0xff, 0xff, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 -}; + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, + 0x00, 0x60, 0xf0, 0xc1, 0x00, 0x00, 0x00, 0xf0, 0x90, 0xe1, 0x01, 0x00, 0x00, 0x98, 0x91, 0x31, 0x03, 0x00, + 0x60, 0x98, 0xf1, 0x31, 0xc3, 0x00, 0xf0, 0xf0, 0xe0, 0xe0, 0xe1, 0x01, 0x98, 0x61, 0x60, 0xe0, 0x30, 0x03, + 0x98, 0xe1, 0x60, 0x60, 0x30, 0x03, 0xf0, 0xe0, 0xe0, 0x60, 0xe0, 0x01, 0xe0, 0xe0, 0xe0, 0x70, 0xe0, 0x00, + 0xc0, 0xe0, 0xb1, 0x70, 0x70, 0x00, 0xc0, 0xe1, 0xb1, 0x50, 0x38, 0x00, 0xc0, 0x63, 0xb1, 0x59, 0x38, 0x00, + 0x80, 0x62, 0x93, 0xc9, 0x3c, 0x00, 0x80, 0x66, 0x93, 0xcd, 0x36, 0x00, 0x80, 0x6c, 0x1e, 0xcd, 0x36, 0x00, + 0x80, 0x6d, 0x1e, 0xc7, 0x13, 0x00, 0x80, 0x79, 0x0c, 0xc7, 0x11, 0x00, 0x00, 0x71, 0x0c, 0xc7, 0x19, 0x00, + 0x00, 0xf1, 0xff, 0xff, 0x19, 0x00, 0x00, 0xff, 0xff, 0xff, 0x1f, 0x00, 0x00, 0x0f, 0x00, 0x00, 0x1f, 0x00, + 0x00, 0x03, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x06, 0xff, 0x1f, 0x0e, 0x00, 0x00, 0xfc, 0xff, 0xff, 0x07, 0x00, + 0x00, 0x18, 0x00, 0x80, 0x03, 0x00, 0x00, 0x18, 0x00, 0x00, 0x01, 0x00, 0x00, 0xf8, 0xff, 0xff, 0x01, 0x00, + 0x00, 0xf8, 0xff, 0xff, 0x03, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x02, 0x00, 0x00, 0x0c, 0x00, 0x00, 0x06, 0x00, + 0x00, 0x0c, 0x00, 0x00, 0x06, 0x00, 0x00, 0x06, 0x00, 0x00, 0x04, 0x00, 0x00, 0xfc, 0xff, 0xff, 0x07, 0x00, + 0x00, 0xe0, 0xff, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; /* Chess King White */ #define Chess_King_White_width 45 #define Chess_King_White_height 45 const uint8_t Chess_King_White_bits[] = { - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x01, 0x00, 0x00, - 0x00, 0x00, 0xf0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, - 0x00, 0x00, 0xf0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x18, 0x03, 0x00, 0x00, - 0x00, 0x00, 0x08, 0x02, 0x00, 0x00, 0x00, 0x78, 0x08, 0xc2, 0x03, 0x00, - 0x00, 0xff, 0x0f, 0xfe, 0x1f, 0x00, 0x80, 0x03, 0x0f, 0x1e, 0x38, 0x00, - 0xc0, 0x00, 0x1c, 0x07, 0x60, 0x00, 0xc0, 0x00, 0x18, 0x03, 0x60, 0x00, - 0x60, 0x00, 0xb0, 0x01, 0xc0, 0x00, 0x60, 0x00, 0xe0, 0x00, 0xc0, 0x00, - 0x60, 0x00, 0xe0, 0x00, 0xc0, 0x00, 0x60, 0x00, 0x40, 0x00, 0xc0, 0x00, - 0xc0, 0x00, 0x40, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x40, 0x00, 0x60, 0x00, - 0x80, 0x01, 0x40, 0x00, 0x30, 0x00, 0x00, 0x03, 0xfe, 0x0f, 0x18, 0x00, - 0x00, 0xce, 0xff, 0x7f, 0x0e, 0x00, 0x00, 0xfc, 0x00, 0xe0, 0x07, 0x00, - 0x00, 0x30, 0xf0, 0x81, 0x01, 0x00, 0x00, 0x90, 0xff, 0x3f, 0x01, 0x00, - 0x00, 0xf0, 0x03, 0xf8, 0x01, 0x00, 0x00, 0x70, 0x00, 0xc0, 0x01, 0x00, - 0x00, 0x10, 0xfe, 0x0f, 0x01, 0x00, 0x00, 0xf0, 0xff, 0xff, 0x01, 0x00, - 0x00, 0xf0, 0x00, 0xe0, 0x01, 0x00, 0x00, 0x70, 0x00, 0xc0, 0x01, 0x00, - 0x00, 0xc0, 0x07, 0x7c, 0x00, 0x00, 0x00, 0x00, 0xff, 0x1f, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 -}; + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x01, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x01, 0x00, 0x00, + 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0x40, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00, 0x00, + 0x00, 0x00, 0xf0, 0x01, 0x00, 0x00, 0x00, 0x00, 0x18, 0x03, 0x00, 0x00, 0x00, 0x00, 0x08, 0x02, 0x00, 0x00, + 0x00, 0x78, 0x08, 0xc2, 0x03, 0x00, 0x00, 0xff, 0x0f, 0xfe, 0x1f, 0x00, 0x80, 0x03, 0x0f, 0x1e, 0x38, 0x00, + 0xc0, 0x00, 0x1c, 0x07, 0x60, 0x00, 0xc0, 0x00, 0x18, 0x03, 0x60, 0x00, 0x60, 0x00, 0xb0, 0x01, 0xc0, 0x00, + 0x60, 0x00, 0xe0, 0x00, 0xc0, 0x00, 0x60, 0x00, 0xe0, 0x00, 0xc0, 0x00, 0x60, 0x00, 0x40, 0x00, 0xc0, 0x00, + 0xc0, 0x00, 0x40, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x40, 0x00, 0x60, 0x00, 0x80, 0x01, 0x40, 0x00, 0x30, 0x00, + 0x00, 0x03, 0xfe, 0x0f, 0x18, 0x00, 0x00, 0xce, 0xff, 0x7f, 0x0e, 0x00, 0x00, 0xfc, 0x00, 0xe0, 0x07, 0x00, + 0x00, 0x30, 0xf0, 0x81, 0x01, 0x00, 0x00, 0x90, 0xff, 0x3f, 0x01, 0x00, 0x00, 0xf0, 0x03, 0xf8, 0x01, 0x00, + 0x00, 0x70, 0x00, 0xc0, 0x01, 0x00, 0x00, 0x10, 0xfe, 0x0f, 0x01, 0x00, 0x00, 0xf0, 0xff, 0xff, 0x01, 0x00, + 0x00, 0xf0, 0x00, 0xe0, 0x01, 0x00, 0x00, 0x70, 0x00, 0xc0, 0x01, 0x00, 0x00, 0xc0, 0x07, 0x7c, 0x00, 0x00, + 0x00, 0x00, 0xff, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; /* Chess Rook White */ #define Chess_Rook_White_width 45 #define Chess_Rook_White_height 45 const uint8_t Chess_Rook_White_bits[] = { - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xfc, 0xf8, 0xe3, 0x07, 0x00, - 0x00, 0xcc, 0x18, 0x63, 0x06, 0x00, 0x00, 0xcc, 0x1f, 0x7f, 0x06, 0x00, - 0x00, 0x0c, 0x00, 0x00, 0x06, 0x00, 0x00, 0xfc, 0xff, 0xff, 0x07, 0x00, - 0x00, 0xfc, 0xff, 0xff, 0x07, 0x00, 0x00, 0x18, 0x00, 0x00, 0x03, 0x00, - 0x00, 0xf0, 0xff, 0xff, 0x01, 0x00, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, - 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, - 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, - 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, - 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, - 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, - 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0xe0, 0xff, 0xff, 0x00, 0x00, - 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0xf0, 0xff, 0xff, 0x01, 0x00, - 0x00, 0xf8, 0xff, 0xff, 0x03, 0x00, 0x00, 0x18, 0x00, 0x00, 0x03, 0x00, - 0x00, 0x18, 0x00, 0x00, 0x03, 0x00, 0x00, 0xf8, 0xff, 0xff, 0x03, 0x00, - 0x00, 0xff, 0xff, 0xff, 0x1f, 0x00, 0x00, 0x03, 0x00, 0x00, 0x18, 0x00, - 0x00, 0xff, 0xff, 0xff, 0x1f, 0x00, 0x00, 0xff, 0xff, 0xff, 0x1f, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 -}; \ No newline at end of file + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0xfc, 0xf8, 0xe3, 0x07, 0x00, 0x00, 0xcc, 0x18, 0x63, 0x06, 0x00, 0x00, 0xcc, 0x1f, 0x7f, 0x06, 0x00, + 0x00, 0x0c, 0x00, 0x00, 0x06, 0x00, 0x00, 0xfc, 0xff, 0xff, 0x07, 0x00, 0x00, 0xfc, 0xff, 0xff, 0x07, 0x00, + 0x00, 0x18, 0x00, 0x00, 0x03, 0x00, 0x00, 0xf0, 0xff, 0xff, 0x01, 0x00, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, + 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, + 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, + 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, + 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0xe0, 0xff, 0xff, 0x00, 0x00, + 0x00, 0x60, 0x00, 0xc0, 0x00, 0x00, 0x00, 0xf0, 0xff, 0xff, 0x01, 0x00, 0x00, 0xf8, 0xff, 0xff, 0x03, 0x00, + 0x00, 0x18, 0x00, 0x00, 0x03, 0x00, 0x00, 0x18, 0x00, 0x00, 0x03, 0x00, 0x00, 0xf8, 0xff, 0xff, 0x03, 0x00, + 0x00, 0xff, 0xff, 0xff, 0x1f, 0x00, 0x00, 0x03, 0x00, 0x00, 0x18, 0x00, 0x00, 0xff, 0xff, 0xff, 0x1f, 0x00, + 0x00, 0xff, 0xff, 0xff, 0x1f, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}; \ No newline at end of file diff --git a/inc/Board.hpp b/inc/Board.hpp index 045049e..ed486ee 100644 --- a/inc/Board.hpp +++ b/inc/Board.hpp @@ -8,7 +8,7 @@ /** @file Board.hpp * @brief The header file for the Board class - * + * * This contains the class definition for the logical representation of the chessboard * itself, as well as a few ancillary enums and forward declarations to make the code * easier to read (hopefully). Unlike the Piece.hpp header, there's no inheritance @@ -17,11 +17,11 @@ #ifndef BOARD_HPP #define BOARD_HPP +#include "Piece.hpp" #include #include #include #include -#include "Piece.hpp" class Piece; /** @enum Players @@ -32,34 +32,31 @@ struct Square; /** @class Board Board.hpp inc/Board.hpp * @brief This class abstracts the chess board itself - * + * * The heart of the entire code is contained in the member variable boardGrid, which * is a representation of the board. It's a 2D vector of Pieces, which are stored using * smart pointers. When other functions want to view the board, they do so with the included * getPieceAt() getters, which return raw pointers so ownership isn't transferred. - * + * * Outside functions/methods can also change the state of the board with setPieceAt() which * can allow one to move a piece (empty squares being nullptr) or add pieces to the Board when * required. */ class Board { private: - std::vector>> boardGrid; /**< This holds the game state. - * It is a 2D vector of Piece types, or nullptr for empty squares - * @image html boardGrid.svg "Logical diagram of boardGrid vector" */ + std::vector>> + boardGrid; /**< This holds the game state. + * It is a 2D vector of Piece types, or nullptr for empty squares + * @image html boardGrid.svg "Logical diagram of boardGrid vector" */ Players playerTurn; Square enPassantTargetSquare = {INVALID_RANK, INVALID_FILE}; // let's get super object-oriented, baby // these help the getters and setters access the boardGrid // and also make them shorter and less duplicative - std::unique_ptr &at(int r, int f) { - return boardGrid[r][f]; - } + std::unique_ptr &at(int r, int f) { return boardGrid[r][f]; } - const std::unique_ptr &at(int r, int f) const { - return boardGrid[r][f]; - } + const std::unique_ptr &at(int r, int f) const { return boardGrid[r][f]; } std::unique_ptr &at(const Square &sq) { return boardGrid[static_cast(sq.rank)][static_cast(sq.file)]; @@ -68,6 +65,7 @@ class Board { const std::unique_ptr &at(const Square &sq) const { return boardGrid[static_cast(sq.rank)][static_cast(sq.file)]; } + public: Board(); virtual ~Board(); @@ -75,69 +73,43 @@ class Board { // These are to allow Piece to access Board in a controlled way // instead of adding a friend declaration for every subclass // ----- 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(int r, int f) { return at(r, f).get(); } + const Piece *getPieceAt(int r, int f) const { return at(r, f).get(); } - Piece *getPieceAt(const Square &sq) { - return at(sq).get(); - } - const Piece *getPieceAt(const Square &sq) const { - return at(sq).get(); - } - Square getEnPassantTargetSquare() const { - return enPassantTargetSquare; - } + Piece *getPieceAt(const Square &sq) { return at(sq).get(); } + const Piece *getPieceAt(const Square &sq) const { return at(sq).get(); } + Square getEnPassantTargetSquare() const { return enPassantTargetSquare; } // ----- Setters ----- - void setPieceAt(int r, int f, std::unique_ptr piece) { - at(r, f) = std::move(piece); - } - void setPieceAt(const Square &sq, std::unique_ptr piece) { - at(sq) = std::move(piece); - } + void setPieceAt(int r, int f, std::unique_ptr piece) { at(r, f) = std::move(piece); } + 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(); - } + void clearSquare(int r, int f) { at(r, f).reset(); } + void clearSquare(const Square &sq) { at(sq).reset(); } - void setEnPassantTargetSquare(Square sq) { - enPassantTargetSquare = sq; - } - void clearEnPassantTargetSquare() { - enPassantTargetSquare = {INVALID_RANK, INVALID_FILE}; - } + void setEnPassantTargetSquare(Square sq) { enPassantTargetSquare = sq; } + void clearEnPassantTargetSquare() { enPassantTargetSquare = {INVALID_RANK, INVALID_FILE}; } // ----- Utility ----- - bool isSquareEmpty(int r, int f) const { - return at(r, f) == nullptr; - } - bool isSquareEmpty(const Square &sq) const { - return at(sq) == nullptr; - } + bool isSquareEmpty(int r, int f) const { return at(r, f) == nullptr; } + bool isSquareEmpty(const Square &sq) const { return at(sq) == nullptr; } /** A function to setup the initial board - * + * * This initializes the boardGrid with the black and white pieces * in their normal starting positions. All empty squares are set * to nullptr. */ void setupInitialPosition(); /** This function sets the entire board to nullptr - * + * * This should, if I understand correctly, destroy * all the Piece instances that existed because they will no longer * have any owners, and smart pointers should auto destruct. */ void clearBoard(); /** This does the actual moving of a piece from one square to another - * + * * Beware! This function does \e not validate if the move is legal. It * will simply do whatever it is told to do. So only call this once you * are positive the move is legal. @@ -151,12 +123,12 @@ class Board { void movePiece(Square from, Square to); void nextTurn(); /** This function takes a standard FEN string and sets up the board. - * + * * FEN is a standard notation that describes only an exact position and * other important things like whose turn it is, whether sides can castle, * but it does \e not include the move history of the game. This makes it * a simpler format for sharing particular interesting positions. - * + * * @param strFEN A std::string that contains a valid FEN position * @return 0 on success, -1 if the FEN string is invalid */ @@ -165,14 +137,14 @@ class Board { // serial shift register stuff uint64_t serialBoard = 0xFFFF00000000FFFF; // opening position /** This takes an incoming serial stream and detects if anything has moved. - * + * * The input is one bit per piece, so it can only detect the presence or * absence of pieces on any particular square. This is the crux of our design, * using simpler reed switches for detection instead of RFID or any other two-way * communication with the pieces. This moves a lot of the complexity into software, * though a lot of the complexity is actually quite easy to deal with from a software * point of view. - * + * * @param incomingBoard A 64-bit value, representing 1 bit per piece. */ void deserializeBoard(uint64_t incomingBoard); diff --git a/inc/NeoPixel.hpp b/inc/NeoPixel.hpp index 8df39f9..d1dd897 100644 --- a/inc/NeoPixel.hpp +++ b/inc/NeoPixel.hpp @@ -13,9 +13,7 @@ class NeoPixel { public: - NeoPixel(uint8_t stringLength) : length(stringLength) { - gpioPin = LATBbits.LATB9; - }; + NeoPixel(uint8_t stringLength) : length(stringLength) { gpioPin = LATBbits.LATB9; }; uint8_t setNeoPixel(uint8_t index, uint8_t red, uint8_t green, uint8_t blue); void allPixelsOff(void); @@ -27,4 +25,3 @@ class NeoPixel { }; #endif // NEOPIXEL_HPP - diff --git a/inc/Piece.hpp b/inc/Piece.hpp index 21dc240..78fc5af 100644 --- a/inc/Piece.hpp +++ b/inc/Piece.hpp @@ -22,34 +22,24 @@ * it helps make it obvious what certain bits of code are doing. */ -#include #include -#include -#include +#include #include +#include +#include #include class Board; -enum PieceType { - PAWN, BISHOP, KNIGHT, ROOK, QUEEN, KING, EMPTY -}; +enum PieceType { PAWN, BISHOP, KNIGHT, ROOK, QUEEN, KING, EMPTY }; -enum PieceColour { - PIECE_WHITE, PIECE_BLACK, PIECE_EMPTY -}; +enum PieceColour { PIECE_WHITE, PIECE_BLACK, PIECE_EMPTY }; -enum Rank { - R1 = 0, R2 = 1, R3 = 2, R4 = 3, R5 = 4, R6 = 5, R7 = 6, R8 = 7, INVALID_RANK = 255 -}; +enum Rank { R1 = 0, R2 = 1, R3 = 2, R4 = 3, R5 = 4, R6 = 5, R7 = 6, R8 = 7, INVALID_RANK = 255 }; -enum File { - A = 0, B = 1, C = 2, D = 3, E = 4, F = 5, G = 6, H = 7, INVALID_FILE = 255 -}; +enum File { A = 0, B = 1, C = 2, D = 3, E = 4, F = 5, G = 6, H = 7, INVALID_FILE = 255 }; -enum CastleSide { - KINGSIDE = 1, QUEENSIDE = 2 -}; +enum CastleSide { KINGSIDE = 1, QUEENSIDE = 2 }; /** @struct Square * A struct that represents a square on the chess board. @@ -62,9 +52,7 @@ struct Square { * A function to test if the square is a valid square on the chessboard * @return True if the square is within the chessboard, False otherwise */ - bool isValid() const { - return rank >= 0 && rank < 8 && file >= 0 && file < 8; - } + bool isValid() const { return rank >= 0 && rank < 8 && file >= 0 && file < 8; } }; /** @struct Move * A struct representing the start and end squares of a Move. @@ -84,40 +72,28 @@ struct Move { class Piece { private: friend class Board; + protected: 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 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 */ bool hasMoved = false; public: - - Piece(PieceColour pColour) : colour(pColour) { - } + Piece(PieceColour pColour) : colour(pColour) {} virtual ~Piece(); + PieceColour getColour() const { return colour; } - PieceColour getColour() const { - return colour; - } + std::string getPieceName() const { return pieceName; } - std::string getPieceName() const { - return pieceName; - } + std::string getPieceSymbol() const { return pieceSymbol; } - std::string getPieceSymbol() const { - return pieceSymbol; - } + PieceType getPieceType() const { return pieceType; } - PieceType getPieceType() const { - return pieceType; - } - - bool checkIfMoved() const { - return hasMoved; - } + bool checkIfMoved() const { return hasMoved; } /** * 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. @@ -130,14 +106,13 @@ class Piece { virtual std::vector getLegalMoves(const Square &from, Board &board) const; bool finalMoveChecks(std::vector *moveList, Board &board); - }; class King : public Piece { private: friend class Board; - public: + public: King(PieceColour colour) : Piece(colour) { pieceName = "King"; pieceSymbol = "K"; @@ -156,12 +131,9 @@ class King : public Piece { return false; } - void setCastleQS(bool canCastle) { - canCastleQS = canCastle; - } - void setCastleKS(bool canCastle) { - canCastleKS = canCastle; - } + void setCastleQS(bool canCastle) { canCastleQS = canCastle; } + void setCastleKS(bool canCastle) { canCastleKS = canCastle; } + protected: bool canCastleQS = true; bool canCastleKS = true; @@ -170,8 +142,8 @@ class King : public Piece { class Rook : public Piece { friend class Board; - public: + public: Rook(PieceColour colour) : Piece(colour) { pieceName = "Rook"; pieceSymbol = "R"; @@ -182,8 +154,8 @@ class Rook : public Piece { class Queen : public Piece { friend class Board; - public: + public: Queen(PieceColour colour) : Piece(colour) { pieceName = "Queen"; pieceSymbol = "Q"; @@ -194,8 +166,8 @@ class Queen : public Piece { class Knight : public Piece { friend class Board; - public: + public: Knight(PieceColour colour) : Piece(colour) { pieceName = "Knight"; pieceSymbol = "N"; @@ -206,8 +178,8 @@ class Knight : public Piece { class Bishop : public Piece { friend class Board; - public: + public: Bishop(PieceColour colour) : Piece(colour) { pieceName = "Bishop"; pieceSymbol = "B"; @@ -218,8 +190,8 @@ class Bishop : public Piece { class Pawn : public Piece { friend class Board; - public: + public: Pawn(PieceColour colour) : Piece(colour) { pieceName = "Pawn"; pieceSymbol = "P"; @@ -227,8 +199,8 @@ class Pawn : public Piece { } virtual std::vector getLegalMoves(const Square &from, Board &board) const override; void promote(const Square &promotionSquare, Board &board, PieceType promoteTo); + protected: bool firstMove = true; }; #endif // PIECE_HPP - diff --git a/inc/strFuncs.hpp b/inc/strFuncs.hpp index ce929d0..5ef99dd 100644 --- a/inc/strFuncs.hpp +++ b/inc/strFuncs.hpp @@ -6,18 +6,15 @@ */ #ifndef STRFUNCS_HPP -#define STRFUNCS_HPP +#define STRFUNCS_HPP -#include -#include -#include #include +#include +#include +#include -template -void split(const std::string &s, char delim, Out result); +template void split(const std::string &s, char delim, Out result); std::vector split(const std::string &s, char delim); - -#endif /* STRFUNCS_HPP */ - +#endif /* STRFUNCS_HPP */ diff --git a/main.cpp b/main.cpp index b014297..692631b 100644 --- a/main.cpp +++ b/main.cpp @@ -10,51 +10,51 @@ // PIC32MX270F256B Configuration Bit Settings // 'C' source line config statements // DEVCFG3 -#pragma config USERID = 0xBEEF // Enter Hexadecimal value (Enter Hexadecimal value) -#pragma config PMDL1WAY = OFF // Peripheral Module Disable Configuration (Allow multiple reconfigurations) -#pragma config IOL1WAY = OFF // Peripheral Pin Select Configuration (Allow multiple reconfigurations) -#pragma config FUSBIDIO = ON // USB USID Selection (Controlled by the USB Module) -#pragma config FVBUSONIO = ON // USB VBUS ON Selection (Controlled by USB Module) +#pragma config USERID = 0xBEEF // Enter Hexadecimal value (Enter Hexadecimal value) +#pragma config PMDL1WAY = OFF // Peripheral Module Disable Configuration (Allow multiple reconfigurations) +#pragma config IOL1WAY = OFF // Peripheral Pin Select Configuration (Allow multiple reconfigurations) +#pragma config FUSBIDIO = ON // USB USID Selection (Controlled by the USB Module) +#pragma config FVBUSONIO = ON // USB VBUS ON Selection (Controlled by USB Module) // DEVCFG2 -#pragma config FPLLIDIV = DIV_2 // PLL Input Divider (2x Divider) -#pragma config FPLLMUL = MUL_24 // PLL Multiplier (24x Multiplier) 96MHz -#pragma config UPLLIDIV = DIV_2 // USB PLL Input Divider (2x Divider) 48MHz -#pragma config UPLLEN = ON // USB PLL Enable (Enabled) -#pragma config FPLLODIV = DIV_8 // System PLL Output Clock Divider (PLL Divide by 8) 12MHz +#pragma config FPLLIDIV = DIV_2 // PLL Input Divider (2x Divider) +#pragma config FPLLMUL = MUL_24 // PLL Multiplier (24x Multiplier) 96MHz +#pragma config UPLLIDIV = DIV_2 // USB PLL Input Divider (2x Divider) 48MHz +#pragma config UPLLEN = ON // USB PLL Enable (Enabled) +#pragma config FPLLODIV = DIV_8 // System PLL Output Clock Divider (PLL Divide by 8) 12MHz // DEVCFG1 -#pragma config FNOSC = FRCPLL // Oscillator Selection Bits (Fast RC Osc with PLL) -#pragma config FSOSCEN = ON // Secondary Oscillator Enable (Enabled) -#pragma config IESO = ON // Internal/External Switch Over (Enabled) -#pragma config POSCMOD = OFF // Primary Oscillator Configuration (Primary osc disabled) -#pragma config OSCIOFNC = OFF // CLKO Output Signal Active on the OSCO Pin (Disabled) -#pragma config FPBDIV = DIV_2 // Peripheral Clock Divisor (Pb_Clk is Sys_Clk/2) -#pragma config FCKSM = CSDCMD // Clock Switching and Monitor Selection (Clock Switch Disable, FSCM Disabled) -#pragma config WDTPS = PS1048576 // Watchdog Timer Postscaler (1:1048576) -#pragma config WINDIS = OFF // Watchdog Timer Window Enable (Watchdog Timer is in Non-Window Mode) -#pragma config FWDTEN = OFF // Watchdog Timer Enable (WDT Disabled (SWDTEN Bit Controls)) -#pragma config FWDTWINSZ = WINSZ_25 // Watchdog Timer Window Size (Window Size is 25%) +#pragma config FNOSC = FRCPLL // Oscillator Selection Bits (Fast RC Osc with PLL) +#pragma config FSOSCEN = ON // Secondary Oscillator Enable (Enabled) +#pragma config IESO = ON // Internal/External Switch Over (Enabled) +#pragma config POSCMOD = OFF // Primary Oscillator Configuration (Primary osc disabled) +#pragma config OSCIOFNC = OFF // CLKO Output Signal Active on the OSCO Pin (Disabled) +#pragma config FPBDIV = DIV_2 // Peripheral Clock Divisor (Pb_Clk is Sys_Clk/2) +#pragma config FCKSM = CSDCMD // Clock Switching and Monitor Selection (Clock Switch Disable, FSCM Disabled) +#pragma config WDTPS = PS1048576 // Watchdog Timer Postscaler (1:1048576) +#pragma config WINDIS = OFF // Watchdog Timer Window Enable (Watchdog Timer is in Non-Window Mode) +#pragma config FWDTEN = OFF // Watchdog Timer Enable (WDT Disabled (SWDTEN Bit Controls)) +#pragma config FWDTWINSZ = WINSZ_25 // Watchdog Timer Window Size (Window Size is 25%) // DEVCFG0 -#pragma config JTAGEN = ON // JTAG Enable (JTAG Port Enabled) -#pragma config ICESEL = ICS_PGx2 // ICE/ICD Comm Channel Select (Communicate on PGEC2/PGED2) -#pragma config PWP = OFF // Program Flash Write Protect (Disable) -#pragma config BWP = OFF // Boot Flash Write Protect bit (Protection Disabled) -#pragma config CP = OFF // Code Protect (Protection Disabled) +#pragma config JTAGEN = ON // JTAG Enable (JTAG Port Enabled) +#pragma config ICESEL = ICS_PGx2 // ICE/ICD Comm Channel Select (Communicate on PGEC2/PGED2) +#pragma config PWP = OFF // Program Flash Write Protect (Disable) +#pragma config BWP = OFF // Boot Flash Write Protect bit (Protection Disabled) +#pragma config CP = OFF // Code Protect (Protection Disabled) // #pragma config statements should precede project file includes. // Use project enums instead of #define for ON and OFF. -#include -#include // for ISR macros +#include // for ISR macros #include +#include #include "inc/Board.hpp" -#include "inc/Piece.hpp" #include "inc/NeoPixel.hpp" +#include "inc/Piece.hpp" #define F_CPU 12000000UL // 12MHz -#define F_PER 6000000UL // 6MHz +#define F_PER 6000000UL // 6MHz #define SPI_BAUD 1000000 // 1MHz hopefully volatile uint8_t spi_rx_buffer[8]; @@ -69,7 +69,8 @@ extern "C" int open(const char *buf, int flags, int mode) { void sendChar(uint8_t c) { U1TXREG = c; - while(!U1STAbits.TRMT); // wait for transmission + while(!U1STAbits.TRMT) + ; // wait for transmission return; } @@ -103,7 +104,7 @@ uint8_t appInfo(uint8_t *msg, uint16_t len) { * */ #define SL_TRIS TRISBbits.TRISB9 -#define SL_LAT LATBbits.LATB9 +#define SL_LAT LATBbits.LATB9 uint8_t initSystem(void) { /* set up GPIO */ @@ -111,45 +112,45 @@ uint8_t initSystem(void) { SYSKEY = 0xAA996655; SYSKEY = 0x556699AA; CFGCON &= ~(1 << 13); // unlock PPS - SDI1R = 0b0100; // RB8 - RPA0R = 0b0001; // U1TX - SYSKEY = 0x12345678; // lock SYSKEY - ANSELBCLR = 0xFFFF; // port B all digital - SL_TRIS = 0; // RA0 as output - SL_LAT = 1; // set high + SDI1R = 0b0100; // RB8 + RPA0R = 0b0001; // U1TX + SYSKEY = 0x12345678; // lock SYSKEY + ANSELBCLR = 0xFFFF; // port B all digital + SL_TRIS = 0; // RA0 as output + SL_LAT = 1; // set high /* Set up SPI1 */ - SPI1CON = 0; // reset SPI config + SPI1CON = 0; // reset SPI config SPI1BRG = (F_PER / (2 * SPI_BAUD)) - 1; // calculate for 1MHz - SPI1STATCLR = _SPI1STAT_SPIROV_MASK; // Clear overflow - SPI1CONbits.MSTEN = 1; // Enable Master mode - SPI1CONbits.CKP = 0; // Clock idle low - SPI1CONbits.CKE = 1; // Transmit on falling edge - SPI1CONbits.SMP = 1; // Input sampled at end - SPI1CONbits.ON = 1; // Enable SPI + SPI1STATCLR = _SPI1STAT_SPIROV_MASK; // Clear overflow + SPI1CONbits.MSTEN = 1; // Enable Master mode + SPI1CONbits.CKP = 0; // Clock idle low + SPI1CONbits.CKE = 1; // Transmit on falling edge + SPI1CONbits.SMP = 1; // Input sampled at end + SPI1CONbits.ON = 1; // Enable SPI /* set up UART */ - U1BRG = 9; // 9600 baud (was 38 @ 24MHz) + U1BRG = 9; // 9600 baud (was 38 @ 24MHz) U1STAbits.UTXEN = 1; // enable transmitter - U1MODEbits.ON = 1; // enable UART + U1MODEbits.ON = 1; // enable UART /* set up DMA */ // clear all 4 DMA channel flags & IE IEC1CLR = 0xF0000000; IFS1CLR = 0xF0000000; - DMACONbits.ON = 1; //enable DMA controller + DMACONbits.ON = 1; // enable DMA controller // === DMA Channel 1 Init (RX from SPI1BUF to spi_rx_buffer[]) === DCH0CON = 0; // Reset channel config DCH0ECON = 0; - DCH0SSA = KVA_TO_PA(&SPI1BUF); // Source: SPI1BUF + DCH0SSA = KVA_TO_PA(&SPI1BUF); // Source: SPI1BUF DCH0DSA = KVA_TO_PA(spi_rx_buffer); // Destination: receive buffer - DCH0SSIZ = 1; // Source size = 1 byte - DCH0DSIZ = sizeof(spi_rx_buffer); // Destination size = 8 bytes - DCH0CSIZ = 1; // Cell transfer size = 1 byte + DCH0SSIZ = 1; // Source size = 1 byte + DCH0DSIZ = sizeof(spi_rx_buffer); // Destination size = 8 bytes + DCH0CSIZ = 1; // Cell transfer size = 1 byte DCH0ECONbits.CHSIRQ = _SPI1_VECTOR; // Trigger on SPI1 receive - DCH0ECONbits.SIRQEN = 1; // Enable IRQ trigger - DCH0INTCLR = 0x00FF00FF; // Clear all interrupt flags - DCH0INTbits.CHBCIE = 1; // Enable block complete interrupt - DCH0CONbits.CHPRI = 2; // Priority 2 (TX is higher) - DCH0CONbits.CHAEN = 1; // Auto-enable on trigger - DCH0CONbits.CHEN = 1; // Enable channel + DCH0ECONbits.SIRQEN = 1; // Enable IRQ trigger + DCH0INTCLR = 0x00FF00FF; // Clear all interrupt flags + DCH0INTbits.CHBCIE = 1; // Enable block complete interrupt + DCH0CONbits.CHPRI = 2; // Priority 2 (TX is higher) + DCH0CONbits.CHAEN = 1; // Auto-enable on trigger + DCH0CONbits.CHEN = 1; // Enable channel return 0; } @@ -157,8 +158,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) { @@ -168,7 +169,8 @@ void startSPI_DMA_Transfer(void) { SL_LAT = 1; DCH0CONbits.CHEN = 1; for(int i = 0; i < 8; i++) { - while(SPI1STATbits.SPITBF); // Wait if TX buffer full + while(SPI1STATbits.SPITBF) + ; // Wait if TX buffer full SPI1BUF = 0x00; // Send dummy byte to clock in data } return; @@ -192,15 +194,15 @@ 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) { -// if (USBE2CSR1bits.RXPKTRDY) { -// int count = USB_receive_EP2(); -// // Echo back -// USB_send_EP2(EP[2].rx_buffer, count); -// USBCSR1bits.EP2RXIF = 0; -// } -// IFS1bits.USBIF = 0; // clear USB interrupt flag -//} \ No newline at end of file +// extern "C" void __ISR(_USB1_VECTOR, IPL4AUTO) USBHandler(void) { +// if (USBE2CSR1bits.RXPKTRDY) { +// int count = USB_receive_EP2(); +// // Echo back +// USB_send_EP2(EP[2].rx_buffer, count); +// USBCSR1bits.EP2RXIF = 0; +// } +// IFS1bits.USBIF = 0; // clear USB interrupt flag +// } \ No newline at end of file diff --git a/strFuncs.cpp b/strFuncs.cpp index a4a2da3..46a5356 100644 --- a/strFuncs.cpp +++ b/strFuncs.cpp @@ -1,7 +1,6 @@ #include "inc/strFuncs.hpp" -template -void split(const std::string &s, char delim, Out result) { +template void split(const std::string &s, char delim, Out result) { std::istringstream iss(s); std::string item; while(std::getline(iss, item, delim))