Swift — Twin Peaks

Objective: Character collects all gems until count is equal to randomized total, while expert moves platform as needed.
FCD9951B-4132-49B9-BF76-01BB932ADC84.png

Not only was this a very challenging level, utilizing all skills practiced until now, it also tested some erroneous assumptions I falsely introduced.

At first, I assumed that gems may also appear along the entire length of the platform, which they did not; gems only ever appeared on the ends.

Secondly, I assumed platform must be raised to an even level with each row, which it does not; only must get within jumping level. (Thanks, Samantha!)

Difference between mine and hers? 47 lines vs 31 — mine has 50% more!!

My solution

let expert = Expert()
let character = Character()
var gemCount = 0
world.place (expert, facing: north, atColumn:1, row:3)
world.place (character, facing: north, atColumn:5, row:0)
func rowRun() {
for i in 1…6 {
if character.isOnGem {
gemCount += 1
character.collectGem()
}
character.jump()
}
}
func turnLock(up:Bool, turns: Int) {
if up == true {
for i in 1…turns {
expert.turnLockUp()
}
} else {
for i in 1…turns {
expert.turnLockDown()
}
}
expert.turnRight()
}
func leftFace() {
character.turnLeft()
character.moveForward()
character.turnLeft()
}
func rightFace() {
character.turnRight()
character.moveForward()
character.turnRight()
}
expert.moveForward()
rowRun()
turnLock(up:true, turns:3)
leftFace()
rowRun()
rightFace()
rowRun()
turnLock(up:false, turns:3)
rightFace()
rowRun()
leftFace()

Samantha’s solution

let expert = Expert()
let character = Character()
var gemCount = 0
func moveLong() {
for i in 1…6 {
if character.isOnGem {
character.collectGem()
gemCount += 1
}
character.jump()
}
}
func moveShort() {
for i in 1…2 {
if character.isOnGem {
character.collectGem()
gemCount += 1
}
character.jump()
}
}
world.place (expert, facing: north, atColumn:1, row:4)
world.place (character, facing: north, atColumn:3, row:0)
expert.turnLock(up:true, numberOfTimes:2)
while gemCount < totalGems {
moveLong()
character.turnRight()
moveShort()
character.turnRight()
}

 
13
Kudos
 
13
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 →