diff --git a/Piece.cpp b/Piece.cpp index 00e787e..6f5260e 100644 --- a/Piece.cpp +++ b/Piece.cpp @@ -149,7 +149,7 @@ std::vector King::getLegalMoves(const Square &from, Board &board) const { // we are being attacked by a knight, so remove it moves.to.rank = INVALID_RANK; 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 moves.to.rank = INVALID_RANK; moves.to.file = INVALID_FILE; @@ -159,6 +159,7 @@ std::vector King::getLegalMoves(const Square &from, Board &board) const { moves.to.file = INVALID_FILE; } } + if(target && (target->getColour() == this->getColour())) { // we've hit a friendly, stop break; @@ -289,6 +290,17 @@ std::vector Knight::getLegalMoves(const Square &from, Board &board) const {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(r), static_cast(f)}; + const Piece *target = board.getPieceAt(targetSquare); + if(!target || (target && target->getColour() != this->getColour())) { + moveList.push_back({from, targetSquare}); + } + } + } return moveList; } @@ -312,6 +324,24 @@ std::vector Bishop::getLegalMoves(const Square &from, Board &board) const {1, -1}, // down-left {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(r), static_cast(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; } @@ -381,7 +411,7 @@ std::vector Pawn::getLegalMoves(const Square &from, Board &board) const { const Piece *target = board.getPieceAt(targetSquare); if(dir[0] == 2 && !this->checkIfMoved()) { // then 2 is potentially legal - if(!target && board.isSquareEmpty(Square{static_cast(r + 1), static_cast(f)})) + if(!target && board.isSquareEmpty(Square{static_cast(r - 1), static_cast(f)})) moveList.push_back({from, targetSquare}); else continue;