King: finalizing check detection and getLegalMoves()

This commit is contained in:
A.M. Rowsell 2025-08-27 14:22:26 -04:00
commit 22ac250f70
Signed by untrusted user who does not match committer: amr
GPG key ID: E0879EDBDB0CA7B1
4 changed files with 38 additions and 13 deletions

View file

@ -44,7 +44,6 @@ std::vector<Move> King::getLegalMoves(const Square &from, const Board &board) co
Square targetSquare = {ra, fi};
if(targetSquare.isValid()) {
const auto &target = board.boardGrid[r][f]; // 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
@ -68,8 +67,8 @@ std::vector<Move> King::getLegalMoves(const Square &from, const Board &board) co
* _1___2__
* ___K____
* _5___6__
* __7_8___
*
* __7_8___
*
*/
std::vector<std::vector<int>> knightAttacks = {
{-1, -2}, // 1
@ -90,7 +89,7 @@ std::vector<Move> King::getLegalMoves(const Square &from, const Board &board) co
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]; // check what's at the square we're examining
auto &target = board.boardGrid[r][f]; // access the square we're examining
if(!target) {
// empty square, continue
continue;
@ -98,14 +97,18 @@ std::vector<Move> King::getLegalMoves(const Square &from, const Board &board) co
if(target && (target->getColour() != this->getColour())) { // is it occupied & an enemy?
if((target->getPieceType() == QUEEN || target->getPieceType() == BISHOP) && diagonal) {
// we are being attacked diagonally on this square, so remove it
//moves.to.rank = 255; moves.to.file = 255; // mark as invalid???
moves.to.rank = INVALID_RANK; moves.to.file = INVALID_FILE;
}
if(target->getPieceType() == KNIGHT && knight) {
else if(target->getPieceType() == KNIGHT && knight) {
// we are being attacked by a knight, so remove it
//moves.to.rank = 255; moves.to.file = 255;
moves.to.rank = INVALID_RANK; moves.to.file = INVALID_FILE;
}
if(target->getPieceType() == ROOK && (!diagonal && !knight)) {
else if(target->getPieceType() == ROOK || target->getPieceType() == QUEEN && (!diagonal && !knight)) {
// again, being attacked, remove it
moves.to.rank = INVALID_RANK; moves.to.file = INVALID_FILE;
}
if(target->getPieceType() == PAWN && (abs(r - startSquare.rank) == 1 && abs(f - startSquare.file) == 1)) {
moves.to.rank = INVALID_RANK; moves.to.file = INVALID_FILE;
}
}
if(target && (target->getColour() == this->getColour())) {
@ -115,13 +118,18 @@ std::vector<Move> King::getLegalMoves(const Square &from, const Board &board) co
if(!knight) {
r += dir[0];
f += dir[1];
} else {
} else
break;
}
}
}
}
// will this actually work?
moveList.erase(
std::remove_if(moveList.begin(), moveList.end(),
[&](const Square &x) {
return std::find(x.begin(), x.end(), !x.isValid()) != x.end();
}),
moveList.end());
return moveList;
}