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

“Everything I know about business in one minute.”

by William Drenttel Focusing on making a partnership work is more profitable than focusing on making money. Love your employees more than you love your clients.  The best new business is your current business. Price projects by... Continue →