style: implement clang-format styling parameters
This commit is contained in:
parent
10a4dcb60b
commit
f2ce83335c
10 changed files with 462 additions and 547 deletions
|
|
@ -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 <cstdint>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#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<std::vector<std::unique_ptr<Piece>>> 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<std::vector<std::unique_ptr<Piece>>>
|
||||
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<Piece> &at(int r, int f) {
|
||||
return boardGrid[r][f];
|
||||
}
|
||||
std::unique_ptr<Piece> &at(int r, int f) { return boardGrid[r][f]; }
|
||||
|
||||
const std::unique_ptr<Piece> &at(int r, int f) const {
|
||||
return boardGrid[r][f];
|
||||
}
|
||||
const std::unique_ptr<Piece> &at(int r, int f) const { return boardGrid[r][f]; }
|
||||
|
||||
std::unique_ptr<Piece> &at(const Square &sq) {
|
||||
return boardGrid[static_cast<int>(sq.rank)][static_cast<int>(sq.file)];
|
||||
|
|
@ -68,6 +65,7 @@ class Board {
|
|||
const std::unique_ptr<Piece> &at(const Square &sq) const {
|
||||
return boardGrid[static_cast<int>(sq.rank)][static_cast<int>(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> piece) {
|
||||
at(r, f) = std::move(piece);
|
||||
}
|
||||
void setPieceAt(const Square &sq, std::unique_ptr<Piece> piece) {
|
||||
at(sq) = std::move(piece);
|
||||
}
|
||||
void setPieceAt(int r, int f, std::unique_ptr<Piece> piece) { at(r, f) = std::move(piece); }
|
||||
void setPieceAt(const Square &sq, std::unique_ptr<Piece> 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);
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
||||
|
|
|
|||
|
|
@ -22,34 +22,24 @@
|
|||
* it helps make it obvious what certain bits of code are doing.
|
||||
*/
|
||||
|
||||
#include <cstdint>
|
||||
#include <algorithm>
|
||||
#include <utility>
|
||||
#include <string>
|
||||
#include <cstdint>
|
||||
#include <memory>
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <vector>
|
||||
|
||||
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<Move> getLegalMoves(const Square &from, Board &board) const;
|
||||
|
||||
bool finalMoveChecks(std::vector<Move> *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<Move> getLegalMoves(const Square &from, Board &board) const override;
|
||||
void promote(const Square &promotionSquare, Board &board, PieceType promoteTo);
|
||||
|
||||
protected:
|
||||
bool firstMove = true;
|
||||
};
|
||||
#endif // PIECE_HPP
|
||||
|
||||
|
|
|
|||
|
|
@ -6,18 +6,15 @@
|
|||
*/
|
||||
|
||||
#ifndef STRFUNCS_HPP
|
||||
#define STRFUNCS_HPP
|
||||
#define STRFUNCS_HPP
|
||||
|
||||
#include <string>
|
||||
#include <sstream>
|
||||
#include <vector>
|
||||
#include <iterator>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
template <typename Out>
|
||||
void split(const std::string &s, char delim, Out result);
|
||||
template <typename Out> void split(const std::string &s, char delim, Out result);
|
||||
|
||||
std::vector<std::string> split(const std::string &s, char delim);
|
||||
|
||||
|
||||
#endif /* STRFUNCS_HPP */
|
||||
|
||||
#endif /* STRFUNCS_HPP */
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue