Skip to content

Instantly share code, notes, and snippets.

Created July 16, 2011 19:46
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 anonymous/1086690 to your computer and use it in GitHub Desktop.
Save anonymous/1086690 to your computer and use it in GitHub Desktop.
(ns robo-replacement.map-maker)
(defrecord Road [])
(defrecord Building [health burned database])
(defrecord Firefighter [health water database])
(defn generate-world-vec
[y x] (vec (repeat y (vec (repeat x Road)))))
(defn add-buildings
([world]
(add-buildings world 0.5 (count world) (count (world 0)) (count (world 0)) (count world)))
([world prob]
(add-buildings world prob (count world) (count (world 0)) (count (world 0)) (count world)))
([world prob maxy maxx cury curx]
(if (and (zero? curx) (zero? cury))
world
(if (zero? curx)
(recur (update-in world [cury curx]
(fn [arg]
(println (str "y:" cury ",x:" curx " -- arg type: " (type arg)))
(if (> (rand) prob) (Building. 1.0 0.0 {}) arg)))
prob maxy maxx (dec cury) maxx)
(recur (update-in world [curx cury]
(fn [arg]
(println (str "y:" cury ",x:" curx " -- arg type: " (type arg)))
(if (> (rand) prob) (Building. 1.0 0.0 {}) arg)))
prob maxy maxx cury (dec curx))))))
(defn pretty-print-world-vec
[pretty-print-world]
(println (first pretty-print-world))
(if (not (empty? pretty-print-world))
(recur (rest pretty-print-world))))
(defn world-obj-to-str
[rec-to-check]
(cond
(instance? Building rec-to-check) "B"
(instance? Road rec-to-check) "r"
(instance? Firefighter rec-to-check) "F"))
(defn pretty-print-world
([world]
(pretty-print-world [] [] world (count (world 0)) (count world)
(count (world 0)) (count world)))
([world-vec-str world-temp world maxx maxy curx cury]
(if (and (zero? curx) (zero? cury))
(pretty-print-world-vec world-vec-str)
(let [add-string (str (world-obj-to-str (get-in world [cury curx])))
world-temp-update (conj world-temp add-string)]
(if (zero? curx)
(recur (conj world-vec-str world-temp-update) [] world maxx maxy maxx (dec cury))
(recur world-vec-str world-temp-update world maxx maxy (dec curx) cury))))))
(defn create-world-vec
([] (create-world-vec 10 10 0.5 true))
([y x prob debg]
(if (not debg)
(add-buildings (generate-world-vec y x) prob)
(pretty-print-world (add-buildings (generate-world-vec y x) prob)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment