Learn To Code Solutions
Learn To Code Solutions
Solution Guide
Solution 1 Solution 2
Solution 1 Solution 2
Toggling a Switch
Solution 1 Solution 2
Portal Practice
Solution 1 Solution 2
Solution 1
moveForward()
moveForward()
turnLeft()
moveForward()
collectGem()
moveForward()
toggleSwitch()
Solution 1 Solution 2
moveForward() moveForward()
turnLeft() turnLeft()
moveForward() for step in 1…2 {
moveForward() moveForward()
toggleSwitch() }
moveForward() toggleSwitch()
moveForward() for step in 1…4 {
moveForward() moveForward()
moveForward() }
collectGem() collectGem()
Solution 1 Solution 2
Functions
Composing a New Behavior
Solution 1 Solution 2
Solution 1 Solution 2
Solution 1
func pickPlace() {
moveForward()
collectGem()
moveForward()
toggleSwitch()
moveForward()
}
pickPlace()
turnLeft()
pickPlace()
moveForward()
turnLeft()
pickPlace()
turnLeft()
pickPlace()
Functions (Continued)
Across the Board
Solution 1 Solution 2
Nesting Patterns
Solution 1 Solution 2
Solution 1
func collectGemTurnAround() {
moveForward()
moveForward()
collectGem()
turnLeft()
turnLeft()
moveForward()
moveForward()
}
func solveRow() {
collectGemTurnAround()
collectGemTurnAround()
}
solveRow()
turnRight()
moveForward()
turnLeft()
solveRow()
turnRight()
moveForward()
turnLeft()
solveRow()
Treasure Hunt
Solution 1
func moveThenToggle() {
moveForward()
moveForward()
toggleSwitch()
}
func toggleThenReturn() {
moveThenToggle()
turnLeft()
turnLeft()
moveForward()
moveForward()
}
toggleThenReturn()
toggleThenReturn()
turnRight()
moveThenToggle()
toggleThenReturn()
moveForward()
moveForward()
moveThenToggle()
moveThenToggle()
For Loops
Using Loops
Solution 1 Solution 2
Solution 1 Solution 2
Solution 1
for i in 1...4 {
moveForward()
moveForward()
toggleSwitch()
turnRight()
turnRight()
moveForward()
moveForward()
turnLeft()
}
For Loops (Continued)
Loop Jumper
Solution 1
for i in 1...5 {
moveForward()
turnLeft()
moveForward()
moveForward()
collectGem()
turnRight()
}
Branch Out
Solution 1
func traverseStairway() {
for i in 1...7 {
moveForward()
}
}
func clearStairway() {
traverseStairway()
toggleSwitch()
turnRight()
turnRight()
traverseStairway()
turnRight()
}
for i in 1...3 {
moveForward()
moveForward()
turnRight()
clearStairway()
}
For Loops (Continued)
Gem Farm
Solution 1
func turnAround() {
turnLeft()
turnLeft()
moveForward()
moveForward()
}
func solveRow() {
turnRight()
moveForward()
collectGem()
moveForward()
collectGem()
turnAround()
moveForward()
toggleSwitch()
moveForward()
toggleSwitch()
turnAround()
turnLeft()
moveForward()
}
for i in 1...3 {
solveRow()
}
Solution 1
func turnAround() {
turnRight()
turnRight()
}
func collectFour() {
collectGem()
moveForward()
collectGem()
turnAround()
moveForward()
turnRight()
moveForward()
collectGem()
turnAround()
moveForward()
moveForward()
collectGem()
}
moveForward()
for i in 1...3 {
collectFour()
moveForward()
moveForward()
}
collectFour()
Conditional Code
Checking for Switches
Solution 1 Solution 2
Using else if
Solution 1
moveForward()
if isOnClosedSwitch {}
toggleSwitch()
} else if isOnGem {
collectGem()
}
moveForward()
if isOnClosedSwitch {
toggleSwitch()
} else if isOnGem {
collectGem()
}
Solution 1
for i in 1...12 {
moveForward()
if isOnClosedSwitch {
toggleSwitch()
} else if isOnGem {
collectGem()
}
}
Conditional Code (Continued)
Conditional Climb
Solution 1
for i in 1...16 {
if isOnGem {
collectGem()
turnLeft()
} else {
moveForward()
}
}
Solution 1
func collectOrToggle() {
moveForward()
moveForward()
if isOnGem {
collectGem()
} else if {
isOnClosedSwitch {
toggleSwitch()
}
}
collectOrToggle()
collectOrToggle()
turnLeft()
moveForward()
moveForward()
turnLeft()
collectOrToggle()
collectOrToggle()
turnRight()
moveForward()
turnRight()
collectOrToggle()
collectOrToggle()
Conditional Code (Continued)
Boxed In
Solution 1
func checkSquare() {
if isOnGem {
collectGem()
} else if isOnClosedSwitch {
toggleSwitch()
}
}
func completeCorner() {
checkSquare()
moveForward()
checkSquare()
turnRight()
moveForward()
}
moveForward()
turnRight()
for i in 1...4 {
completeCorner()
}
Decision Tree
Solution 1
func solveRightSide() {
collectGem()
turnRight()
moveForward()
moveForward()
moveForward()
turnLeft()
moveForward()
collectGem()
turnLeft()
turnLeft()
moveForward()
turnRight()
moveForward()
moveForward()
moveForward()
turnRight()
}
for i in 1...5 {
moveForward()
if isOnGem {
solveRightSide()
} else if isOnClosedSwitch {
toggleSwitch()
turnLeft()
moveForward()
collectGem()
turnLeft()
turnLeft()
moveForward()
turnLeft()
}
}
Logical Operators
Using the NOT Operator
Solution 1
for i in 1...4 {
moveForward()
if !isOnGem {
turnLeft()
moveForward()
moveForward()
collectGem()
turnLeft()
turnLeft()
moveForward()
moveForward()
turnLeft()
} else {
collectGem()
}
}
Spiral of NOT
Solution 1
for i in 1...16 {
if !isBlocked {
moveForward()
} else {
turnLeft()
}
}
toggleSwitch()
Solution 1
for i in 1 ... 7 {
moveForward()
if isOnGem && isBlockedLeft {
collectGem()
turnRight()
moveForward()
moveForward()
toggleSwitch()
turnLeft()
turnLeft()
moveForward()
moveForward()
turnRight()
} else if isOnGem {
collectGem()
}
}
Logical Operators (Continued)
Checking This OR That
Solution 1
for i in 1...12 {
if isBlocked || isBlockedLeft {
turnRight()
moveForward()
} else {
moveForward()
}
}
collectGem()
Logical Labyrinth
Solution 1
for i in 1 ... 8 {
moveForward()
if isOnGem && isOnClosedSwitch {
turnRight()
moveForward()
moveForward()
collectGem()
turnLeft()
turnLeft()
moveForward()
moveForward()
turnRight()
collectGem()
toggleSwitch()
} else if isOnClosedSwitch {
turnLeft()
toggleSwitch()
}
if isOnGem {
collectGem()
}
}
While Loops
Running Code While...
Solution 1 Solution 2
Solution 1
while !isBlocked {
if isOnClosedSwitch {
toggleSwitch()
}
moveForward()
}
Solution 1
func turnAndCollectGem() {
moveForward()
turnLeft()
moveForward()
collectGem()
turnRight()
}
while !isBlocked {
turnAndCollectGem()
}
Four by Four
Solution 1 Solution 2
Solution 1
moveForward()
while isOnGem {
turnLeft()
collectGem()
moveForward()
collectGem()
turnLeft()
moveForward()
turnRight()
moveForward()
}
Land of Bounty
Solution 1 Solution 2
Nesting Loops
Solution 1
while !isBlocked {
while !isOnGem {
moveForward()
}
collectGem()
turnLeft()
}
While Loops (Continued)
Random Rectangles
Solution 1
while !isBlocked {
while !isBlocked {
moveForward()
}
turnRight()
}
toggleSwitch()
Solution 1
while !isOnGem {
while !isBlocked {
moveForward()
if isOnClosedSwitch {
toggleSwitch()
}
}
turnRight()
}
collectGem()
Algorithms
The Right-Hand Rule
Solution 1
func navigateAroundWall() {
if isBlockedRight {
moveForward()
} else {
turnRight()
moveForward()
}
}
while !isOnClosedSwitch {
navigateAroundWall()
if isOnGem {
collectGem()
turnLeft()
turnLeft()
}
}
toggleSwitch()
Algorithms (Continued)
Adjusting Your Algorithm
Solution 1
func navigateAroundWall() {
if isBlockedRight && isBlocked {
turnLeft()
} else if isBlockedRight {
moveForward()
} else {
turnRight()
moveForward()
}
}
while !isOnClosedSwitch {
navigateAroundWall()
if isOnGem {
collectGem()
}
}
toggleSwitch()
Conquering a Maze
Solution 1
func navigateAroundWall() {
if isBlockedRight && isBlocked {
turnLeft()
} else if isBlockedRight {
moveForward()
} else {
turnRight()
moveForward()
}
}
while !isOnGem {
navigateAroundWall()
}
collectGem()
Solution 1
while !isOnGem {
while !isOnClosedSwitch && !isOnGem {
moveForward()
}
if isOnClosedSwitch && isBlocked {
toggleSwitch()
turnLeft()
} else if isOnClosedSwitch {
toggleSwitch()
turnRight()
}
}
collectGem()
Algorithms (Continued)
Roll Right, Roll Left
Solution 1
while !isOnOpenSwitch {
moveForward()
if isOnGem {
collectGem()
turnRight()
moveForward()
collectGem()
} else if isOnClosedSwitch {
toggleSwitch()
turnLeft()
moveForward()
toggleSwitch()
}
while !isBlocked {
moveForward()
}
if !isBlockedRight {
turnRight()
} else {
turnLeft()
}
}
Variables
Keeping Track
Solution 1
var gemCounter = 0
moveForward()
moveForward()
collectGem()
gemCounter = 1
Solution 1 Solution 2
Solution 1
var gemCounter = 0
while !isBlocked {
while !isBlocked {
if isOnGem {
collectGem()
gemCounter = gemCounter + 1
}
moveForward()
}
turnRight()
}
Solution 1
var gemCounter = 0
while gemCounter < 7 {
if isOnGem {
collectGem()
gemCounter = gemCounter + 1
}
if isBlocked {
turnRight()
turnRight()
}
moveForward()
}
Solution 1
var gemCounter = 0
var switchCounter = 0
Solution 1
var gemCounter = 0
Solution 1
var gemCounter = 0
var switchCounter = 0
while !isOnClosedSwitch {
while !isBlocked {
if isOnGem {
collectGem()
gemCounter = gemCounter + 1
}
moveForward()
}
turnRight()
}
Solution 1
Types
Deactivating a Portal
Solution 1
greenPortal.isActive = false
func moveThree() {
moveForward()
moveForward()
moveForward()
}
for i in 1...3 {
moveThree()
turnRight()
moveThree()
toggleSwitch()
turnLeft()
turnLeft()
}
Types (Continued)
Portal On and Off
Solution 1
func moveAndCollect() {
while !isBlocked {
moveForward()
if isOnGem {
collectGem()
}
}
}
func turnAround() {
turnLeft()
turnLeft()
}
moveAndCollect()
turnAround()
purplePortal.isActive = false
while !isBlocked {
moveForward()
}
toggleSwitch()
turnAround()
purplePortal.isActive = true
moveAndCollect()
Solution 1
func moveCollect() {
moveForward()
collectGem()
}
func turnAround() {
turnLeft()
turnLeft()
}
moveForward()
moveCollect()
turnAround()
bluePortal.isActive = false
moveForward()
moveCollect()
turnAround()
bluePortal.isActive = true
pinkPortal.isActive = false
moveForward()
moveForward()
moveForward()
collectGem()
turnAround()
pinkPortal.isActive = true
moveForward()
turnAround()
moveCollect()
Types (Continued)
Corners of the World
Solution 1
func turnAround() {
turnLeft()
turnLeft()
}
func checkSquare() {
if isOnGem {
collectGem()
} else if isOnClosedSwitch {
toggleSwitch()
}
}
func collectOrToggle() {
moveForward()
checkSquare()
turnAround()
}
func collectOrToggleThenTurnRight() {
collectOrToggle()
moveForward()
turnRight()
}
func collectOrToggleThenTurnLeft() {
collectOrToggle()
moveForward()
turnLeft()
}
turnLeft()
moveForward()
moveForward()
greenPortal.isActive = false
for i in 1...3 {
collectOrToggleThenTurnRight()
}
collectOrToggle()
greenPortal.isActive = true
moveForward()
greenPortal.isActive = false
collectOrToggleThenTurnLeft()
collectOrToggleThenTurnLeft()
moveForward()
moveForward()
orangePortal.isActive = false
moveForward()
for i in 1...3 {
collectOrToggleThenTurnRight()
}
collectOrToggle()
orangePortal.isActive = true
moveForward()
orangePortal.isActive = false
turnLeft()
collectOrToggleThenTurnRight()
collectOrToggle()
Types (Continued)
Random Gems Everywhere
Solution 1
bluePortal.isActive = false
pinkPortal.isActive = false
while gemCounter < totalGems {
if isOnGem {
collectGem()
gemCounter = gemCounter + 1
}
moveForward()
if isBlocked {
turnLeft()
turnLeft()
if bluePortal.isActive == true {
bluePortal.isActive = false
pinkPortal.isActive = false
} else if bluePortal.isActive ==
false {
bluePortal.isActive = true
pinkPortal.isActive = true
}
}
}
Initalization
Initializing Your Expert
Solution 1
func solveSide() {
expert.moveForward()
expert.moveForward()
expert.moveForward()
if expert.isOnGem {
expert.collectGem()
} else {
expert.turnLockUp()
}
}
func returnToCenter() {
expert.turnLeft()
expert.turnLeft()
expert.moveForward()
expert.moveForward()
expert.moveForward()
expert.turnRight()
}
for i in 1...3 {
solveSide()
returnToCenter()
}
solveSide()
Initalization (Continued)
Train Your Expert
Solution 1
func turnAround() {
expert.turnLeft()
expert.turnLeft()
expert.moveForward()
expert.moveForward()
}
func completeSide() {
expert.moveForward()
expert.moveForward()
expert.collectGem()
}
for i in 1 ... 2 {
completeSide()
turnAround()
expert.turnRight()
}
completeSide()
expert.turnLockDown()
turnAround()
expert.turnRight()
for i in 1 ... 3 {
expert.moveForward()
}
expert.turnLeft()
for i in 1 ... 3 {
completeSide()
turnAround()
expert.turnLeft()
}
Solution 1
expert.moveForward()
expert.turnLockUp()
character.moveForward()
character.collectGem()
character.moveForward()
character.turnRight()
character.moveForward()
character.moveForward()
expert.turnLockDown()
expert.turnLockDown()
character.moveForward()
character.collectGem()
Initalization (Continued)
It Takes Two
Solution 1
func turnCorner() {
expert.moveForward()
expert.moveForward()
expert.turnRight()
expert.moveForward()
expert.moveForward()
}
expert.turnLeft()
expert.moveForward()
turnCorner()
expert.turnLeft()
expert.turnLockDown()
expert.turnLockDown()
character.moveForward()
character.moveForward()
character.collectGem()
expert.turnRight()
turnCorner()
expert.moveForward()
expert.moveForward()
turnCorner()
expert.turnLeft()
expert.turnLockUp()
character.moveForward()
character.moveForward()
character.toggleSwitch()
Parameters
Moving Further Forward
Solution 1 Solution 2
Solution 1
func turnAround() {
character.turnLeft()
character.turnLeft()
}
func collectGemTurnAround() {
character.moveForward()
character.moveForward()
character.collectGem()
turnAround()
character.moveForward()
character.moveForward()
character.turnRight()
}
for i in 1...4 {
expert.turnLock(up: true,
numberOfTimes: 4)
expert.turnRight()
}
for i in 1...3 {
while !character.isOnGem {
character.moveForward()
}
character.collectGem()
character.turnRight()
}
character.moveForward()
for i in 1...4 {
expert.turnLock(up: false,
numberOfTimes: 3)
expert.turnRight()
}
character.turnLeft()
character.moveForward()
character.collectGem()
turnAround()
for i in 1...3 {
character.moveForward()
character.moveForward()
if !character.isOnGem {
character.turnRight()
collectGemTurnAround()
} else {
character.collectGem()
}
}
Parameters (continued)
Placing at a Specific Location
Solution 1 Solution 2
func turnLockCollectGem() {
expert.turnLeft()
expert.turnLockUp()
turnAround()
expert.moveForward()
expert.collectGem()
turnAround()
expert.moveForward()
expert.turnRight()
}
turnLockCollectGem()
expert.move(distance: 5)
turnLockCollectGem()
expert.move(distance: 6)
expert.collectGem()
Rivers to Cross
Solution 1
func collectGemsInLine() {
while !expert.isBlocked {
if expert.isOnGem {
expert.collectGem()
}
expert.moveForward()
}
}
collectGemsInLine()
expert.turnLockDown()
expert.turnLeft()
collectGemsInLine()
expert.turnLockUp()
expert.turnRight()
collectGemsInLine()
Parameters (continued)
Placing Two Characters
Solution 1
func collectAndJump() {
for i in 1 ... 2 {
character.collectGem()
character.jump()
character.jump()
}
}
expert.toggleSwitch()
expert.turnLockUp()
collectAndJump()
character.turnRight()
collectAndJump()
character.turnLeft()
character.collectGem()
character.move(distance: 2)
character.collectGem()
Two Experts
Solution 1
bottomExpert.collectGem()
bottomExpert.move(distance: 3)
bottomExpert.turnLeft()
bottomExpert.turnLock(up: true, numberOfTimes: 2)
bottomExpert.turnRight()
topExpert.turnLockDown()
bottomExpert.move(distance: 3)
bottomExpert.turnLock(up: false, numberOfTimes: 2)
topExpert.turnRight()
while !topExpert.isBlocked {
if topExpert.isOnGem {
topExpert.collectGem()
}
topExpert.moveForward()
}
Parameters (continued)
Twin Peaks
Solution 1
var gemCounter = 0
var platformPosition = 0
func jumpAcrossSide() {
for i in 1...6 {
if character.isOnGem && gemCounter < totalGems {
character.collectGem()
gemCounter = gemCounter + 1
}
character.jump()
}
}
Solution 1
while !isOnClosedSwitch {
moveForward()
if isBlocked {
turnLeft()
if isBlocked {
turnRight()
turnRight()
}
}
}
toggleSwitch()
Solution 1
func crossBridge() {
turnRight()
move(distance: 4)
collectGem()
turnLeft()
turnLeft()
move(distance: 4)
turnRight()
}
for i in 1...3 {
move(distance: 2)
toggleSwitch()
crossBridge()
}
World Building (continued)
Making Your Own Portals
Solution 1
var gemCounter = 0
while gemCounter < 8 {
moveForward()
if gemCounter == 4 {
turnLeft()
turnLeft()
} else {
turnLeft()
}
moveForward()
collectGem()
gemCounter = gemCounter + 1
turnLeft()
turnLeft()
}
World Building (continued)
Reach for the Stairs
Solution 1
func toggleSide() {
toggleSwitch()
while !isBlocked {
moveForward()
toggleSwitch()
}
}
func turnCorner() {
turnRight()
move(distance: 2)
turnLeft()
move(distance: 2)
turnRight()
}
move(distance: 4)
turnLeft()
move(distance: 3)
turnRight()
for i in 1 ... 2 {
toggleSide()
turnCorner()
}
toggleSide()
World Building (continued)
Floating Islands
Solution 1
func completeIsland() {
character.toggleSwitch()
character.jump()
character.collectGem()
character.turnLeft()
character.jump()
character.toggleSwitch()
}
completeIsland()
world.place(character, facing: north, atColumn: 6, row: 3)
completeIsland()
world.place(character, facing: east, atColumn: 1, row: 1)
completeIsland()
Build a Loop
Solution 1
Solution 1
Arrays
Storing Information
Solution 1
Iteration Exploration
Solution 1
Stacking Blocks
Solution 1
let blockLocations = [
Coordinate(column: 0, row: 0),
Coordinate(column: 3, row: 3),
Coordinate(column: 0, row: 3),
Coordinate(column: 3, row: 0)
]
for coordinate in blockLocations {
for i in 1 ... 5 {
world.place(Block(), at: coordinate)
}
}
Arrays (continued)
Getting in Order
Solution 1
var rowPlacement = 0
for character in characters {
world.place(character, at: Coordinate(column: 1, row: rowPlacement))
rowPlacement += 1
}
Appending to an Array
Solution 1
Solution 1
Solution 1
for i in 1...12 {
for coordinate in row2 {
world.place(Block(), at: coordinate)
}
// Remove a coordinate and append it to your empty array
discardedCoordinates.append(row2.remove(at: 0))
}
Solution 1
var columnPlacement = 0
for blu in teamBlu {
world.place(blu, at: Coordinate(column: columnPlacement, row: 4))
columnPlacement += 1
}
Generate a Landscape
Solution 1
var index = 0
for coordinate in allCoordinates {
if index == heights.count {
index = 0
}
for i in 0...heights[index] {
world.place(Block(), at: coordinate)
}
index += 1
}
Arrays (continued)
Randomized Lands
Solution 1
var index = 0
for coordinate in allCoordinates {
if index == heights.count {
index = 0
}
if currentHeight == 0 {
// Do something interesting if currentHeight is equal to 0.
world.removeItems(at: coordinate)
} else {
for i in 1...currentHeight {
world.place(Block(), at: coordinate)
}
if currentHeight > 5 {
// Do something different, such as placing a character.
world.place(Character(), at: coordinate)
}
// Add more rules to customize your world.
}
index += 1
}
Arrays (continued)
Another Way to Create an Array
Solution 1
for i in 0...height {
world.place(Block(), at: coordinate)
}
Solution 1
// Do cool stuff.
squareLock.movePlatforms(up: true, numberOfTimes: 3)
Solution 1