Swift Playground - Round up the switches - character won't move

Trying to find an easy solution for the "Round up the switches" level. I think the while-loop is bugged - I have no explanation for the problem...

var gemcount = 0

var switchcount = 0

func movement() {

    moveForward()

    if isBlocked && !isBlockedRight{

        turnRight()

    }

    if isBlocked && !isBlockedLeft{

        turnLeft()

    }

}

func collect() {

    if isOnGem {

        collectGem()

        gemcount = gemcount + 1}

    }

func toggle() {

    if isOnClosedSwitch && switchcount != gemcount {

        toggleSwitch()

        switchcount = switchcount + 1}

}

while switchcount < gemcount {

    movement()

    collect()

    toggle()

}

What error do you get ?

problem is that both are 0

var gemcount = 0
var switchcount = 0

Hence,

while switchcount < gemcount

is never executed.

Replace by

var gemcount = 2
var switchcount = 0

Thanks - it's one step further :)

But: how do I code the while-loop correctly? The loop should run until gemcount = switchcount.

Late to this, but in case anyone else stumbles across this and is stuck, here is my suggestion to the code above:

while switchCount < gemCount **|| gemCount < 1{**

    movement()

    collect()

    toggle()

}

The issue is because both switchCount and gemCount are = 0. This prevents the while switchCount < gemCount loop to start. Adding || gemCount < 1, allows this loop to start.

start the loof by using a for i in 1 ... 3 { moveForward() if isOnGem { collectGem() gemCounter += 1 }

it cretats a difference in the initial steps

var switchCounter = 0 var gemCounter = 0 for i in 1 ... 3 { moveForward() if isOnGem { collectGem() gemCounter += 1 }

}

while switchCounter < gemCounter{ if isOnGem { collectGem() gemCounter += 1 } else if isOnClosedSwitch { toggleSwitch() switchCounter += 1 }else if isBlocked { turnRight() } moveForward()

}

WORKED , still open to inprovment

Swift Playground - Round up the switches - character won't move
 
 
Q