Compare commits

..

No commits in common. "a56fb4d60f0729bedfbbde6c559bd1f048aa5596" and "6e05baa87d1716c18d6d32ba08a108d0fd2aed85" have entirely different histories.

4 changed files with 39 additions and 59 deletions

View file

@ -7,7 +7,6 @@
// defined by the Mozilla Public License, v. 2.0. // defined by the Mozilla Public License, v. 2.0.
#include "inc/Board.hpp" #include "inc/Board.hpp"
#include "inc/Piece.hpp"
#include "inc/strFuncs.hpp" #include "inc/strFuncs.hpp"
/* /*
@ -242,8 +241,8 @@ int Board::setupFromFEN(std::string strFEN) {
// ====== START OF FIELD 3 ====== // ====== START OF FIELD 3 ======
std::string k = "k", K = "K", q = "q", Q = "Q"; std::string k = "k", K = "K", q = "q", Q = "Q";
// yeah this is hacky af but it works... blame SO // yeah this is hacky af but it works... blame SO
Piece *baseBKing = getPieceAt(blackKing); Piece *baseBKing = getPieceAt(blackKing).get();
Piece *baseWKing = getPieceAt(whiteKing); Piece *baseWKing = getPieceAt(whiteKing).get();
King *bKing = dynamic_cast<King *>(baseBKing); King *bKing = dynamic_cast<King *>(baseBKing);
King *wKing = dynamic_cast<King *>(baseWKing); King *wKing = dynamic_cast<King *>(baseWKing);
if(splitFEN[2] == "-") { if(splitFEN[2] == "-") {
@ -311,4 +310,8 @@ void Board::deserializeBoard(uint64_t incomingBoard) {
serialBoard.boardRows[i] = static_cast<uint8_t>((incomingBoard >> (8 * i)) & 0xFF); serialBoard.boardRows[i] = static_cast<uint8_t>((incomingBoard >> (8 * i)) & 0xFF);
// how do we then figure out what has moved? // how do we then figure out what has moved?
return; return;
}
std::unique_ptr<Piece> &Board::getPieceAt(Square square) {
return boardGrid[static_cast<int>(square.rank)][static_cast<int>(square.file)];
} }

View file

@ -7,7 +7,6 @@
// defined by the Mozilla Public License, v. 2.0. // defined by the Mozilla Public License, v. 2.0.
#include "inc/Piece.hpp" #include "inc/Piece.hpp"
#include "inc/Board.hpp"
Piece::~Piece() { Piece::~Piece() {
return; return;
@ -86,10 +85,12 @@ std::vector<Move> King::getLegalMoves(const Square &from, Board &board) const {
// establish r/f for square to check // establish r/f for square to check
int r = from.rank + dir[0]; int r = from.rank + dir[0];
int f = from.file + dir[1]; int f = from.file + dir[1];
Square targetSquare{static_cast<Rank>(r), static_cast<File>(f)}; auto ra = static_cast<Rank>(r);
auto fi = static_cast<File>(f);
Square targetSquare = {ra, fi};
if(targetSquare.isValid()) { if(targetSquare.isValid()) {
const Piece *target = board.getPieceAt(targetSquare); // examine the target square const auto &target = board.boardGrid[r][f]; // examine the target square
if(!target) { // if square is empty (nullptr) if(!target) { // if square is empty (NULL)
moveList.push_back({from, targetSquare}); // then it's potentially a legal move moveList.push_back({from, targetSquare}); // then it's potentially a legal move
} else if(target && target->getColour() != this->getColour()) { // if it's occupied with a piece of opposite colour } else if(target && target->getColour() != this->getColour()) { // if it's occupied with a piece of opposite colour
moveList.push_back({from, targetSquare}); // then again it's potentially legal moveList.push_back({from, targetSquare}); // then again it's potentially legal
@ -134,8 +135,7 @@ std::vector<Move> King::getLegalMoves(const Square &from, Board &board) const {
bool diagonal = (abs(dir[0]) + abs(dir[1]) == 2) ? 1 : 0; // check if diagonal bool diagonal = (abs(dir[0]) + abs(dir[1]) == 2) ? 1 : 0; // check if diagonal
bool knight = (abs(dir[0]) + abs(dir[1]) == 3) ? 1 : 0; // check if knight attack bool knight = (abs(dir[0]) + abs(dir[1]) == 3) ? 1 : 0; // check if knight attack
while(r >= 0 && r < 8 && f >= 0 && f < 8) { while(r >= 0 && r < 8 && f >= 0 && f < 8) {
Square targetSquare{static_cast<Rank>(r), static_cast<File>(f)}; auto &target = board.boardGrid[r][f]; // access the square we're examining
const Piece *target = board.getPieceAt(targetSquare); // access the square we're examining
if(!target) { if(!target) {
// empty square, continue // empty square, continue
continue; continue;
@ -204,8 +204,10 @@ std::vector<Move> Rook::getLegalMoves(const Square &from, Board &board) const {
int r = from.rank + dir[0]; int r = from.rank + dir[0];
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) {
Square targetSquare{static_cast<Rank>(r), static_cast<File>(f)}; const auto& target = board.boardGrid[r][f];
const Piece *target = board.getPieceAt(targetSquare); auto ra = static_cast<Rank>(r);
auto fi = static_cast<File>(f);
Square targetSquare = {ra, fi};
if(!target) { if(!target) {
moveList.push_back({from, targetSquare}); moveList.push_back({from, targetSquare});
} else if(target && target->getColour() != this->getColour()) { } else if(target && target->getColour() != this->getColour()) {
@ -246,9 +248,11 @@ std::vector<Move> Queen::getLegalMoves(const Square &from, Board &board) const {
// establish r/f for square to check // establish r/f for square to check
int r = from.rank + dir[0]; int r = from.rank + dir[0];
int f = from.file + dir[1]; int f = from.file + dir[1];
auto ra = static_cast<Rank>(r);
auto fi = static_cast<File>(f);
Square targetSquare = {ra, fi};
while(r >= 0 && r < 8 && f >= 0 && f < 8) { while(r >= 0 && r < 8 && f >= 0 && f < 8) {
Square targetSquare{static_cast<Rank>(r), static_cast<File>(f)}; const auto &target = board.boardGrid[r][f]; // examine the target square
const Piece *target = board.getPieceAt(targetSquare);// examine the target square
if(!target) { // if square is empty (NULL) if(!target) { // if square is empty (NULL)
moveList.push_back({from, targetSquare}); // then it's potentially a legal move moveList.push_back({from, targetSquare}); // then it's potentially a legal move
} else if(target && target->getColour() != this->getColour()) { // if it's occupied with a piece of opposite colour } else if(target && target->getColour() != this->getColour()) { // if it's occupied with a piece of opposite colour
@ -350,11 +354,13 @@ std::vector<Move> Pawn::getLegalMoves(const Square &from, Board &board) const {
int r = from.rank + dir[0]; int r = from.rank + dir[0];
int f = from.file + dir[1]; int f = from.file + dir[1];
if(r >= 0 && r < 8 && f >= 0 && f < 8) { // no need for a while loop as we only have finite moves in limited directions if(r >= 0 && r < 8 && f >= 0 && f < 8) { // no need for a while loop as we only have finite moves in limited directions
Square targetSquare{static_cast<Rank>(r), static_cast<File>(f)}; const auto& target = board.boardGrid[r][f];
const Piece *target = board.getPieceAt(targetSquare); auto ra = static_cast<Rank>(r);
auto fi = static_cast<File>(f);
Square targetSquare = {ra, fi};
if(dir[0] == -2 && !this->checkIfMoved()) { if(dir[0] == -2 && !this->checkIfMoved()) {
// then 2 is potentially legal // then 2 is potentially legal
if(!target && board.isSquareEmpty(Square{static_cast<Rank>(r + 1), static_cast<File>(f)})) // check both squares for pieces of any colour if(!target && !(board.boardGrid[r + 1][f])) // check both squares for pieces of any colour
moveList.push_back({from, targetSquare}); moveList.push_back({from, targetSquare});
else else
continue; continue;
@ -377,11 +383,13 @@ std::vector<Move> Pawn::getLegalMoves(const Square &from, Board &board) const {
int r = from.rank + dir[0]; int r = from.rank + dir[0];
int f = from.file + dir[1]; int f = from.file + dir[1];
if(r >= 0 && r < 8 && f >= 0 && f < 8) { // no need for a while loop as we only have finite moves in limited directions if(r >= 0 && r < 8 && f >= 0 && f < 8) { // no need for a while loop as we only have finite moves in limited directions
Square targetSquare{static_cast<Rank>(r), static_cast<File>(f)}; const auto& target = board.boardGrid[r][f];
const Piece *target = board.getPieceAt(targetSquare); auto ra = static_cast<Rank>(r);
auto fi = static_cast<File>(f);
Square targetSquare = {ra, fi};
if(dir[0] == 2 && !this->checkIfMoved()) { if(dir[0] == 2 && !this->checkIfMoved()) {
// then 2 is potentially legal // then 2 is potentially legal
if(!target && board.isSquareEmpty(Square{static_cast<Rank>(r + 1), static_cast<File>(f)})) if(!target && !(board.boardGrid[r - 1][f]))
moveList.push_back({from, targetSquare}); moveList.push_back({from, targetSquare});
else else
continue; continue;

View file

@ -13,8 +13,10 @@
#include <memory> #include <memory>
#include <string> #include <string>
#include <vector> #include <vector>
#include "Piece.hpp" #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 };
@ -23,56 +25,23 @@ 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
std::vector<std::vector<std::unique_ptr<Piece>>> boardGrid;
Players playerTurn; 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: 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;
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(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 setupInitialPosition();
void clearBoard(); void clearBoard();
std::unique_ptr<Piece> &getPieceAt(Square square);
void movePiece(Square from, Square to); void movePiece(Square from, Square to);
void nextTurn(); void nextTurn();
int setupFromFEN(std::string strFEN); int setupFromFEN(std::string strFEN);
bool isInBounds(Square square) const; bool isInBounds(Square square) const;
bool isEmpty(Square square) const;
// serial shift register stuff // serial shift register stuff
uint64_t serialBoard = 0xFFFF00000000FFFF; // opening position uint64_t serialBoard = 0xFFFF00000000FFFF; // opening position
void deserializeBoard(uint64_t incomingBoard); void deserializeBoard(uint64_t incomingBoard);

View file

@ -28,7 +28,7 @@
#include <string> #include <string>
#include <memory> #include <memory>
#include <vector> #include <vector>
#include "Board.hpp"
class Board; class Board;
enum PieceType { enum PieceType {