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

Accept Losing Your Best

Great employees are always being lost, so prepare from the start. The best candidates continue to grow and learn, pushing themselves and their boundaries. They’re always striving to improve; to move forward; to move upward. As a result,... Continue →