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;
class Board {
private:
std::vector<std::vector<std::unique_ptr<Piece>>> boardGrid;
friend class Piece;
public:
Board();
virtual ~Board();
@ -32,6 +30,9 @@ public:
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;
};
#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} // Right
};
return moveList;
}
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];
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) {
moves.push_back({from,
{r, f}});
} else if (target->color != this->color) {
moves.push_back({from,
{r, f}});
moveList.push_back({from,
targetSquare});
} else if (target->getColour() != this->colour) {
moveList.push_back({from,
targetSquare});
break;
} else {
break;
@ -70,3 +72,27 @@ std::vector<Move> Rook::getLegalMoves(const Square &from, const Board &board) co
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"
class Board;
enum PieceType { PAWN, BISHOP, KNIGHT, ROOK, QUEEN, KING, 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 };
enum File { A = 0, B = 1, C = 2, D = 3, E = 4, F = 5, G = 6, H = 7 };
enum PieceType {
PAWN, BISHOP, KNIGHT, ROOK, QUEEN, KING, 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
};
enum File {
A = 0, B = 1, C = 2, D = 3, E = 4, F = 5, G = 6, H = 7
};
struct Square {
Rank rank;
File file;
bool isValid() const {
return rank >= 0 && rank < 8 && file >= 0 && file < 8;
}
};
struct Move {
Square from;
Square to;
};
class Piece {
friend class Board;
protected:
PieceColour colour;
bool hasMoved = false;
public:
// methods
Piece(PieceColour pColour) : colour(pColour) {}
Piece(PieceColour pColour) : colour(pColour) {
}
virtual ~Piece();
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 {
King(PieceColour colour) : Piece(colour) {}
public:
King(PieceColour colour) : Piece(colour) {
}
virtual std::vector<Move> getLegalMoves(const Square &from, const Board &board) const override;
};
class Rook : public Piece {
Rook(PieceColour colour) : Piece(colour) {}
virtual std::vector<Move> getLegalMoves(const square &from, const Board &board) const override;
public:
Rook(PieceColour colour) : Piece(colour) {
}
virtual std::vector<Move> getLegalMoves(const Square &from, const Board &board) const override;
};
class Queen : public Piece {
Queen(PieceColour colour) : Piece(colour) {}
virtual std::vector<Move> getLegalMoves(const square &from, const Board &board) const override;
public:
Queen(PieceColour colour) : Piece(colour) {
}
virtual std::vector<Move> getLegalMoves(const Square &from, const Board &board) const override;
};
class Knight: public Piece {
Knight(PieceColour colour) : Piece(colour) {}
virtual std::vector<Move> getLegalMoves(const square &from, const Board &board) const override;
class Knight : public Piece {
public:
Knight(PieceColour colour) : Piece(colour) {
}
virtual std::vector<Move> getLegalMoves(const Square &from, const Board &board) const override;
};
class Bishop : public Piece {
Bishop(PieceColour colour) : Piece(colour) {}
virtual std::vector<Move> getLegalMoves(const square &from, const Board &board) const override;
public:
Bishop(PieceColour colour) : Piece(colour) {
}
virtual std::vector<Move> getLegalMoves(const Square &from, const Board &board) const override;
};
class Pawn : public Piece {
Pawn(PieceColour colour) : Piece(colour) {}
virtual std::vector<Move> getLegalMoves(const square &from, const Board &board) const override;
public:
Pawn(PieceColour colour) : Piece(colour) {
}
virtual std::vector<Move> getLegalMoves(const Square &from, const Board &board) const override;
};
#endif // PIECE_HPP

View file

@ -66,7 +66,7 @@
</archiverTool>
<loading>
<useAlternateLoadableFile>false</useAlternateLoadableFile>
<parseOnProdLoad>false</parseOnProdLoad>
<parseOnProdLoad>true</parseOnProdLoad>
<alternateLoadableFile></alternateLoadableFile>
</loading>
<subordinates>
@ -280,7 +280,7 @@
</archiverTool>
<loading>
<useAlternateLoadableFile>false</useAlternateLoadableFile>
<parseOnProdLoad>false</parseOnProdLoad>
<parseOnProdLoad>true</parseOnProdLoad>
<alternateLoadableFile></alternateLoadableFile>
</loading>
<subordinates>
@ -425,7 +425,7 @@
<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-symbols" value="false"/>
<property key="enable-unroll-loops" value="false"/>
<property key="exceptions" value="true"/>
<property key="exclude-floating-point" value="true"/>