Beginner on Swift Playgrounds

Hi, I'm new to the Swift language but I've found the learning process to be quite enjoyable thus far. I have, however, been stumped by one of the "lessons". It is Swift Playgrounds > Learn to Code 1 > Algorithms > Which Way to Turn?


Here is the code I have used:


func     navigatethemaze() {
     if     isBlocked && !isBlockedRight {
     turnRight()
     moveForward()
     }
     if     isBlocked && !isBlockedLeft {
     turnLeft()
     moveForward()
     }
     if     isBlockedRight && !isBlocked || isBlockedLeft && !isBlocked {
     moveForward()
     }
}

while     !isonGem {
     while!     isOnClosedSwitch {
     navigatethemaze()
     }
     if     !isBlocked {
     toggleSwitch()
     turnRight()
     moveForward()
     } else {
     toggleSwitch()
     turnLeft()
     }
}

collectGem()   


I really don't know where I went wrong because I am able to toggle all of the switches just fine but the program does not want to stop the character when it is on a Gem, thus not allowing me to finish. I tried to attach a video of the problem but am not sure if it actually came, apologies.


Again, any help whatsoever is appreciated. Thank You.

Replies

I think he might be stuck at the navigateTheMaze function. This because it can't exit the

while !isOnGem {
     while !isOnClosedSwitch {   // <-- Can't exit this while Block/ It just always repeats lines 2&3
          navigateTheMaze()
     }
}

while-block. Therefore you first have to exit the while block at line 2, so that it can go on with checking the other condition at line 01.