Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
(defn agent-die [state]
(println (str (:id state) ": Alas! I am slain!"))
(remove-watch *agent* :stepper)
(swap! remaining-agents dec)
(assoc state :alive false))
(defn agent-fn [state]
(let [new-x (mod ((rand-dir) (:x state)) len-x)
new-y (mod ((rand-dir) (:y state)) len-y)
next-cell (at-loc new-x new-y)]
(if-let [foe (deref next-cell)]
(do
(send foe agent-die)
(println "There can only be one!")
(println (str (:id state) " killed " (:id @foe)
" at " new-x ", " new-y))))
(if (:alive state)
(do
(reset! next-cell *agent*)
(println (str (:id state) " moving to " new-x " " new-y))
(assoc state :x new-x :y new-y))
(remove-watch *agent* :stepper))))
(defn create-agent [id x y]
(let [new-agent (agent {:id id :x x :y y :alive true})]
(add-watch new-agent :stepper
(fn [_ agt _ _]
(Thread/sleep (rand-int 3000))
(send agt agent-fn)))
new-agent))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.