dev: refactoring things to be more OOP! whoop whoop
Still struggling to understand all these concepts, and I will admit to using ChatGPT to try and explain where I was going wrong which did help quite a bit. But if I get this right it will be much more robust and less "fragile" as they say.
This commit is contained in:
parent
c333e3da9e
commit
a56fb4d60f
4 changed files with 32 additions and 57 deletions
|
|
@ -13,7 +13,7 @@
|
|||
#include <memory>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "Piece.hpp"
|
||||
// suggested to forward declare here, and put include in the .cpp
|
||||
class Piece;
|
||||
|
||||
|
|
@ -28,24 +28,19 @@ class Board {
|
|||
// 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 {
|
||||
std::unique_ptr<Piece>& at(int r, int f) {
|
||||
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)];
|
||||
const std::unique_ptr<Piece>& at(int r, int f) const {
|
||||
return boardGrid[r][f];
|
||||
}
|
||||
|
||||
inline std::unique_ptr<Piece>& at(const Square& sq) {
|
||||
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 {
|
||||
|
||||
const std::unique_ptr<Piece>& at(const Square& sq) const {
|
||||
return boardGrid[static_cast<int>(sq.rank)][static_cast<int>(sq.file)];
|
||||
}
|
||||
public:
|
||||
|
|
@ -53,40 +48,31 @@ class Board {
|
|||
virtual ~Board();
|
||||
|
||||
// These are to allow Piece to access Board in a controlled way
|
||||
// --- GETTERS ---
|
||||
// ----- 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 ---
|
||||
// ----- 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(int r, int f) { at(r, f).reset(); }
|
||||
void clearSquare(const Square& sq) { at(sq).reset(); }
|
||||
|
||||
// --- CHECKERS ---
|
||||
// ----- Utility -----
|
||||
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 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);
|
||||
|
|
|
|||
|
|
@ -28,7 +28,7 @@
|
|||
#include <string>
|
||||
#include <memory>
|
||||
#include <vector>
|
||||
#include "Board.hpp"
|
||||
|
||||
class Board;
|
||||
|
||||
enum PieceType {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue