implement rotate right
This commit is contained in:
parent
f8a58c4a68
commit
78484b1115
32
rover.fnl
32
rover.fnl
@ -13,15 +13,31 @@
|
|||||||
:actual actual#
|
:actual actual#
|
||||||
})))))
|
})))))
|
||||||
|
|
||||||
(fn command [rover command]
|
(fn rotate [direction rotation]
|
||||||
(match rover.direction
|
(let [chooser
|
||||||
|
(match rotation
|
||||||
|
:r {
|
||||||
|
:n :e
|
||||||
|
:e :s
|
||||||
|
:s :w
|
||||||
|
:w :n
|
||||||
|
}
|
||||||
|
)]
|
||||||
|
(. chooser direction)))
|
||||||
|
|
||||||
|
|
||||||
|
(fn command [rover string]
|
||||||
|
(match string
|
||||||
|
:f (match rover.direction
|
||||||
:n (merge rover {:y (+ rover.y 1)})
|
:n (merge rover {:y (+ rover.y 1)})
|
||||||
:s (merge rover {:y (- rover.y 1)})
|
:s (merge rover {:y (- rover.y 1)})
|
||||||
:w (merge rover {:x (- rover.x 1)})
|
:w (merge rover {:x (- rover.x 1)})
|
||||||
:e (merge rover {:x (+ rover.x 1)})))
|
:e (merge rover {:x (+ rover.x 1)}))
|
||||||
|
:r (merge rover {:direction (rotate rover.direction :r)})
|
||||||
|
_ (assert false (. "unrecognised command " string))))
|
||||||
|
|
||||||
(fn execute [rover [cmd & cmds]]
|
(fn execute [rover [cmd & cmds]]
|
||||||
(let [r1 (command rover)]
|
(let [r1 (command rover cmd)]
|
||||||
(if (next cmds)
|
(if (next cmds)
|
||||||
(r1:execute cmds)
|
(r1:execute cmds)
|
||||||
r1)))
|
r1)))
|
||||||
@ -65,5 +81,13 @@
|
|||||||
(r:execute [:f :f :f])
|
(r:execute [:f :f :f])
|
||||||
{:x 1 :y -2 :direction :s}))
|
{:x 1 :y -2 :direction :s}))
|
||||||
|
|
||||||
|
(let [r (rover 2 4 :e)]
|
||||||
|
(expect "Rotates to south when pointing east and asked to turn right"
|
||||||
|
(r:execute [:r]) {:x 2 :y 4 :direction :s}))
|
||||||
|
|
||||||
|
(let [r (rover 2 4 :w)]
|
||||||
|
(expect "Rotates to north when pointing west and asked to turn right"
|
||||||
|
(r:execute [:r]) {:x 2 :y 4 :direction :n}))
|
||||||
|
|
||||||
|
|
||||||
(print "OK")
|
(print "OK")
|
||||||
|
Loading…
Reference in New Issue
Block a user