| Hi. Happily writing in arc, and trying to make my functions more functional, rather than imperative. The code below are the functions to move a creature through a square grid, and to keep him/her in this grid. The general mode of moving of the creature is (1) to go forward, (2) occasionally change direction, (3) not to move outside the grid, and (4) if it is at the edge to change its direction so that it moves away from the edge. Three of these functions are nice pretty one-liners, but the last one I couldn't think of a way to make it pretty, and that is where I request your help (but feel free to meddle with the other three as well, if you want to point out something interesting). (def newdir ()
`(,(- (rand 3) 1) ,(- (rand 3) 1)))
(def forward (creature)
(map + (creature 'pos) (creature 'dir)))
(def placewithinbounds (creature)
; sort of wrongly assuming the world has the same height as it has width
(map [min _ (- (len world) 1)] (map [max _ 0] (creature 'pos))))
(def directawayfrombounds (creature)
; don't know how to do this more elegant hmpf
(let newdir '(nil nil)
(if (and (is (car (creature 'pos)) 0) (is (car (creature 'dir)) -1)) (= (car newdir) 1))
(if (and (is (car (creature 'pos)) (- (len (world 0)) 1)) (is (car (creature 'dir)) 1)) (= (car newdir) -1))
(if (and (is (cadr (creature 'pos)) 0) (is (cadr (creature 'dir)) -1)) (= (cadr newdir) 1))
(if (and (is (cadr (creature 'pos)) (- (len (world 0)) 1)) (is (cadr (creature 'dir)) 1)) (= (cadr newdir) -1))
(if (no (car newdir)) (= (car newdir) (car (creature 'pos))))
(if (no (cadr newdir)) (= (cadr newdir) (cadr (creature 'pos))))
newdir))
|