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

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