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()
}

 
28
Kudos
 
28
Kudos

Now read this

Swift — Crank Up and Down

Objective: Initialize two monsters, then have one collect all gems with the other moving platforms up and down as needed. My assumption was to start with top left gems and then move down to lower level before finishing with far right top... Continue →