dev: fixed bugs, added logic for bishop & knight

This commit is contained in:
A.M. Rowsell 2026-09-22 01:49:47 -04:00
commit 1842e25918
Signed by: amr
GPG key ID: E0879EDBDB0CA7B1

View file

@ -149,7 +149,7 @@ std::vector<Move> King::getLegalMoves(const Square &from, Board &board) const {
// we are being attacked by a knight, so remove it // we are being attacked by a knight, so remove it
moves.to.rank = INVALID_RANK; moves.to.rank = INVALID_RANK;
moves.to.file = INVALID_FILE; moves.to.file = INVALID_FILE;
} else if(target->getPieceType() == ROOK || target->getPieceType() == QUEEN && (!diagonal && !knight)) { } else if((target->getPieceType() == ROOK || target->getPieceType() == QUEEN) && (!diagonal && !knight)) {
// again, being attacked, remove it // again, being attacked, remove it
moves.to.rank = INVALID_RANK; moves.to.rank = INVALID_RANK;
moves.to.file = INVALID_FILE; moves.to.file = INVALID_FILE;
@ -159,6 +159,7 @@ std::vector<Move> King::getLegalMoves(const Square &from, Board &board) const {
moves.to.file = INVALID_FILE; moves.to.file = INVALID_FILE;
} }
} }
if(target && (target->getColour() == this->getColour())) { if(target && (target->getColour() == this->getColour())) {
// we've hit a friendly, stop // we've hit a friendly, stop
break; break;
@ -289,6 +290,17 @@ std::vector<Move> Knight::getLegalMoves(const Square &from, Board &board) const
{2, -1}, {2, -1},
{2, 1} {2, 1}
}; };
for(auto &dir : directions) {
int r = from.rank + dir[0];
int f = from.file + dir[1];
if(r >= 0 && r < 8 && f >= 0 && f < 8) {
Square targetSquare{static_cast<Rank>(r), static_cast<File>(f)};
const Piece *target = board.getPieceAt(targetSquare);
if(!target || (target && target->getColour() != this->getColour())) {
moveList.push_back({from, targetSquare});
}
}
}
return moveList; return moveList;
} }
@ -312,6 +324,24 @@ std::vector<Move> Bishop::getLegalMoves(const Square &from, Board &board) const
{1, -1}, // down-left {1, -1}, // down-left
{1, 1}, // down-right {1, 1}, // down-right
}; };
for(auto &dir : directions) {
int r = from.rank + dir[0];
int f = from.file + dir[1];
while(r >= 0 && r < 8 && f >= 0 && f < 8) {
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()) {
moveList.push_back({from, targetSquare});
break;
} else {
break;
}
r += dir[0];
f += dir[1];
}
}
return moveList; return moveList;
} }
@ -381,7 +411,7 @@ std::vector<Move> Pawn::getLegalMoves(const Square &from, Board &board) const {
const Piece *target = board.getPieceAt(targetSquare); const Piece *target = board.getPieceAt(targetSquare);
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.isSquareEmpty(Square{static_cast<Rank>(r - 1), static_cast<File>(f)}))
moveList.push_back({from, targetSquare}); moveList.push_back({from, targetSquare});
else else
continue; continue;