dev: Updates to Board and Piece, formatted code
This commit is contained in:
parent
8e381cf109
commit
f05dd6d90f
6 changed files with 122 additions and 121 deletions
49
Board.cpp
49
Board.cpp
|
|
@ -93,11 +93,10 @@ void Board::setupInitialPosition() {
|
||||||
*/
|
*/
|
||||||
void Board::clearBoard() {
|
void Board::clearBoard() {
|
||||||
for(int i = 0; i < 8; i++) {
|
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;
|
boardGrid[i][j] = nullptr;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Takes a FEN string and creates a board to that spec
|
* Takes a FEN string and creates a board to that spec
|
||||||
|
|
@ -167,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;
|
||||||
|
|
@ -181,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;
|
||||||
|
|
@ -241,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);
|
||||||
|
|
@ -269,19 +271,17 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -302,13 +302,16 @@ void Board::movePiece(Square from, Square to) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void Board::deserializeBoard(uint64_t incomingBoard) {
|
void Board::deserializeBoard(uint64_t incomingBoard) {
|
||||||
|
union {
|
||||||
uint8_t boardRows[8];
|
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)];
|
||||||
}
|
}
|
||||||
85
Piece.cpp
85
Piece.cpp
|
|
@ -35,17 +35,16 @@ bool Piece::finalMoveChecks(std::vector<Move> *moveList, Board &board) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// *INDENT-OFF*
|
// *INDENT-OFF*
|
||||||
// ##
|
//
|
||||||
// ## ##
|
// ## #
|
||||||
// ##
|
// #
|
||||||
// ## ## ## # ## ####
|
// # ## ## # ## ## #
|
||||||
// ## # ## ## ## ## #
|
// # # # # # # #
|
||||||
// ### ## ## ## ## #
|
// ## # # # # #
|
||||||
// ## # ## ## ## ###
|
// # # # # # # #
|
||||||
// ## ## ## ## ### ##
|
// ## ## ##### ### ## ###
|
||||||
// ####
|
// #
|
||||||
// # #
|
// ###
|
||||||
// ####
|
|
||||||
// *INDENT-ON*
|
// *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 = {
|
||||||
|
|
@ -182,13 +181,15 @@ std::vector<Move> King::getLegalMoves(const Square &from, Board &board) const {
|
||||||
}
|
}
|
||||||
|
|
||||||
// *INDENT-OFF*
|
// *INDENT-OFF*
|
||||||
|
//
|
||||||
// ##
|
// ##
|
||||||
// ##
|
// #
|
||||||
// ## # ### ### ## ##
|
// # ### ## ## # ##
|
||||||
// #### ## ## ## ## ## #
|
// # # # # # # #
|
||||||
// ## ## ## ## ## ###
|
// # # # # # ##
|
||||||
// ## ## ## ## ## ## #
|
// # # # # # # #
|
||||||
// #### ## ## ## ##
|
// #### ## ## ## ##
|
||||||
|
//
|
||||||
// *INDENT-OM*
|
// *INDENT-OM*
|
||||||
|
|
||||||
std::vector<Move> Rook::getLegalMoves(const Square &from, Board &board) const {
|
std::vector<Move> Rook::getLegalMoves(const Square &from, Board &board) const {
|
||||||
|
|
@ -222,12 +223,12 @@ std::vector<Move> Rook::getLegalMoves(const Square &from, Board &board) const {
|
||||||
}
|
}
|
||||||
|
|
||||||
// *INDENT-OFF*
|
// *INDENT-OFF*
|
||||||
// ### ## ## ### ### # ##
|
|
||||||
// ## # ## # ## # ## # ## ##
|
|
||||||
// ## # ## # #### #### ## ##
|
|
||||||
// ## # ## ## ## ## # ##
|
// ## # ## ## ## ## # ##
|
||||||
|
// # # # # # # # # # #
|
||||||
|
// # # # # ### ### # #
|
||||||
|
// # # # # # # # #
|
||||||
// ### ## # ### ### ### ##
|
// ### ## # ### ### ### ##
|
||||||
// ##
|
// #
|
||||||
// ###
|
// ###
|
||||||
// *INDENT-ON*
|
// *INDENT-ON*
|
||||||
|
|
||||||
|
|
@ -269,18 +270,15 @@ std::vector<Move> Queen::getLegalMoves(const Square &from, Board &board) const {
|
||||||
}
|
}
|
||||||
|
|
||||||
// *INDENT-OFF*
|
// *INDENT-OFF*
|
||||||
//
|
|
||||||
// ##
|
|
||||||
// ## ## ## #
|
|
||||||
// ## # ## #
|
// ## # ## #
|
||||||
// ## ## # ## ## #### #### ###
|
// # # #
|
||||||
// ## # ## ## ## ## # ## # ##
|
// # ## # ## ## ## # ### ####
|
||||||
// ### ## ## ## ## # ## # ##
|
// # # # # # # # # # #
|
||||||
// ## # ## ## ## ### ## # ##
|
// ## # # # # # # # #
|
||||||
// ## ## ## ### ## ## ## ## ##
|
// # # # # # # # # # # #
|
||||||
// ####
|
// ## ## ### ## ##### ### ### ## ##
|
||||||
// # #
|
// #
|
||||||
// ####
|
// ###
|
||||||
// *INDENT-ON*
|
// *INDENT-ON*
|
||||||
|
|
||||||
std::vector<Move> Knight::getLegalMoves(const Square &from, Board &board) const {
|
std::vector<Move> Knight::getLegalMoves(const Square &from, Board &board) const {
|
||||||
|
|
@ -299,16 +297,14 @@ std::vector<Move> Knight::getLegalMoves(const Square &from, Board &board) const
|
||||||
}
|
}
|
||||||
|
|
||||||
// *INDENT-OFF*
|
// *INDENT-OFF*
|
||||||
//
|
// ## # ##
|
||||||
// ##
|
|
||||||
// ## ## ##
|
|
||||||
// ## ##
|
|
||||||
// ### ## ## #### ### ###
|
|
||||||
// ## # ## ## ## # ## ## ## #
|
|
||||||
// ## # ## ### ## # ## ## ## #
|
|
||||||
// ## # ## ## ## # ## ## ## #
|
|
||||||
// ### ## ### ## ## ### ###
|
|
||||||
// # #
|
// # #
|
||||||
|
// ### ## ### ### ## # ##
|
||||||
|
// # # # # # # # # # #
|
||||||
|
// # # # ## # # # # # #
|
||||||
|
// # # # # # # # # # #
|
||||||
|
// #### ##### ### ### ## ## ###
|
||||||
|
// #
|
||||||
// ###
|
// ###
|
||||||
// *INDENT-ON*
|
// *INDENT-ON*
|
||||||
|
|
||||||
|
|
@ -324,12 +320,13 @@ std::vector<Move> Bishop::getLegalMoves(const Square &from, Board &board) const
|
||||||
}
|
}
|
||||||
|
|
||||||
// *INDENT-OFF*
|
// *INDENT-OFF*
|
||||||
|
//
|
||||||
// # ## ## # # ## # ##
|
// # ## ## # # ## # ##
|
||||||
|
// # # # # # # # #
|
||||||
|
// # # ### # # # # #
|
||||||
|
// # # # # # # # #
|
||||||
// ### ## # # # ### ##
|
// ### ## # # # ### ##
|
||||||
// ## # ### # ### ## ##
|
// #
|
||||||
// ## # # # #### ## ##
|
|
||||||
// ### ##### # # ## ###
|
|
||||||
// ##
|
|
||||||
// ###
|
// ###
|
||||||
// *INDENT-ON*
|
// *INDENT-ON*
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,6 @@
|
||||||
#include <memory>
|
#include <memory>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
#include "Piece.hpp"
|
#include "Piece.hpp"
|
||||||
|
|
||||||
|
|
@ -25,7 +24,7 @@ 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,
|
||||||
|
|
@ -35,11 +34,15 @@ public:
|
||||||
virtual ~Board();
|
virtual ~Board();
|
||||||
|
|
||||||
void setupInitialPosition();
|
void setupInitialPosition();
|
||||||
|
|
||||||
|
void clearBoard();
|
||||||
std::unique_ptr<Piece> &getPieceAt(Square square);
|
std::unique_ptr<Piece> &getPieceAt(Square square);
|
||||||
void movePiece(Square from, Square to);
|
void movePiece(Square from, Square to);
|
||||||
|
void nextTurn();
|
||||||
int setupFromFEN(std::string strFEN);
|
int setupFromFEN(std::string strFEN);
|
||||||
bool isInBounds(Square square) const;
|
bool isInBounds(Square square) const;
|
||||||
bool isEmpty(Square square) const;
|
bool isEmpty(Square square) const;
|
||||||
|
// serial shift register stuff
|
||||||
uint64_t serialBoard = 0xFFFF00000000FFFF; // opening position
|
uint64_t serialBoard = 0xFFFF00000000FFFF; // opening position
|
||||||
void deserializeBoard(uint64_t incomingBoard);
|
void deserializeBoard(uint64_t incomingBoard);
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -88,7 +88,8 @@ class Piece {
|
||||||
PieceColour colour;
|
PieceColour colour;
|
||||||
PieceType pieceType;
|
PieceType pieceType;
|
||||||
std::string pieceName; /**< std::string pieceName is a string showing the full name of the piece, i.e. "PAWN" */
|
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;
|
bool hasMoved = false;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
|
@ -96,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;
|
||||||
|
|
@ -106,7 +107,7 @@ class Piece {
|
||||||
return pieceName;
|
return pieceName;
|
||||||
}
|
}
|
||||||
|
|
||||||
char getPieceSymbol() const {
|
std::string getPieceSymbol() const {
|
||||||
return pieceSymbol;
|
return pieceSymbol;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -139,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;
|
||||||
|
|
@ -149,10 +150,8 @@ 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;
|
||||||
}
|
}
|
||||||
|
|
@ -175,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;
|
||||||
|
|
@ -187,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;
|
||||||
|
|
@ -199,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;
|
||||||
|
|
@ -211,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;
|
||||||
|
|
@ -223,7 +222,7 @@ 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;
|
||||||
|
|
|
||||||
7
main.cpp
7
main.cpp
|
|
@ -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) {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue