Swift Playgrounds: Learn to Code 1: Roll Right Roll Left Infinitely Walking Around

Hello everyone,

doing the Swift playgrounds Learn to Code 1 and am stuck on the very last one.

My code walks the map and collects all gems and toggles all switches, but I am stumped on how to make him stop walking around when he's done doing all the tasks. Here's what I have:

func checker() {

    if isOnClosedSwitch {

        toggleSwitch()

    } else if isOnOpenSwitch {

        moveForward()

    }

}

while !isOnClosedSwitch {

        while !isOnGem {

            moveForward()

            if isBlockedRight && isBlocked {

                turnLeft()

                moveForward()

                checker()

                turnLeft()

                moveForward()

                checker()

            }

            if isBlocked {

                turnRight()

            }

        } collectGem()

}

Any suggestions on why he won't stop walking? Obviously, I'm very new to this, so I'm trying to think using the logic I'm learning from the course and could use whatever tips on this.

I entered your code and ran it. Byte doesn't stop walking because Byte isn't done. There's a little scoreboard at the top left of the scene - it shows Bytes has collected 1/1 gems and tripped 0/1 switches. Try to split your code into functions which do only one thing, and are well named. For instance, rename 'checker' to 'checkSwitch' and have it only change the switch state if necessary, rather than sometimes changes switch state, and sometimes move forward. In this exercise, the path is predictable (you can see it), so your steps can be a list of instructions, with no branches and no conditionals.

Swift Playgrounds: Learn to Code 1: Roll Right Roll Left Infinitely Walking Around
 
 
Q