dev: Fixing compilation/linking errors, added other piece classes

This commit is contained in:
A.M. Rowsell 2025-08-04 21:45:23 -04:00
commit 2d83f43cfb
Signed by untrusted user who does not match committer: amr
GPG key ID: E0879EDBDB0CA7B1
4 changed files with 103 additions and 38 deletions

View file

@ -19,9 +19,7 @@ enum Players { PL_WHITE, PL_BLACK };
struct Square; struct Square;
class Board { class Board {
private: friend class Piece;
std::vector<std::vector<std::unique_ptr<Piece>>> boardGrid;
public: public:
Board(); Board();
virtual ~Board(); virtual ~Board();
@ -32,6 +30,9 @@ public:
bool isEmpty(Square square) const; bool isEmpty(Square square) const;
uint64_t serialBoard = 0xFFFF00000000FFFF; // opening position uint64_t serialBoard = 0xFFFF00000000FFFF; // opening position
void deserializeBoard(uint64_t incomingBoard); 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;
}; };
#endif // BOARD_HPP #endif // BOARD_HPP

View file

@ -34,7 +34,7 @@ std::vector<Move> King::getLegalMoves(const Square &from, const Board &board) co
{0, -1}, // Left {0, -1}, // Left
{0, 1} // Right {0, 1} // Right
}; };
return moveList;
} }
std::vector<Move> Rook::getLegalMoves(const Square &from, const Board &board) const { std::vector<Move> Rook::getLegalMoves(const Square &from, const Board &board) const {
@ -50,14 +50,16 @@ std::vector<Move> Rook::getLegalMoves(const Square &from, const Board &board) co
int f = from.file + dir[1]; int f = from.file + dir[1];
while (r >= 0 && r < 8 && f >= 0 && f < 8) { while (r >= 0 && r < 8 && f >= 0 && f < 8) {
const auto& target = board[r][f]; const auto& target = board.boardGrid[r][f];
auto ra = static_cast<Rank> (r);
auto fi = static_cast<File> (f);
Square targetSquare = {ra, fi};
if (!target) { if (!target) {
moves.push_back({from, moveList.push_back({from,
{r, f}}); targetSquare});
} else if (target->color != this->color) { } else if (target->getColour() != this->colour) {
moves.push_back({from, moveList.push_back({from,
{r, f}}); targetSquare});
break; break;
} else { } else {
break; break;
@ -70,3 +72,27 @@ std::vector<Move> Rook::getLegalMoves(const Square &from, const Board &board) co
return moveList; return moveList;
} }
std::vector<Move> Queen::getLegalMoves(const Square &from, const Board &board) const {
std::vector<Move> moveList;
return moveList;
}
std::vector<Move> Knight::getLegalMoves(const Square &from, const Board &board) const {
std::vector<Move> moveList;
return moveList;
}
std::vector<Move> Bishop::getLegalMoves(const Square &from, const Board &board) const {
std::vector<Move> moveList;
return moveList;
}
std::vector<Move> Pawn::getLegalMoves(const Square &from, const Board &board) const {
std::vector<Move> moveList;
return moveList;
}

View file

@ -16,61 +16,99 @@
#include "Board.hpp" #include "Board.hpp"
class Board; class Board;
enum PieceType { PAWN, BISHOP, KNIGHT, ROOK, QUEEN, KING, EMPTY }; enum PieceType {
enum PieceColour { PIECE_WHITE, PIECE_BLACK, PIECE_EMPTY }; PAWN, BISHOP, KNIGHT, ROOK, QUEEN, KING, EMPTY
enum Rank { R1 = 0, R2 = 1, R3 = 2, R4 = 3, R5 = 4, R6 = 5, R7 = 6, R8 = 7 }; };
enum File { A = 0, B = 1, C = 2, D = 3, E = 4, F = 5, G = 6, H = 7 };
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
};
enum File {
A = 0, B = 1, C = 2, D = 3, E = 4, F = 5, G = 6, H = 7
};
struct Square { struct Square {
Rank rank; Rank rank;
File file; File file;
bool isValid() const { bool isValid() const {
return rank >= 0 && rank < 8 && file >= 0 && file < 8; return rank >= 0 && rank < 8 && file >= 0 && file < 8;
} }
}; };
struct Move { struct Move {
Square from; Square from;
Square to; Square to;
}; };
class Piece { class Piece {
friend class Board;
protected: protected:
PieceColour colour; PieceColour colour;
bool hasMoved = false;
public: public:
// methods Piece(PieceColour pColour) : colour(pColour) {
Piece(PieceColour pColour) : colour(pColour) {} }
virtual ~Piece(); virtual ~Piece();
virtual std::vector<Move> getLegalMoves(const Square &from, const Board &board) const; virtual std::vector<Move> getLegalMoves(const Square &from, const Board &board) const;
PieceColour getColour() const { return colour; }
PieceColour getColour() const {
return colour;
}
}; };
class King : public Piece { class King : public Piece {
King(PieceColour colour) : Piece(colour) {} public:
King(PieceColour colour) : Piece(colour) {
}
virtual std::vector<Move> getLegalMoves(const Square &from, const Board &board) const override; virtual std::vector<Move> getLegalMoves(const Square &from, const Board &board) const override;
}; };
class Rook : public Piece { class Rook : public Piece {
Rook(PieceColour colour) : Piece(colour) {} public:
virtual std::vector<Move> getLegalMoves(const square &from, const Board &board) const override;
Rook(PieceColour colour) : Piece(colour) {
}
virtual std::vector<Move> getLegalMoves(const Square &from, const Board &board) const override;
}; };
class Queen : public Piece { class Queen : public Piece {
Queen(PieceColour colour) : Piece(colour) {} public:
virtual std::vector<Move> getLegalMoves(const square &from, const Board &board) const override;
Queen(PieceColour colour) : Piece(colour) {
}
virtual std::vector<Move> getLegalMoves(const Square &from, const Board &board) const override;
}; };
class Knight: public Piece { class Knight : public Piece {
Knight(PieceColour colour) : Piece(colour) {} public:
virtual std::vector<Move> getLegalMoves(const square &from, const Board &board) const override;
Knight(PieceColour colour) : Piece(colour) {
}
virtual std::vector<Move> getLegalMoves(const Square &from, const Board &board) const override;
}; };
class Bishop : public Piece { class Bishop : public Piece {
Bishop(PieceColour colour) : Piece(colour) {} public:
virtual std::vector<Move> getLegalMoves(const square &from, const Board &board) const override;
Bishop(PieceColour colour) : Piece(colour) {
}
virtual std::vector<Move> getLegalMoves(const Square &from, const Board &board) const override;
}; };
class Pawn : public Piece { class Pawn : public Piece {
Pawn(PieceColour colour) : Piece(colour) {} public:
virtual std::vector<Move> getLegalMoves(const square &from, const Board &board) const override;
Pawn(PieceColour colour) : Piece(colour) {
}
virtual std::vector<Move> getLegalMoves(const Square &from, const Board &board) const override;
}; };
#endif // PIECE_HPP #endif // PIECE_HPP

View file

@ -66,7 +66,7 @@
</archiverTool> </archiverTool>
<loading> <loading>
<useAlternateLoadableFile>false</useAlternateLoadableFile> <useAlternateLoadableFile>false</useAlternateLoadableFile>
<parseOnProdLoad>false</parseOnProdLoad> <parseOnProdLoad>true</parseOnProdLoad>
<alternateLoadableFile></alternateLoadableFile> <alternateLoadableFile></alternateLoadableFile>
</loading> </loading>
<subordinates> <subordinates>
@ -280,7 +280,7 @@
</archiverTool> </archiverTool>
<loading> <loading>
<useAlternateLoadableFile>false</useAlternateLoadableFile> <useAlternateLoadableFile>false</useAlternateLoadableFile>
<parseOnProdLoad>false</parseOnProdLoad> <parseOnProdLoad>true</parseOnProdLoad>
<alternateLoadableFile></alternateLoadableFile> <alternateLoadableFile></alternateLoadableFile>
</loading> </loading>
<subordinates> <subordinates>
@ -425,7 +425,7 @@
<property key="eh-specs" value="true"/> <property key="eh-specs" value="true"/>
<property key="enable-app-io" value="false"/> <property key="enable-app-io" value="false"/>
<property key="enable-omit-frame-pointer" value="false"/> <property key="enable-omit-frame-pointer" value="false"/>
<property key="enable-symbols" value="true"/> <property key="enable-symbols" value="false"/>
<property key="enable-unroll-loops" value="false"/> <property key="enable-unroll-loops" value="false"/>
<property key="exceptions" value="true"/> <property key="exceptions" value="true"/>
<property key="exclude-floating-point" value="true"/> <property key="exclude-floating-point" value="true"/>