dev: formatting fixes, additional Piece logic and properties, etc

This commit is contained in:
A.M. Rowsell 2025-08-09 07:06:24 -04:00
commit b0afa6cd78
Signed by untrusted user who does not match committer: amr
GPG key ID: E0879EDBDB0CA7B1
7 changed files with 295 additions and 21 deletions

View file

@ -31,6 +31,7 @@
Board::Board() {
// set up the board grid with smart pointers
// initialize each square with a Piece, set to PIECE_EMPTY
playerTurn = PL_WHITE;
boardGrid.resize(8);
for (int i = 0; i < 8; i++) {
boardGrid[i].resize(8);
@ -92,5 +93,10 @@ void Board::movePiece(Square from, Square to) {
}
void Board::deserializeBoard(uint64_t incomingBoard) {
uint8_t boardRows[8];
for (int i = 0; i < 8; i++) {
boardRows[i] = (incomingBoard >> (8 * i)) & 0xFF;
}
// how do we then figure out what has moved?
return;
}

View file

@ -16,24 +16,30 @@
// 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 {
friend class Piece;
friend class Piece;
private:
Players playerTurn;
public:
Board();
virtual ~Board();
void setupInitialPosition();
Piece *getPieceAt(Square square) const;
void movePiece(Square from, Square to);
bool isInBounds(Square square) const;
bool isEmpty(Square square) const;
uint64_t serialBoard = 0xFFFF00000000FFFF; // opening position
void deserializeBoard(uint64_t incomingBoard);
// this should be protected, but even when Piece is declared as a friend,
// accessing it in Piece.cpp threw an error
std::vector<std::vector<std::unique_ptr<Piece>>> boardGrid;
// this should be protected, but even when Piece is declared as a friend,
// accessing it in Piece.cpp threw an error
std::vector<std::vector<std::unique_ptr<Piece>>> boardGrid;
Board();
virtual ~Board();
void setupInitialPosition();
Piece *getPieceAt(Square square);
void movePiece(Square from, Square to);
bool isInBounds(Square square) const;
bool isEmpty(Square square) const;
uint64_t serialBoard = 0xFFFF00000000FFFF; // opening position
void deserializeBoard(uint64_t incomingBoard);
};
#endif // BOARD_HPP

View file

@ -125,13 +125,13 @@ std::vector<Move> Pawn::getLegalMoves(const Square &from, const Board &board) co
if (!target && !(board.boardGrid[r + 1][f])) // check both squares for pieces of any colour
moveList.push_back({from, targetSquare});
else
break;
continue;
} else if (abs(dir[1]) == 1) { // attempted capture (diagonal)
if (target && target->getColour() != this->getColour()) {
// legal capture
moveList.push_back({from, targetSquare});
} else {
break;
continue;
}
} else { // normal one square forward
if (!target)
@ -153,16 +153,16 @@ std::vector<Move> Pawn::getLegalMoves(const Square &from, const Board &board) co
if (dir[0] == 2 && !this->checkIfMoved()) {
// then 2 is potentially legal
if (!target && !(boardGrid[r - 1][f]))
if (!target && !(board.boardGrid[r - 1][f]))
moveList.push_back({from, targetSquare});
else
break;
continue;
} 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});
} else {
break;
continue;
}
} else { // normal one square forward
if (!target)

View file

@ -51,6 +51,9 @@ class Piece {
friend class Board;
protected:
PieceColour colour;
PieceType pieceType;
std::string pieceName;
char pieceSymbol;
bool hasMoved = false;
public:
@ -63,6 +66,18 @@ public:
PieceColour getColour() const {
return colour;
}
std::string getPieceName() const {
return pieceName;
}
char getPieceSymbol() const {
return pieceSymbol;
}
PieceType getPieceType() const {
return pieceType;
}
bool checkIfMoved() const {
return hasMoved;
@ -74,6 +89,9 @@ class King : public Piece {
public:
King(PieceColour colour) : Piece(colour) {
pieceName = "King";
pieceSymbol = 'K';
pieceType = KING;
}
virtual std::vector<Move> getLegalMoves(const Square &from, const Board &board) const override;
@ -94,6 +112,9 @@ class Rook : public Piece {
public:
Rook(PieceColour colour) : Piece(colour) {
pieceName = "Rook";
pieceSymbol = 'R';
pieceType = ROOK;
}
virtual std::vector<Move> getLegalMoves(const Square &from, const Board &board) const override;
};
@ -103,6 +124,9 @@ class Queen : public Piece {
public:
Queen(PieceColour colour) : Piece(colour) {
pieceName = "Queen";
pieceSymbol = 'Q';
pieceType = QUEEN;
}
virtual std::vector<Move> getLegalMoves(const Square &from, const Board &board) const override;
};
@ -112,6 +136,9 @@ class Knight : public Piece {
public:
Knight(PieceColour colour) : Piece(colour) {
pieceName = "Knight";
pieceSymbol = 'N';
pieceType = KNIGHT;
}
virtual std::vector<Move> getLegalMoves(const Square &from, const Board &board) const override;
};
@ -121,6 +148,9 @@ class Bishop : public Piece {
public:
Bishop(PieceColour colour) : Piece(colour) {
pieceName = "Bishop";
pieceSymbol = 'B';
pieceType = BISHOP;
}
virtual std::vector<Move> getLegalMoves(const Square &from, const Board &board) const override;
};
@ -130,6 +160,9 @@ class Pawn : public Piece {
public:
Pawn(PieceColour colour) : Piece(colour) {
pieceName = "Pawn";
pieceSymbol = 'P';
pieceType = PAWN;
}
virtual std::vector<Move> getLegalMoves(const Square &from, const Board &board) const override;
};

View file

@ -58,6 +58,7 @@
#define SPI_BAUD 1000000 // 1MHz hopefully
volatile uint8_t spi_rx_buffer[8];
volatile uint32_t usbBDT[512] __attribute__((aligned(512))); // USB buffer description table
// dummy open to get rid of linker error
@ -130,7 +131,7 @@ uint8_t initSystem(void) {
SPI1CONbits.ON = 1; // Enable SPI
/* set up UART */
U1BRG = 19; // 9600 baud (was 38 @ 24MHz)
U1BRG = 9; // 9600 baud (was 38 @ 24MHz)
U1STAbits.UTXEN = 1; // enable transmitter
U1MODEbits.ON = 1; // enable UART
@ -199,7 +200,7 @@ extern "C" int main(void) {
// === Interrupt Service Routine for DMA0 (RX complete) ===
extern "C" void __ISR(_DMA0_VECTOR, IPL3SOFT) DMA0Handler(void) {
extern "C" void __ISR(_DMA0_VECTOR, IPL3AUTO) DMA0Handler(void) {
__builtin_disable_interrupts(); // stop additional ints from firing
if (DCH0INTbits.CHBCIF) {
DCH0INTCLR = _DCH0INT_CHBCIF_MASK; // Clear block complete flag
@ -207,4 +208,14 @@ extern "C" void __ISR(_DMA0_VECTOR, IPL3SOFT) DMA0Handler(void) {
}
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
}

View file

@ -37,6 +37,7 @@
<itemPath>Board.cpp</itemPath>
<itemPath>Piece.cpp</itemPath>
<itemPath>NeoPixel.cpp</itemPath>
<itemPath>graphics.cpp</itemPath>
</logicalFolder>
</logicalFolder>
<sourceRootList>
@ -472,5 +473,218 @@
<property key="wpo-lto" value="false"/>
</C32Global>
</conf>
<conf name="PIC32MM" type="2">
<toolsSet>
<developmentServer>localhost</developmentServer>
<targetDevice>PIC32MM0256GPM028</targetDevice>
<targetHeader></targetHeader>
<targetPluginBoard></targetPluginBoard>
<platformTool>noID</platformTool>
<languageToolchain>XC32</languageToolchain>
<languageToolchainVersion>4.60</languageToolchainVersion>
<platform>2</platform>
</toolsSet>
<packs>
<pack name="PIC32MM-GPM-0XX_DFP" vendor="Microchip" version="1.4.156"/>
</packs>
<ScriptingSettings>
</ScriptingSettings>
<compileType>
<linkerTool>
<linkerLibItems>
</linkerLibItems>
</linkerTool>
<archiverTool>
</archiverTool>
<loading>
<useAlternateLoadableFile>false</useAlternateLoadableFile>
<parseOnProdLoad>true</parseOnProdLoad>
<alternateLoadableFile></alternateLoadableFile>
</loading>
<subordinates>
</subordinates>
</compileType>
<makeCustomizationType>
<makeCustomizationPreStepEnabled>false</makeCustomizationPreStepEnabled>
<makeUseCleanTarget>false</makeUseCleanTarget>
<makeCustomizationPreStep></makeCustomizationPreStep>
<makeCustomizationPostStepEnabled>false</makeCustomizationPostStepEnabled>
<makeCustomizationPostStep></makeCustomizationPostStep>
<makeCustomizationPutChecksumInUserID>false</makeCustomizationPutChecksumInUserID>
<makeCustomizationEnableLongLines>false</makeCustomizationEnableLongLines>
<makeCustomizationNormalizeHexFile>false</makeCustomizationNormalizeHexFile>
</makeCustomizationType>
<C32>
<property key="additional-warnings" value="false"/>
<property key="addresss-attribute-use" value="false"/>
<property key="cast-align" value="false"/>
<property key="code-model" value="default"/>
<property key="const-model" value="default"/>
<property key="data-model" value="default"/>
<property key="disable-instruction-scheduling" value="false"/>
<property key="enable-app-io" value="false"/>
<property key="enable-omit-frame-pointer" value="false"/>
<property key="enable-procedural-abstraction" value="false"/>
<property key="enable-short-double" value="false"/>
<property key="enable-symbols" value="true"/>
<property key="enable-unroll-loops" value="false"/>
<property key="exclude-floating-point" value="true"/>
<property key="expand-pragma-config" value="false"/>
<property key="extra-include-directories" value=""/>
<property key="generate-16-bit-code" value="false"/>
<property key="generate-micro-compressed-code" value="false"/>
<property key="isolate-each-function" value="false"/>
<property key="keep-inline" value="false"/>
<property key="make-warnings-into-errors" value="false"/>
<property key="oXC16gcc-errata" value=""/>
<property key="oXC16gcc-large-aggregate" value="false"/>
<property key="oXC16gcc-mpa-lvl" value=""/>
<property key="oXC16gcc-name-text-sec" value=""/>
<property key="oXC16gcc-near-chars" value="false"/>
<property key="oXC16gcc-no-isr-warn" value="false"/>
<property key="oXC16gcc-sfr-warn" value="false"/>
<property key="oXC16gcc-smar-io-lvl" value="1"/>
<property key="oXC16gcc-smart-io-fmt" value=""/>
<property key="optimization-level" value="-O0"/>
<property key="place-data-into-section" value="true"/>
<property key="post-instruction-scheduling" value="default"/>
<property key="pre-instruction-scheduling" value="default"/>
<property key="preprocessor-macros" value=""/>
<property key="scalar-model" value="default"/>
<property key="strict-ansi" value="false"/>
<property key="support-ansi" value="false"/>
<property key="tentative-definitions" value="-fno-common"/>
<property key="toplevel-reordering" value=""/>
<property key="unaligned-access" value=""/>
<property key="use-cci" value="false"/>
<property key="use-iar" value="false"/>
<property key="use-indirect-calls" value="false"/>
</C32>
<C32-AR>
<property key="additional-options-chop-files" value="false"/>
</C32-AR>
<C32-AS>
<property key="assembler-symbols" value=""/>
<property key="enable-symbols" value="true"/>
<property key="exclude-floating-point-library" value="true"/>
<property key="expand-macros" value="false"/>
<property key="extra-include-directories-for-assembler" value=""/>
<property key="extra-include-directories-for-preprocessor" value=""/>
<property key="false-conditionals" value="false"/>
<property key="generate-16-bit-code" value="false"/>
<property key="generate-micro-compressed-code" value="false"/>
<property key="keep-locals" value="false"/>
<property key="list-assembly" value="false"/>
<property key="list-section-info" value="false"/>
<property key="list-source" value="false"/>
<property key="list-symbols" value="false"/>
<property key="oXC16asm-extra-opts" value=""/>
<property key="oXC32asm-list-to-file" value="false"/>
<property key="omit-debug-dirs" value="false"/>
<property key="omit-forms" value="false"/>
<property key="preprocessor-macros" value=""/>
<property key="relax" value="false"/>
<property key="warning-level" value=""/>
</C32-AS>
<C32-CO>
<property key="coverage-enable" value=""/>
<property key="stack-guidance" value="false"/>
</C32-CO>
<C32-LD>
<property key="additional-options-use-response-files" value="false"/>
<property key="additional-options-write-sla" value="false"/>
<property key="allocate-dinit" value="false"/>
<property key="code-dinit" value="false"/>
<property key="ebase-addr" value=""/>
<property key="enable-check-sections" value="false"/>
<property key="enable-data-init" value="true"/>
<property key="enable-default-isr" value="true"/>
<property key="exclude-floating-point-library" value="false"/>
<property key="exclude-standard-libraries" value="false"/>
<property key="extra-lib-directories" value=""/>
<property key="fill-flash-options-addr" value=""/>
<property key="fill-flash-options-const" value=""/>
<property key="fill-flash-options-how" value="0"/>
<property key="fill-flash-options-inc-const" value="1"/>
<property key="fill-flash-options-increment" value=""/>
<property key="fill-flash-options-seq" value=""/>
<property key="fill-flash-options-what" value="0"/>
<property key="generate-16-bit-code" value="false"/>
<property key="generate-cross-reference-file" value="false"/>
<property key="generate-micro-compressed-code" value="false"/>
<property key="heap-size" value="768"/>
<property key="input-libraries" value=""/>
<property key="kseg-length" value=""/>
<property key="kseg-origin" value=""/>
<property key="linker-symbols" value=""/>
<property key="map-file" value="${DISTDIR}/${PROJECTNAME}.${IMAGE_TYPE}.map"/>
<property key="no-device-startup-code" value="false"/>
<property key="no-ivt" value="false"/>
<property key="no-startup-files" value="false"/>
<property key="oXC16ld-force-link" value="false"/>
<property key="oXC16ld-no-smart-io" value="false"/>
<property key="oXC16ld-stackguard" value="16"/>
<property key="oXC32ld-extra-opts" value=""/>
<property key="optimization-level" value="-O1"/>
<property key="preprocessor-macros" value=""/>
<property key="remove-unused-sections" value="true"/>
<property key="report-memory-usage" value="true"/>
<property key="serial-length" value=""/>
<property key="serial-origin" value=""/>
<property key="stack-size" value="100"/>
<property key="symbol-stripping" value=""/>
<property key="trace-symbols" value=""/>
<property key="warn-section-align" value="false"/>
</C32-LD>
<C32CPP>
<property key="additional-warnings" value="false"/>
<property key="addresss-attribute-use" value="false"/>
<property key="check-new" value="false"/>
<property key="eh-specs" value="true"/>
<property key="enable-app-io" value="false"/>
<property key="enable-omit-frame-pointer" value="false"/>
<property key="enable-symbols" value="true"/>
<property key="enable-unroll-loops" value="false"/>
<property key="exceptions" value="true"/>
<property key="exclude-floating-point" value="true"/>
<property key="extra-include-directories" value=""/>
<property key="generate-16-bit-code" value="false"/>
<property key="generate-micro-compressed-code" value="false"/>
<property key="isolate-each-function" value="false"/>
<property key="make-warnings-into-errors" value="false"/>
<property key="optimization-level" value=""/>
<property key="place-data-into-section" value="true"/>
<property key="post-instruction-scheduling" value="default"/>
<property key="pre-instruction-scheduling" value="default"/>
<property key="preprocessor-macros" value=""/>
<property key="rtti" value="true"/>
<property key="strict-ansi" value="false"/>
<property key="toplevel-reordering" value=""/>
<property key="unaligned-access" value=""/>
<property key="use-cci" value="false"/>
<property key="use-iar" value="false"/>
<property key="use-indirect-calls" value="false"/>
</C32CPP>
<C32Global>
<property key="combine-sourcefiles" value="false"/>
<property key="common-include-directories" value=""/>
<property key="common-macros" value=""/>
<property key="dual-boot-partition" value="0"/>
<property key="generic-16-bit" value="false"/>
<property key="gp-relative-option" value=""/>
<property key="legacy-libc" value="true"/>
<property key="mdtcm" value=""/>
<property key="mitcm" value=""/>
<property key="mpreserve-all" value="false"/>
<property key="mstacktcm" value="false"/>
<property key="omit-pack-options" value="1"/>
<property key="preserve-all" value="false"/>
<property key="preserve-file" value=""/>
<property key="relaxed-math" value="false"/>
<property key="save-temps" value="false"/>
<property key="stack-smashing" value=""/>
<property key="wpo-lto" value="false"/>
</C32Global>
</conf>
</confs>
</configurationDescriptor>

View file

@ -21,6 +21,10 @@
<name>Production</name>
<type>2</type>
</confElem>
<confElem>
<name>PIC32MM</name>
<type>2</type>
</confElem>
</confList>
<formatting>
<project-formatting-style>false</project-formatting-style>