Learn to code 1 issue

Hi team, I’m new to swift - just getting started - and as such have been working through swift playgrounds on iPad however at the final exercise “ roll left, roll right” I’m having an issue escaping my while loop.
This is my current code:

while !(isBlocked &&  isBlockedLeft &&  isBlockedRight) {
    moveForward()
    if isBlocked && isBlockedLeft {
        turnRight()
    } else if isBlocked {
        turnLeft()
    }
    if isOnGem {
        collectGem()
    } else if isOnClosedSwitch {
        toggleSwitch()
    }
}

problem being that when Byte reaches the end of the maze and is blocked front, left, and right the code doesn’t exit as (I believe) it should but rather byte turns and loops the maze again.
what am I missing?
That's a great question!

Looking at your code it should work, but, for some reason that last tile with the stone blocking the path forward doesn't think it's being blocked forward, to the left or to the right.
(Try your code with an extra:
Code Block
if isBlocked {
        collectGem()
    }
    if isBlockedLeft {
        collectGem()
    }
    if isBlockedRight {
        collectGem()
    }

for the craic and you'll see that Byte jumps to collect gems in lots of other places, but not on that last tile until Byte turns.)

In the hints they mention that there's a certain pattern to when you turn left and when you turn right... and in the solution, from the teacher's guide, the while loop is working until we see that Byte isOnOpenSwitch.

But you're right, I think your algorithm should work, but I'd need to do some more digging to see why it's not...

I hope that helps!


(The teacher's guide for all the solutions, even for comparing against your own solutions, is available here: https://education-static.apple.com/teaching-code/learn-to-code-solutions.pdf)
Learn to code 1 issue
 
 
Q