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