Swift Playground Part 1: Commands

In this section we look at the basic commands we can use in Swift Playgrounds. There’s not that many but when combined together they allow us to do some amazing things. Let’s take a look.

Issuing commands

In our very first challenge we look at how we can make Byte move. We only have two options here. Move forward and collect the gem so go ahead and give this a go.

Issuing commands - Solution
moveForward()
moveForward()
moveForward()
collectGem()

Adding a new command

Here we add a new command called turn left. Add this command in and collect the gem!

Adding a new command - Solution
moveForward()
moveForward()
turnLeft()
moveForward()
moveForward()
collectGem()

Toggling a switch

Here we learn how to toggle or turn on a switch. Remember that when the light is on, it means that the switch is on. Also, if you toggle an on switch, you actually turn it off.

Toggling a switch - Solution
moveForward()
moveForward()
turnLeft()
moveForward()
collectGem()
moveForward()
turnLeft()
moveForward()
moveForward()
toggleSwitch()

Portal practise

This challenge introduces a portal which is something that allows Byte to teleport or magically appear at another location. Remember that you exit the portal facing the same direction as you enter.

Portal practise - Solution
moveForward()
moveForward()
moveForward()
turnLeft()
moveForward()
moveForward()
toggleSwitch()
moveForward()
moveForward()
turnLeft()
moveForward()
moveForward()
collectGem()

Finding and fixing bugs

In this challenge, you are given some code that has a bug. You have to find out what is wrong and fix it. Finding and fixing bugs is a very important skill to have because as a programmer, you’ll be forever reading not only your code but other people’s code.

You’ll want to not just run the code but to step through it, figure out where the bug or the wrong piece of code is and then fix it.

Finding and fixing bugs - Solution
moveForward()
moveForward()

turnLeft()

moveForward()
collectGem()
moveForward()
toggleSwitch()

Bug squash practise

Here is some more practise at finding bugs. Notice how a switch that is already on gets toggled again and becomes off. So we need to remove this bug and continue testing.

An short cut solution would be to ignore that switch altogether and just start going up the stairs. This is ok as well, but we didn’t do that here.

Bug squash practise - Solution
moveForward()
moveForward()
moveForward()
turnLeft()

moveForward()
moveForward()

turnLeft()
moveForward()
moveForward()
toggleSwitch()

turnLeft()
turnLeft()
turnLeft()

moveForward()
moveForward()
moveForward()
moveForward()

collectGem()
Bug squash practise - A short cut solution
moveForward()
turnLeft()
moveForward()
moveForward()
toggleSwitch()
moveForward()
moveForward()
moveForward()
moveForward()
collectGem()

The shortest route

In this challenge we have two portals we can use on our way to collecting the gem and toggling the switch. You get to choose how to do this because there are several paths you can take. Give it a try and if you get stuck, check the answers or the video below.

The shortest route - Solution
moveForward()
moveForward()
moveForward()
collectGem()

moveForward()
moveForward()

moveForward()
moveForward()
toggleSwitch()

See all solutions in the video below

Leave a Reply

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