Roll right roll left swift playgrounds

hi guy so not sure if I'm asking this question in the right place but trying to learn to code, complete novice, I'm just at the end of the Lear to code 1 in swift playgrounds and I'm stuck on the roll right roll left challange,


if if any one could help me out would really appreciate it,



while isBlockedLeft || isBlockedRight {

moveForward()

if isBlocked && isBlockedRight {

turnLeft()

}

if isBlocked && isBlockedLeft {

turnRight()

}

if isBlocked && !isBlockedRight {

turnLeft()

}

while isOnGem {

collectGem()

}

while isOnClosedSwitch {

toggleSwitch()

}

}

}

Replies

this is what I got so far,


ignore above


while !isOnClosedSwitch {

while isBlockedLeft || isBlockedRight {

moveForward()

if isBlocked && isBlockedRight {

turnLeft()

}

if isBlocked && isBlockedLeft {

turnRight()

}

if isBlocked && !isBlockedRight {

turnLeft()

}

while isOnGem {

collectGem()

}

while isOnClosedSwitch {

toggleSwitch()

}

}

while !isOnClosedSwitch {

...

while isOnClosedSwitch {

toggleSwitch()

}

}


Isn't going to get you very far 😁


This should work.


while !isBlocked || !isBlockedLeft || !isBlockedRight {

if isOnClosedSwitch {

toggleSwitch()

if !isBlockedRight && !isBlocked {

turnRight()

}

} else if !isBlocked {

moveForward()

if isOnGem {

collectGem()

}

} else if isBlocked && isBlockedLeft {

turnRight()

} else {

turnLeft()

}

}

while !isBlocked || !isBlockedLeft || !isBlockedRight {

if isOnClosedSwitch {

toggleSwitch()

} else if !isBlocked {

moveForward()

if isOnGem {

collectGem()

}

} else if isBlocked && isBlockedLeft {

turnRight()

} else {

turnLeft()

}

}

Add a Comment
I think all gays are so clever because my code may be so long:

func nevigateAroundWall() {
    if isBlocked && isBlockedLeft {
        turnRight()
    }else if isBlocked && isBlockedRight {
        turnLeft()
    }else if isBlockedRight || isBlockedLeft {
        moveForward()
    }else {
        turnLeft()
    }
}

while !isBlocked !isBlockedLeft !isBlockedRight{
    nevigateAroundWall()
    if isOnGem {
        collectGem()
    }else if isOnClosedSwitch {
        toggleSwitch()
    }
}
my code is

func nevigateAroundWall() {
if isBlocked && isBlockedRight{
turnLeft()
}else if isBlocked {
turnRight()
}else{
moveForward()
}
}
while !isOnOpenSwitch OR !isBlockedLeft OR isBlocked {
nevigateAroundWall()
if isOnClosedSwitch {
toogle Switch()
}else if isOnGem{
collectGem()
}
}
Post not yet marked as solved Up vote reply of e2rd Down vote reply of e2rd
func checkTile(){
    if isOnGem {
        collectGem()
    }
    if isOnClosedSwitch {
        toggleSwitch()
    }
}

func nav() {
    if isBlocked && isBlockedRight{
        turnLeft()
    }
    if isBlocked && isOnClosedSwitch {
        turnLeft()
    }
    if isBlocked {
        turnRight()
    }
    if isOnClosedSwitch && isBlocked {
        toggleSwitch()
    }
}

while !isOnOpenSwitch {
    nav()
    checkTile()
    moveForward()
}
I've solved it without using a switch as a condition of While :D

while !isBlocked || !isBlockedLeft && !isBlockedRight  {

    moveForward()

    if isBlocked && isOnGem{

        turnRight()

    } else if isBlockedRight && isBlocked{

        turnLeft()

    } else if isOnClosedSwitch && isBlocked{

        turnLeft()

        

        

    } else if isBlockedLeft && isBlocked{

        turnRight()

    }

    if isOnGem{

        collectGem()

    } else if isOnClosedSwitch{

        toggleSwitch()

    } 

    

    

}
  • This was beautiful. Thanks for sharing!

    PS: Maybe I shouldn´t drink whiskey and learn code at the same time?

Add a Comment

I think this is the answer. I've checked it already. it works!

while !isOnOpenSwitch {

    moveForward()

    if isBlocked && isBlockedRight {

        turnLeft()

    }

    if isBlocked && isBlockedLeft {

        turnRight()

    }

    if isBlocked && !isBlockedRight {

        turnLeft()

    }

    while isOnGem {

        collectGem()

    }

    while isOnClosedSwitch {

        toggleSwitch()

        moveForward()

    }

}

Try

func collectToggle() {

    if isOnGem {

        collectGem()

    } else if isOnClosedSwitch {

        toggleSwitch()

    }

}

while !isOnOpenSwitch {

    collectToggle()

    if isBlocked && isBlockedLeft {

        turnRight()

    } else if isBlocked && isBlockedRight {

        turnLeft()

    } else if isOnOpenSwitch && isBlocked {

        turnLeft()

    } 

    moveForward()

}

it's less confusing in my opinion

func doIt() {
  if isOnClosedSwitch {
    toggleSwitch()
  }
  if isOnGem {
    collectGem()
  }
}

func nav() {
  doIt()
  if isBlockedLeft && isBlocked {
    turnRight()
  }
  else if isBlocked && isBlockedRight {
    turnLeft()
  }
  else if isOnOpenSwitch && isBlocked {
    turnLeft()
  }
  moveForward()
}

while !isOnOpenSwitch{
  nav()
}

this worked for me. the else to handle the switches in this level isn't very reusable though

func navigateAroundWall() {

    if !isBlocked {

        moveForward()

    } else if isBlockedRight && isBlocked {

        turnLeft()

    } else if isBlockedLeft && isBlocked {

        turnRight()

    }

}

while !isOnOpenSwitch {

    navigateAroundWall()

    if isOnGem || isOnClosedSwitch {

        if isOnGem {

            collectGem()

        } else {

            toggleSwitch()

            turnLeft()

            moveForward()

            toggleSwitch()

            moveForward()

        }

    }

}