Created
December 5, 2010 15:24
-
-
Save jamesnvc/729167 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(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