From 31499ed6e1abf8cb39ac3c63859b149d31ad4e5b Mon Sep 17 00:00:00 2001 From: "A.M. Rowsell" Date: Tue, 26 Aug 2025 20:34:04 -0400 Subject: [PATCH] dev: working on check detection algorithm --- Piece.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 50 insertions(+), 7 deletions(-) diff --git a/Piece.cpp b/Piece.cpp index 40555f7..bab48ec 100644 --- a/Piece.cpp +++ b/Piece.cpp @@ -25,7 +25,7 @@ std::vector Piece::getLegalMoves(const Square &from, const Board &board) c std::vector King::getLegalMoves(const Square &from, const Board &board) const { std::vector moveList; - const int directions[8][2] = { + std::vector> directions = { {-1, 0}, // Up {-1, -1}, // up-left {-1, 1}, // up-right @@ -39,11 +39,12 @@ std::vector King::getLegalMoves(const Square &from, const Board &board) co // establish r/f for square to check int r = from.rank + dir[0]; int f = from.file + dir[1]; - if(r >= 0 && r < 8 && f >= 0 && f < 8) { + auto ra = static_cast(r); + auto fi = static_cast(f); + Square targetSquare = {ra, fi}; + if(targetSquare.isValid()) { const auto &target = board.boardGrid[r][f]; // examine the target square - auto ra = static_cast(r); - auto fi = static_cast(f); - Square targetSquare = {ra, fi}; + 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 @@ -61,19 +62,61 @@ std::vector King::getLegalMoves(const Square &from, const Board &board) co // - Determine the piece type // - Determine the direction we are looking at (diagonal or not) // - If that piece can attack in that direction, it would be check, so remove this move from moveList + // We need to add knight attack to the directions to check + /* + * __3_4___ + * _1___2__ + * ___K____ + * _5___6__ + * __7_8___ + * + */ + std::vector> knightAttacks = { + {-1, -2}, // 1 + {-1, 2}, // 2 + {-2, -1}, // 3 + {-2, 1}, // 4 + {1, -2}, // 5 + {1, 2}, // 6 + {2, -1}, // 7 + {2, 1} // 8 + }; + directions.insert(directions.end(), knightAttacks.begin(), knightAttacks.end()); // concatenate the knight attacks for(auto &moves : moveList) { - Square startSquare = moves.to; // get the potential move to square + Square startSquare = moves.to; // get the potential moveto square for(auto &dir : directions) { // now go through all directions int r = startSquare.rank + dir[0]; int f = startSquare.file + dir[1]; 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 - if(target && target->getColour() != this->getColour()) { // is it occupied & an enemy? + if(!target) { + // empty square, continue + continue; + } + 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??? } + if(target->getPieceType() == KNIGHT && knight) { + // we are being attacked by a knight, so remove it + //moves.to.rank = 255; moves.to.file = 255; + } + if(target->getPieceType() == ROOK && (!diagonal && !knight)) { + // again, being attacked, remove it + } + } + if(target && (target->getColour() == this->getColour())) { + // we've hit a friendly, stop + break; + } + if(!knight) { + r += dir[0]; + f += dir[1]; + } else { + break; } } }