dev: refactoring Board to be simpler for Piece to use
This commit adds a bunch of getters/setters for accessing the boardGrid. This is a simpler way to allow both Piece and all the inherited classes to access the Board without having to declare every single class a friend. So now when we want to access boardGrid, we use getPieceAt() or setPieceAt(). They are overloaded to accept rank/file, Square, or int/int. There's also a convenience function to check if the square is empty, though that may get re-written to use the existing Square.isValid().
This commit is contained in:
parent
6e05baa87d
commit
c333e3da9e
1 changed files with 52 additions and 7 deletions
|
|
@ -14,9 +14,7 @@
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
|
||||||
#include "Piece.hpp"
|
// suggested to forward declare here, and put include in the .cpp
|
||||||
|
|
||||||
// why do I have to forward declare all these?!
|
|
||||||
class Piece;
|
class Piece;
|
||||||
|
|
||||||
enum Players { PL_WHITE, PL_BLACK };
|
enum Players { PL_WHITE, PL_BLACK };
|
||||||
|
|
@ -25,14 +23,61 @@ struct Square;
|
||||||
class Board {
|
class Board {
|
||||||
private:
|
private:
|
||||||
friend class Piece; // this doesn't seem to do anything
|
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;
|
std::vector<std::vector<std::unique_ptr<Piece>>> boardGrid;
|
||||||
|
Players playerTurn;
|
||||||
|
// let's get super object-oriented, baby
|
||||||
|
// these help the getters and setters access the boardGrid
|
||||||
|
// and also make them shorter and less duplicative
|
||||||
|
inline std::unique_ptr<Piece>& at(int r, int f) {
|
||||||
|
return boardGrid[r][f];
|
||||||
|
}
|
||||||
|
inline const std::unique_ptr<Piece>& at(int r, int f) const {
|
||||||
|
return boardGrid[r][f];
|
||||||
|
}
|
||||||
|
|
||||||
|
inline std::unique_ptr<Piece>& at(Rank rank, File file) {
|
||||||
|
return boardGrid[static_cast<int>(rank)][static_cast<int>(file)];
|
||||||
|
}
|
||||||
|
inline const std::unique_ptr<Piece>& at(Rank rank, File file) const {
|
||||||
|
return boardGrid[static_cast<int>(rank)][static_cast<int>(file)];
|
||||||
|
}
|
||||||
|
|
||||||
|
inline std::unique_ptr<Piece>& at(const Square& sq) {
|
||||||
|
return boardGrid[static_cast<int>(sq.rank)][static_cast<int>(sq.file)];
|
||||||
|
}
|
||||||
|
inline const std::unique_ptr<Piece>& at(const Square& sq) const {
|
||||||
|
return boardGrid[static_cast<int>(sq.rank)][static_cast<int>(sq.file)];
|
||||||
|
}
|
||||||
|
public:
|
||||||
Board();
|
Board();
|
||||||
virtual ~Board();
|
virtual ~Board();
|
||||||
|
|
||||||
|
// These are to allow Piece to access Board in a controlled way
|
||||||
|
// --- 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(Rank rank, File file) { return at(rank, file).get(); }
|
||||||
|
const Piece* getPieceAt(Rank rank, File file) const { return at(rank, file).get(); }
|
||||||
|
|
||||||
|
Piece* getPieceAt(const Square& sq) { return at(sq).get(); }
|
||||||
|
const Piece* getPieceAt(const Square& sq) const { return at(sq).get(); }
|
||||||
|
|
||||||
|
// --- SETTERS ---
|
||||||
|
void setPieceAt(int r, int f, std::unique_ptr<Piece> piece) { at(r, f) = std::move(piece); }
|
||||||
|
void clearSquare(int r, int f) { at(r, f).reset(); }
|
||||||
|
|
||||||
|
void setPieceAt(Rank rank, File file, std::unique_ptr<Piece> piece) { at(rank, file) = std::move(piece); }
|
||||||
|
void clearSquare(Rank rank, File file) { at(rank, file).reset(); }
|
||||||
|
|
||||||
|
void setPieceAt(const Square& sq, std::unique_ptr<Piece> piece) { at(sq) = std::move(piece); }
|
||||||
|
void clearSquare(const Square& sq) { at(sq).reset(); }
|
||||||
|
|
||||||
|
// --- CHECKERS ---
|
||||||
|
bool isSquareEmpty(int r, int f) const { return at(r, f) == nullptr; }
|
||||||
|
bool isSquareEmpty(Rank rank, File file) const { return at(rank, file) == nullptr; }
|
||||||
|
bool isSquareEmpty(const Square& sq) const { return at(sq) == nullptr; }
|
||||||
|
|
||||||
void setupInitialPosition();
|
void setupInitialPosition();
|
||||||
|
|
||||||
void clearBoard();
|
void clearBoard();
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue