extract drive
function which can go forward and backward
This commit is contained in:
parent
a4ac0dfe52
commit
13e283a2a5
19
rover.fnl
19
rover.fnl
@ -13,6 +13,13 @@
|
|||||||
:actual actual#
|
:actual actual#
|
||||||
})))))
|
})))))
|
||||||
|
|
||||||
|
(fn drive [{: x : y : direction} distance]
|
||||||
|
(match direction
|
||||||
|
:n {:y (+ y distance)}
|
||||||
|
:s {:y (- y distance)}
|
||||||
|
:w {:x (- x distance)}
|
||||||
|
:e {:x (+ x distance)}))
|
||||||
|
|
||||||
(fn turn-left [direction]
|
(fn turn-left [direction]
|
||||||
(-> {
|
(-> {
|
||||||
:n :w
|
:n :w
|
||||||
@ -34,16 +41,8 @@
|
|||||||
(fn command [rover string]
|
(fn command [rover string]
|
||||||
(merge rover
|
(merge rover
|
||||||
(match string
|
(match string
|
||||||
:f (match rover.direction
|
:f (drive rover 1)
|
||||||
:n {:y (+ rover.y 1)}
|
:b (drive rover -1)
|
||||||
:s {:y (- rover.y 1)}
|
|
||||||
:w {:x (- rover.x 1)}
|
|
||||||
:e {:x (+ rover.x 1)})
|
|
||||||
:b (match rover.direction
|
|
||||||
:n {:y (- rover.y 1)}
|
|
||||||
:s {:y (+ rover.y 1)}
|
|
||||||
:w {:x (+ rover.x 1)}
|
|
||||||
:e {:x (- rover.x 1)})
|
|
||||||
:r {:direction (turn-right rover.direction)}
|
:r {:direction (turn-right rover.direction)}
|
||||||
:l {:direction (turn-left rover.direction)}
|
:l {:direction (turn-left rover.direction)}
|
||||||
_ (assert false (. "unrecognised command " string)))))
|
_ (assert false (. "unrecognised command " string)))))
|
||||||
|
Loading…
Reference in New Issue
Block a user