dev: Updates to Board and Piece, formatted code

This commit is contained in:
A.M. Rowsell 2025-09-07 17:59:16 -04:00
commit f05dd6d90f
Signed by untrusted user who does not match committer: amr
GPG key ID: E0879EDBDB0CA7B1
6 changed files with 122 additions and 121 deletions

View file

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

View file

@ -11,12 +11,12 @@
/** @file Piece.hpp
* @brief The header files for the Piece class.
*
*
* This file contains not only the definition of the Piece class itself, but also all
* of the derived classes (King, Queen, Rook, etc.) I was considering having them be in
* separate files, but each derived class does not contain that much code, so keeping them
* all in one place seemed easier to deal with. This may change in the future!
*
*
* Also defined here are important enums that help abstract chess terms, for example the
* PieceType, PieceColour, Rank, File, etc. These allow the code to be more readable and
* it helps make it obvious what certain bits of code are doing.
@ -77,7 +77,7 @@ struct Move {
/**
* @class Piece Piece.hpp "inc/Piece.hpp"
* @brief A class abstracting chess pieces
*
*
* This is a polymorphic base class containing the basic properties all chess pieces have.
* It is intended to be extended by a child class for each piece Type, i.e. King, Queen, etc.
*/
@ -88,7 +88,8 @@ class Piece {
PieceColour colour;
PieceType pieceType;
std::string pieceName; /**< std::string pieceName is a string showing the full name of the piece, i.e. "PAWN" */
std::string pieceSymbol; /**< std::string pieceSymbol is a single character, using the standard algebraic chess notation, ie N for Knight */
std::string
pieceSymbol; /**< std::string pieceSymbol is a single character, using the standard algebraic chess notation, ie N for Knight */
bool hasMoved = false;
public:
@ -96,7 +97,7 @@ class Piece {
Piece(PieceColour pColour) : colour(pColour) {
}
virtual ~Piece();
virtual std::vector<Move> getLegalMoves(const Square &from, Board &board) const;
PieceColour getColour() const {
return colour;
@ -106,7 +107,7 @@ class Piece {
return pieceName;
}
char getPieceSymbol() const {
std::string getPieceSymbol() const {
return pieceSymbol;
}
@ -121,7 +122,7 @@ class Piece {
* A virtual const function that should get all legal moves for a piece, but it may
* include some moves that need to be pruned, like those exposing the king to check.
* Still working out how to handle this.
*
*
* @param from the Square the Piece is currently on
* @param board A reference to the Board we are analyzing
* @return std::vector of Move: a list of all potentially legal moves
@ -129,7 +130,7 @@ class Piece {
virtual std::vector<Move> getLegalMoves(const Square &from, Board &board) const;
bool finalMoveChecks(std::vector<Move> *moveList, Board &board);
};
class King : public Piece {
@ -139,7 +140,7 @@ class King : public Piece {
King(PieceColour colour) : Piece(colour) {
pieceName = "King";
pieceSymbol = 'K';
pieceSymbol = "K";
pieceType = KING;
}
virtual std::vector<Move> getLegalMoves(const Square &from, Board &board) const override;
@ -149,14 +150,12 @@ class King : public Piece {
bool checkForCastle(enum CastleSide side) const {
if(side == KINGSIDE)
return canCastleKS;
else if(side == QUEENSIDE)
return canCastleQS;
else
return false;
}
void setCastleQS(bool canCastle) {
canCastleQS = canCastle;
}
@ -175,7 +174,7 @@ class Rook : public Piece {
Rook(PieceColour colour) : Piece(colour) {
pieceName = "Rook";
pieceSymbol = 'R';
pieceSymbol = "R";
pieceType = ROOK;
}
virtual std::vector<Move> getLegalMoves(const Square &from, Board &board) const override;
@ -187,7 +186,7 @@ class Queen : public Piece {
Queen(PieceColour colour) : Piece(colour) {
pieceName = "Queen";
pieceSymbol = 'Q';
pieceSymbol = "Q";
pieceType = QUEEN;
}
virtual std::vector<Move> getLegalMoves(const Square &from, Board &board) const override;
@ -199,7 +198,7 @@ class Knight : public Piece {
Knight(PieceColour colour) : Piece(colour) {
pieceName = "Knight";
pieceSymbol = 'N';
pieceSymbol = "N";
pieceType = KNIGHT;
}
virtual std::vector<Move> getLegalMoves(const Square &from, Board &board) const override;
@ -211,7 +210,7 @@ class Bishop : public Piece {
Bishop(PieceColour colour) : Piece(colour) {
pieceName = "Bishop";
pieceSymbol = 'B';
pieceSymbol = "B";
pieceType = BISHOP;
}
virtual std::vector<Move> getLegalMoves(const Square &from, Board &board) const override;
@ -223,12 +222,12 @@ class Pawn : public Piece {
Pawn(PieceColour colour) : Piece(colour) {
pieceName = "Pawn";
pieceSymbol = 'P';
pieceSymbol = "P";
pieceType = PAWN;
}
virtual std::vector<Move> getLegalMoves(const Square &from, Board &board) const override;
void promote(const Square &promotionSquare, Board &board, PieceType promoteTo);
protected:
protected:
bool vulnEnPassant = false;
bool firstMove = true;
};

View file

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