chessmcu/inc/Board.hpp

101 lines
2.8 KiB
C++

// © 2025 A.M. Rowsell <amr@frzn.dev>
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
// This Source Code Form is "Incompatible With Secondary Licenses", as
// defined by the Mozilla Public License, v. 2.0.
#ifndef BOARD_HPP
#define BOARD_HPP
#include <cstdint>
#include <memory>
#include <string>
#include <vector>
#include "Piece.hpp"
// suggested to forward declare here, and put include in the .cpp
class Piece;
enum Players { PL_WHITE, PL_BLACK };
struct Square;
class Board {
private:
friend class Piece; // this doesn't seem to do anything
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
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];
}
std::unique_ptr<Piece> &at(const Square& sq) {
return boardGrid[static_cast<int>(sq.rank)][static_cast<int>(sq.file)];
}
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();
// 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(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 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();
}
// ----- Utility -----
bool isSquareEmpty(int r, int f) const {
return at(r, f) == nullptr;
}
bool isSquareEmpty(const Square& sq) const {
return at(sq) == nullptr;
}
void setupInitialPosition();
void clearBoard();
void movePiece(Square from, Square to);
void nextTurn();
int setupFromFEN(std::string strFEN);
bool isInBounds(Square square) const;
// serial shift register stuff
uint64_t serialBoard = 0xFFFF00000000FFFF; // opening position
void deserializeBoard(uint64_t incomingBoard);
};
#endif // BOARD_HPP