Compare commits

...

5 commits

Author SHA1 Message Date
A.M. Rowsell
6e05baa87d
docs: Added Doxygen config 2025-09-07 18:00:30 -04:00
A.M. Rowsell
f05dd6d90f
dev: Updates to Board and Piece, formatted code 2025-09-07 17:59:16 -04:00
A.M. Rowsell
8e381cf109
docs: Starting to add Doxygen comments 2025-09-07 17:57:15 -04:00
A.M. Rowsell
ca6468cc62
docs: temporarily added docs/ to .gitignore as they are not ready 2025-09-07 17:46:48 -04:00
A.M. Rowsell
b68169dd2b
cool: I added figlet headings because I'm that cool 2025-09-01 01:42:42 -04:00
8 changed files with 3206 additions and 64 deletions

4
.gitignore vendored
View file

@ -39,4 +39,6 @@ chessmcu_default/
# End of https://www.toptal.com/developers/gitignore/api/mplabx # End of https://www.toptal.com/developers/gitignore/api/mplabx
localTest/ localTest/
docs/
.ca/

View file

@ -88,6 +88,21 @@ void Board::setupInitialPosition() {
} }
return; return;
} }
/**
* Sets entire board to nullptr
*/
void Board::clearBoard() {
for(int i = 0; i < 8; i++) {
for(int j = 0; j < 8; j++)
boardGrid[i][j] = nullptr;
}
}
/**
* Takes a FEN string and creates a board to that spec
* @param strFEN A full FEN string
* @return Returns -1 on failure, 0 on success
*/
int Board::setupFromFEN(std::string strFEN) { int Board::setupFromFEN(std::string strFEN) {
std::vector<std::string> splitFEN = split(strFEN, ' '); std::vector<std::string> splitFEN = split(strFEN, ' ');
@ -151,8 +166,8 @@ int Board::setupFromFEN(std::string strFEN) {
boardGrid[rank][file] = std::make_unique<King>(PIECE_WHITE); boardGrid[rank][file] = std::make_unique<King>(PIECE_WHITE);
wKingPlaced = true; wKingPlaced = true;
// store king position for later // store king position for later
whiteKing.file = file; whiteKing.file = static_cast<File>(file);
whiteKing.rank = rank; whiteKing.rank = static_cast<Rank>(rank);
file++; file++;
} }
break; break;
@ -165,8 +180,8 @@ int Board::setupFromFEN(std::string strFEN) {
boardGrid[rank][file] = std::make_unique<King>(PIECE_BLACK); boardGrid[rank][file] = std::make_unique<King>(PIECE_BLACK);
bKingPlaced = true; bKingPlaced = true;
// store king position for later // store king position for later
blackKing.file = file; blackKing.file = static_cast<File>(file);
blackKing.rank = rank; blackKing.rank = static_cast<Rank>(rank);
file++; file++;
} }
break; break;
@ -225,8 +240,11 @@ int Board::setupFromFEN(std::string strFEN) {
// ====== END OF FIELD 2 ====== // ====== END OF FIELD 2 ======
// ====== START OF FIELD 3 ====== // ====== START OF FIELD 3 ======
std::string k = "k", K = "K", q = "q", Q = "Q"; std::string k = "k", K = "K", q = "q", Q = "Q";
auto &bKing = this->getPieceAt(blackKing); // yeah this is hacky af but it works... blame SO
auto &wKing = this->getPieceAt(whiteKing); Piece *baseBKing = getPieceAt(blackKing).get();
Piece *baseWKing = getPieceAt(whiteKing).get();
King *bKing = dynamic_cast<King *>(baseBKing);
King *wKing = dynamic_cast<King *>(baseWKing);
if(splitFEN[2] == "-") { if(splitFEN[2] == "-") {
// nobody can castle, either side // nobody can castle, either side
bKing->setCastleKS(false); bKing->setCastleKS(false);
@ -253,34 +271,47 @@ int Board::setupFromFEN(std::string strFEN) {
// ====== END OF FIELD 3 ====== // ====== END OF FIELD 3 ======
// ====== START OF FIELD 4 ====== // ====== START OF FIELD 4 ======
// if a pawn has just moved and can be attacked enPassant, it will be listed here // if a pawn has just moved and can be attacked enPassant, it will be listed here
//#ifdef DEBUG
// for(auto &ranks : boardGrid) {
#ifdef DEBUG // for(auto &files : ranks) {
for(auto &ranks : boardGrid) { // if(files)
for(auto &files : ranks) { // std::cout << files->getPieceName() << " ";
if(files) // else
std::cout << files->getPieceName() << " "; // std::cout << "E ";
else // }
std::cout << "E "; // std::cout << "\n";
} // }
std::cout << "\n"; //#endif
}
#endif
return 0; return 0;
} }
void Board::nextTurn() {
playerTurn = (playerTurn == PL_WHITE) ? PL_BLACK : PL_WHITE; // switch to other player's turn
// do initial checks -- is their king in check? do they have any legal moves?
// stalemate check - do we really need to call getLegalMoves() on every single Piece?
}
/**
* This function should only be called after you have confirmed the move is legal!
* This will execute the move and assume you've already checked it.
* @param from The Square on which the Piece is departing
* @param to The Square to which the Piece is moving to
*/
void Board::movePiece(Square from, Square to) { void Board::movePiece(Square from, Square to) {
return; return;
} }
void Board::deserializeBoard(uint64_t incomingBoard) { 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++) for(int i = 0; i < 8; i++)
boardRows[i] = (incomingBoard >> (8 * i)) & 0xFF; serialBoard.boardRows[i] = static_cast<uint8_t>((incomingBoard >> (8 * i)) & 0xFF);
// how do we then figure out what has moved? // how do we then figure out what has moved?
return; return;
} }
std::unique_ptr<Piece> &Board::getPieceAt(Square square) { std::unique_ptr<Piece> &Board::getPieceAt(Square square) {
return boardGrid[square.rank][square.file]; return boardGrid[static_cast<int>(square.rank)][static_cast<int>(square.file)];
} }

2988
Doxyfile Normal file

File diff suppressed because it is too large Load diff

View file

@ -25,6 +25,27 @@ std::vector<Move> Piece::getLegalMoves(const Square &from, Board &board) const {
std::vector<Move> moveList; std::vector<Move> moveList;
return moveList; return moveList;
} }
bool Piece::finalMoveChecks(std::vector<Move> *moveList, Board &board) {
// This function needs to do the final checks before declaring a move legal
// Check to see if the piece that is being moves is currently blocking check
// Check to see if a castle is trying to go through check or out of check
// Check if it's a piece trying to capture the King (though this shouldn't happen normally)
return true;
}
// *INDENT-OFF*
//
// ## #
// #
// # ## ## # ## ## #
// # # # # # # #
// ## # # # # #
// # # # # # # #
// ## ## ##### ### ## ###
// #
// ###
// *INDENT-ON*
bool King::checkForCheck(Board &board) const { bool King::checkForCheck(Board &board) const {
std::vector<std::vector<int>> kingVulnerable = { std::vector<std::vector<int>> kingVulnerable = {
{-1, 0}, // Up {-1, 0}, // Up
@ -159,6 +180,18 @@ std::vector<Move> King::getLegalMoves(const Square &from, Board &board) const {
return moveList; return moveList;
} }
// *INDENT-OFF*
//
// ##
// #
// # ### ## ## # ##
// # # # # # # #
// # # # # # ##
// # # # # # # #
// #### ## ## ## ##
//
// *INDENT-OM*
std::vector<Move> Rook::getLegalMoves(const Square &from, Board &board) const { std::vector<Move> Rook::getLegalMoves(const Square &from, Board &board) const {
std::vector<Move> moveList; std::vector<Move> moveList;
const int directions[4][2] = { const int directions[4][2] = {
@ -189,6 +222,16 @@ std::vector<Move> Rook::getLegalMoves(const Square &from, Board &board) const {
return moveList; return moveList;
} }
// *INDENT-OFF*
// ## # ## ## ## ## # ##
// # # # # # # # # # #
// # # # # ### ### # #
// # # # # # # # #
// ### ## # ### ### ### ##
// #
// ###
// *INDENT-ON*
std::vector<Move> Queen::getLegalMoves(const Square &from, Board &board) const { std::vector<Move> Queen::getLegalMoves(const Square &from, Board &board) const {
std::vector<Move> moveList; std::vector<Move> moveList;
std::vector<std::vector<int>> directions = { std::vector<std::vector<int>> directions = {
@ -226,6 +269,18 @@ std::vector<Move> Queen::getLegalMoves(const Square &from, Board &board) const {
return moveList; return moveList;
} }
// *INDENT-OFF*
// ## # ## #
// # # #
// # ## # ## ## ## # ### ####
// # # # # # # # # # #
// ## # # # # # # # #
// # # # # # # # # # # #
// ## ## ### ## ##### ### ### ## ##
// #
// ###
// *INDENT-ON*
std::vector<Move> Knight::getLegalMoves(const Square &from, Board &board) const { std::vector<Move> Knight::getLegalMoves(const Square &from, Board &board) const {
std::vector<Move> moveList; std::vector<Move> moveList;
std::vector<std::vector<int>> directions = { std::vector<std::vector<int>> directions = {
@ -241,6 +296,18 @@ std::vector<Move> Knight::getLegalMoves(const Square &from, Board &board) const
return moveList; return moveList;
} }
// *INDENT-OFF*
// ## # ##
// # #
// ### ## ### ### ## # ##
// # # # # # # # # # #
// # # # ## # # # # # #
// # # # # # # # # # #
// #### ##### ### ### ## ## ###
// #
// ###
// *INDENT-ON*
std::vector<Move> Bishop::getLegalMoves(const Square &from, Board &board) const { std::vector<Move> Bishop::getLegalMoves(const Square &from, Board &board) const {
std::vector<Move> moveList; std::vector<Move> moveList;
std::vector<std::vector<int>> directions = { std::vector<std::vector<int>> directions = {
@ -252,6 +319,17 @@ std::vector<Move> Bishop::getLegalMoves(const Square &from, Board &board) const
return moveList; return moveList;
} }
// *INDENT-OFF*
//
// # ## ## # # ## # ##
// # # # # # # # #
// # # ### # # # # #
// # # # # # # # #
// ### ## # # # ### ##
// #
// ###
// *INDENT-ON*
std::vector<Move> Pawn::getLegalMoves(const Square &from, Board &board) const { std::vector<Move> Pawn::getLegalMoves(const Square &from, Board &board) const {
std::vector<Move> moveList; std::vector<Move> moveList;
const int directions[2][4][2] = { const int directions[2][4][2] = {

View file

@ -13,7 +13,6 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
#include <iostream>
#include "Piece.hpp" #include "Piece.hpp"
@ -24,24 +23,28 @@ enum Players { PL_WHITE, PL_BLACK };
struct Square; struct Square;
class Board { class Board {
private: private:
friend class Piece; friend class Piece; // this doesn't seem to do anything
Players playerTurn; Players playerTurn;
public: public:
// this should be protected, but even when Piece is declared as a friend, // this should be protected, but even when Piece is declared as a friend,
// accessing it in Piece.cpp threw an error // accessing it in Piece.cpp threw an error
std::vector<std::vector<std::unique_ptr<Piece>>> boardGrid; std::vector<std::vector<std::unique_ptr<Piece>>> boardGrid;
Board(); Board();
virtual ~Board(); virtual ~Board();
void setupInitialPosition(); void setupInitialPosition();
std::unique_ptr<Piece> &getPieceAt(Square square);
void movePiece(Square from, Square to); void clearBoard();
int setupFromFEN(std::string strFEN); std::unique_ptr<Piece> &getPieceAt(Square square);
bool isInBounds(Square square) const; void movePiece(Square from, Square to);
bool isEmpty(Square square) const; void nextTurn();
uint64_t serialBoard = 0xFFFF00000000FFFF; // opening position int setupFromFEN(std::string strFEN);
void deserializeBoard(uint64_t incomingBoard); 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 #endif // BOARD_HPP

View file

@ -9,6 +9,19 @@
#ifndef PIECE_HPP #ifndef PIECE_HPP
#define PIECE_HPP #define PIECE_HPP
/** @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.
*/
#include <cstdint> #include <cstdint>
#include <algorithm> #include <algorithm>
#include <utility> #include <utility>
@ -38,28 +51,45 @@ enum CastleSide {
KINGSIDE = 1, QUEENSIDE = 2 KINGSIDE = 1, QUEENSIDE = 2
}; };
/** @struct Square
* A struct that represents a square on the chess board.
*/
struct Square { struct Square {
Rank rank; Rank rank; /**< enum Rank rank represents the rank of the Square */
File file; File file; /**< enum File file represents the file of the 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 { bool isValid() const {
return rank >= 0 && rank < 8 && file >= 0 && file < 8; return rank >= 0 && rank < 8 && file >= 0 && file < 8;
} }
}; };
/**
* A struct representing the start and end squares of a Move.
*/
struct Move { struct Move {
Square from; Square from;
Square to; Square to;
}; };
/**
* @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.
*/
class Piece { class Piece {
private: private:
friend class Board; friend class Board;
protected: protected:
PieceColour colour; PieceColour colour;
PieceType pieceType; PieceType pieceType;
std::string pieceName; std::string pieceName; /**< std::string pieceName is a string showing the full name of the piece, i.e. "PAWN" */
char pieceSymbol; std::string
pieceSymbol; /**< std::string pieceSymbol is a single character, using the standard algebraic chess notation, ie N for Knight */
bool hasMoved = false; bool hasMoved = false;
public: public:
@ -67,7 +97,7 @@ class Piece {
Piece(PieceColour pColour) : colour(pColour) { Piece(PieceColour pColour) : colour(pColour) {
} }
virtual ~Piece(); virtual ~Piece();
virtual std::vector<Move> getLegalMoves(const Square &from, Board &board) const;
PieceColour getColour() const { PieceColour getColour() const {
return colour; return colour;
@ -77,7 +107,7 @@ class Piece {
return pieceName; return pieceName;
} }
char getPieceSymbol() const { std::string getPieceSymbol() const {
return pieceSymbol; return pieceSymbol;
} }
@ -88,6 +118,19 @@ class Piece {
bool checkIfMoved() const { bool checkIfMoved() const {
return hasMoved; 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.
* 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
*/
virtual std::vector<Move> getLegalMoves(const Square &from, Board &board) const;
bool finalMoveChecks(std::vector<Move> *moveList, Board &board);
}; };
class King : public Piece { class King : public Piece {
@ -97,7 +140,7 @@ class King : public Piece {
King(PieceColour colour) : Piece(colour) { King(PieceColour colour) : Piece(colour) {
pieceName = "King"; pieceName = "King";
pieceSymbol = 'K'; pieceSymbol = "K";
pieceType = KING; pieceType = KING;
} }
virtual std::vector<Move> getLegalMoves(const Square &from, Board &board) const override; virtual std::vector<Move> getLegalMoves(const Square &from, Board &board) const override;
@ -107,14 +150,12 @@ class King : public Piece {
bool checkForCastle(enum CastleSide side) const { bool checkForCastle(enum CastleSide side) const {
if(side == KINGSIDE) if(side == KINGSIDE)
return canCastleKS; return canCastleKS;
else if(side == QUEENSIDE) else if(side == QUEENSIDE)
return canCastleQS; return canCastleQS;
else else
return false; return false;
} }
void setCastleQS(bool canCastle) { void setCastleQS(bool canCastle) {
canCastleQS = canCastle; canCastleQS = canCastle;
} }
@ -133,7 +174,7 @@ class Rook : public Piece {
Rook(PieceColour colour) : Piece(colour) { Rook(PieceColour colour) : Piece(colour) {
pieceName = "Rook"; pieceName = "Rook";
pieceSymbol = 'R'; pieceSymbol = "R";
pieceType = ROOK; pieceType = ROOK;
} }
virtual std::vector<Move> getLegalMoves(const Square &from, Board &board) const override; virtual std::vector<Move> getLegalMoves(const Square &from, Board &board) const override;
@ -145,7 +186,7 @@ class Queen : public Piece {
Queen(PieceColour colour) : Piece(colour) { Queen(PieceColour colour) : Piece(colour) {
pieceName = "Queen"; pieceName = "Queen";
pieceSymbol = 'Q'; pieceSymbol = "Q";
pieceType = QUEEN; pieceType = QUEEN;
} }
virtual std::vector<Move> getLegalMoves(const Square &from, Board &board) const override; virtual std::vector<Move> getLegalMoves(const Square &from, Board &board) const override;
@ -157,7 +198,7 @@ class Knight : public Piece {
Knight(PieceColour colour) : Piece(colour) { Knight(PieceColour colour) : Piece(colour) {
pieceName = "Knight"; pieceName = "Knight";
pieceSymbol = 'N'; pieceSymbol = "N";
pieceType = KNIGHT; pieceType = KNIGHT;
} }
virtual std::vector<Move> getLegalMoves(const Square &from, Board &board) const override; virtual std::vector<Move> getLegalMoves(const Square &from, Board &board) const override;
@ -169,7 +210,7 @@ class Bishop : public Piece {
Bishop(PieceColour colour) : Piece(colour) { Bishop(PieceColour colour) : Piece(colour) {
pieceName = "Bishop"; pieceName = "Bishop";
pieceSymbol = 'B'; pieceSymbol = "B";
pieceType = BISHOP; pieceType = BISHOP;
} }
virtual std::vector<Move> getLegalMoves(const Square &from, Board &board) const override; virtual std::vector<Move> getLegalMoves(const Square &from, Board &board) const override;
@ -181,12 +222,12 @@ class Pawn : public Piece {
Pawn(PieceColour colour) : Piece(colour) { Pawn(PieceColour colour) : Piece(colour) {
pieceName = "Pawn"; pieceName = "Pawn";
pieceSymbol = 'P'; pieceSymbol = "P";
pieceType = PAWN; pieceType = PAWN;
} }
virtual std::vector<Move> getLegalMoves(const Square &from, Board &board) const override; virtual std::vector<Move> getLegalMoves(const Square &from, Board &board) const override;
void promote(const Square &promotionSquare, Board &board, PieceType promoteTo); void promote(const Square &promotionSquare, Board &board, PieceType promoteTo);
protected: protected:
bool vulnEnPassant = false; bool vulnEnPassant = false;
bool firstMove = true; bool firstMove = true;
}; };

View file

@ -1,4 +1,4 @@
/* /*
* File: strFuncs.hpp * File: strFuncs.hpp
* Author: amr * Author: amr
* *

View file

@ -157,8 +157,8 @@ void initInterrupts(void) {
INTCONbits.MVEC = 1; // Enable multi-vector interrupts INTCONbits.MVEC = 1; // Enable multi-vector interrupts
__builtin_enable_interrupts(); __builtin_enable_interrupts();
IPC10bits.DMA0IP = 3; // Priority level 3 IPC10bits.DMA0IP = 3; // Priority level 3
IFS1CLR = _IFS1_DMA0IF_MASK; // Clear interrupt flag // IFS1CLR = _IFS1_DMA0IF_MASK; // Clear interrupt flag
IEC1SET = _IEC1_DMA0IE_MASK; // Enable DMA1 interrupt // IEC1SET = _IEC1_DMA0IE_MASK; // Enable DMA1 interrupt
} }
void startSPI_DMA_Transfer(void) { void startSPI_DMA_Transfer(void) {
@ -179,7 +179,6 @@ extern "C" int main(void) {
initInterrupts(); initInterrupts();
Board gameBoard; Board gameBoard;
NeoPixel boardLights(64); NeoPixel boardLights(64);
gameBoard.setupInitialPosition(); gameBoard.setupInitialPosition();
while(1) { while(1) {
} }
@ -193,7 +192,7 @@ extern "C" void __ISR(_DMA0_VECTOR, IPL3AUTO) DMA0Handler(void) {
DCH0INTCLR = _DCH0INT_CHBCIF_MASK; // Clear block complete flag DCH0INTCLR = _DCH0INT_CHBCIF_MASK; // Clear block complete flag
// DMA RX completed — spi_rx_buffer[] now contains the data // 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) { //extern "C" void __ISR(_USB1_VECTOR, IPL4AUTO) USBHandler(void) {