Skip to content

Instantly share code, notes, and snippets.

@jamesnvc
Created December 5, 2010 15:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jamesnvc/729167 to your computer and use it in GitHub Desktop.
Save jamesnvc/729167 to your computer and use it in GitHub Desktop.
(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