Swift — Rivers to Cross

Objective: Place monster on a specific square and facing a specific direction, and then complete the maze by collecting all gems.
1BBC9703-A4DE-4DFC-9DB4-48954FABB4A8.png

I didn’t much issues with this one, and so I challenged myself to recreate entirely new algorithm using less code — did it with 25% less lines!

Take 1

let expert = Expert()
world.place (expert, facing: .south, atColumn:1, row:8)
func gemGoLock() {
for i in 1…4 {
expert.collectGem()
expert.moveForward()
}
}
gemGoLock()
expert.turnLockDown()
expert.turnLeft()
gemGoLock()
expert.turnLockUp()
expert.turnRight()
gemGoLock()
expert.collectGem()

Take 2

let expert = Expert()
world.place (expert, facing: .south, atColumn:1, row:8)
for i in 1…12 {
expert.collectGem()
expert.moveForward()
if expert.isBlocked && expert.isBlockedRight {
expert.turnLockDown()
expert.turnLeft()
}
if expert.isBlocked && expert.isBlockedLeft {
expert.turnLockUp()
expert.turnRight()

 
4
Kudos
 
4
Kudos

Now read this

Swift — Twin Peaks

Objective: Character collects all gems until count is equal to randomized total, while expert moves platform as needed. Not only was this a very challenging level, utilizing all skills practiced until now, it also tested some erroneous... Continue →