Swift — It Takes Two

This was a challenging level which initially stumped me when I tried to puzzle it out in my head before writing a line of code.

To overcome this, I resigned to first just write out every single action line by line and then reviewed my code to look for patterns — which I then turned in to functions. My code came out to 49 lines (below).

I then searched for another solution that may be more efficient.

This one was built in 33 lines — and reminded me to utilize for i in 1…X with a puzzle like this one that has a known area to navigate.

Objective: Create two monsters; one to turn locks for moving platforms up or down, and the other to do actions on each — get gem or toggle switch.
0390972D-55FF-4068-B83F-AF3F33725EC0.png

let expert = Expert()
let character = Character()
var itemCount = 0
func gemSwitch() {
character.moveForward()
character.moveForward()
if character.isOnGem {
itemCount += 1
character.collectGem()
}
if character.isOnClosedSwitch {
itemCount += 1
character.toggleSwitch()
}
}
func lockTurn() {
expert.turnLeft()
if itemCount == 0 {
expert.turnLockDown()
expert.turnLockDown()
}
if itemCount == 1 {
expert.turnLockUp()
}
gemSwitch()
}
func rightMove() {
expert.turnRight()
expert.moveForward()
expert.moveForward()
}
func moveFour() {
expert.moveForward()
expert.moveForward()
expert.moveForward()
expert.moveForward()
}
expert.turnLeft()
while itemCount <= 1 {
moveFour()
rightMove()
lockTurn()
rightMove()
rightMove()
moveFour()
rightMove()
lockTurn()
}

 
26
Kudos
 
26
Kudos

Now read this

Swift — Corners of the World

This was the first level to truly stump me. Since I couldn’t come up with a solution myself, I turned to others. Below are two that helped me out. The lesson I took away from this is that it’s not just the item needed to do an action to... Continue →