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