Swift Playground Part 3: For Loops

In this section we look at how we can repeat the same commands using a short cut called looping, in particular, for loops.

Using Loops

In this challenge we need to collect four gems that are all in the same position. The secret is to solve for one row, and then rinse and repeat four times.

Using loops - Solution
for i in  1 ... 5 {
    moveForward()
    moveForward()
    collectGem()
    moveForward()
}

Looping all the sides

Similar to the previous challenge, this challenge doesn’t use portals but uses a corner so you’ll have to figure out which way you need to turn. Again, solve for one side and then repeat.

Looping all the sides - Solution
for i in 1 ... 4 {
    moveForward()
    collectGem()
    moveForward()
    moveForward()
    moveForward()
    turnRight()
}

To the edge and back

Here you get more chance to practise with for loops where you need to toggle a switch and then return back to the centre. Returning back is important because it is part of the repeatable pattern. That’s your hint, so now, give it a try yourself.

To the edge and back - Solution 1
for i in 1 ... 4 {
    moveForward()
    moveForward()
    toggleSwitch()
    turnLeft()
    turnLeft()
    moveForward()
    moveForward()
    turnLeft()
}

To the edge and back - Solution using functions
func turnAround() {
    turnLeft()
    turnLeft()
}

func toggleAndReturn() {
    moveForward()
    moveForward()
    toggleSwitch()
    turnAround()
    moveForward()
    moveForward()
}

for i in 1 ... 4 {
    toggleAndReturn()
    turnLeft()
}

Loop Jumper

This challenge combines all the commands such as moving forward, turning corners, collecting gems as well as jumping through portals. The concepts remain the same though. Look for a pattern, test it, and then repeat it. Good luck!

Loop jumper - Solution
for i in 1 ... 5 {
    moveForward()
    turnLeft()
    moveForward()
    moveForward()
    collectGem()
    turnRight()
}

Branch out

This challenge can be solved in a number of ways but we want to make use of functions we learnt in the previous section. Use functions to break down specific tasks, such as turn around, and then you can also use a loop within a loop.

Branch out - Solution
func turnAround() {
    turnLeft()
    turnLeft()
}

for i in 1 ... 3 {
    moveForward()
    moveForward()
    turnRight()
    for i in 1 ... 7 {
        moveForward()
    }
    toggleSwitch()
    turnAround()
    for i in 1 ... 7 {
        moveForward()
    }
    turnRight()
    
}

Gem farm

This challenge may seem difficult at first but once you see the pattern, it becomes super simple. Instead of collecting all the gems first and then toggling all the switches, try to collect the gems and toggle the switches for each row. If you get stuck, see the answers below.

Gem farm - Solution
func turnAround() {
    turnLeft()
    turnLeft()
}

func backToCentre() {
    moveForward()
    moveForward()
}
for i in 1 ... 3 {
    
    turnLeft()
    moveForward()
    toggleSwitch()
    moveForward()
    toggleSwitch()
    turnAround()
    backToCentre()
    
    moveForward()
    collectGem()
    moveForward()
    collectGem()
    turnAround()
    backToCentre()
    turnRight()
    moveForward()
}

Four stash sweep*

We continue to practise finding patterns, creating functions and looping. Start by finding a pattern and then solving for this pattern first. Then try and apply it to the other patterns.

Four stash sweep - Solution
func turnAround() {
    turnLeft()
    turnLeft()
}
for i in 1 ... 4 {
    
    moveForward()
    collectGem()
    turnLeft()
    moveForward()
    collectGem()
    turnAround()
    
    moveForward()
    turnLeft()
    moveForward()
    collectGem()
    turnAround()
    moveForward()
    turnLeft()
    moveForward()
    collectGem()
    moveForward()
    
}

See all solutions in the video below

Leave a Reply

Your email address will not be published. Required fields are marked *