bool ChessMove::IsValidMove(const ChessBoard& board) const{ if (endX < 0 || endX > 7 || endY < 0 || endY > 7) return false; ChessPiece* startPiece = board.GetPiece(startX, startY); ChessPiece* occupyingPiece = board.GetPiece(endX, endY); ChessColor color = startPiece->GetColor(); if (occupyingPiece && occupyingPiece->GetColor() == color) return false; switch (startPiece->GetRank()){ case PAWN: return IsValidMovePawn(board) || IsValidEnPassantMove(board, GetDelta(color), GetStartingRow(color)); case ROOK: return IsValidMoveRook(board); case KNIGHT: return IsValidMoveKnight(); case BISHOP: return IsValidMoveBishop(board); case QUEEN: return IsValidMoveBishop(board) || IsValidMoveRook(board); case KING: return IsValidMoveKing() || IsValidCastleMove(board); } return false; }
bool ChessMove::IsValidEnPassantMove(const ChessBoard& board, int delta, int startingRow) const{ if (startY != startingRow+delta*3 || abs(startX-endX) != 1 || startY+delta != endY) return false; if (board.GetPiece(endX, endY)) return false; ChessPiece* potentialPawn = board.GetPiece(endX, startY); return potentialPawn && potentialPawn->GetRank()==PAWN && potentialPawn->GetMoveCount()==1; }