Created
July 16, 2011 23:45
-
-
Save anonymous/1086926 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
(ns robo-replacement.map-maker) | |
(defn make-road | |
[] {:type "road"}) | |
(defn make-building | |
[health burned database] | |
{:type "building" :health health :burned burned :database database}) | |
(defn make-firefighter | |
[health water database] | |
{:type "firefighter" :health health :water water :database database}) | |
;;creates base layout 2d vector array full of Road records | |
(defn generate-world-vec | |
[y x] (vec (repeat y (vec (repeat x (make-road)))))) | |
;;populates a given road-only-world (generated by generate-world-vec) | |
;;with Buildings with probability prob [0-1.0] or .5 by default | |
(defn add-buildings | |
([world] | |
(let [prob 0.5 | |
maxy (dec (count world)) | |
maxx (dec (count (world 0)))] | |
(add-buildings world prob maxy maxx maxy maxx))) | |
([world prob] | |
(let [maxy (dec (count world)) | |
maxx (dec (count (world 0)))] | |
(add-buildings world prob maxy maxx maxy maxx))) | |
([world prob maxy maxx cury curx] | |
(if (and (zero? curx) (zero? cury)) | |
world | |
(if (zero? curx) | |
(recur (update-in world [cury curx] | |
(fn [arg] | |
(if (> (rand) prob) (make-building 1.0 0.0 {}) arg))) | |
prob maxy maxx (dec cury) maxx) | |
(recur (update-in world [curx cury] | |
(fn [arg] | |
(if (> (rand) prob) (make-building 1.0 0.0 {}) arg))) | |
prob maxy maxx cury (dec curx)))))) | |
;;prings the string vectors generated by pretty-print-world | |
(defn pretty-print-world-vec | |
[pretty-print-world] | |
(println (first pretty-print-world)) | |
(if (not (empty? pretty-print-world)) | |
(recur (rest pretty-print-world)))) | |
;;returns the string representation of a given record in the world vector | |
(defn world-obj-to-str | |
[world-obj] | |
(cond | |
(= "building" (:type world-obj)) "B" | |
(= "road" (:type world-obj)) "r" | |
(= "firefighter" (:type world-obj)) "F")) | |
;;handles trying to nicely print the existing world | |
(defn pretty-print-world | |
([world] | |
(let [world-vec-str [] | |
world-temp [] | |
maxy (dec (count world)) | |
maxx (dec (count (world 0))) | |
cury maxy | |
curx maxx] | |
(pretty-print-world world-vec-str world-temp world maxy maxx cury curx))) | |
([world-vec-str world-temp world maxy maxx cury curx] | |
(if (and (zero? curx) (zero? cury)) | |
(conj world-vec-str (conj world-temp (str (world-obj-to-str (get-in world [cury curx]))))) | |
(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 maxy maxx (dec cury) maxx) | |
(recur world-vec-str world-temp-update world maxy maxx cury (dec curx))))))) | |
;;entry point function for creating a world | |
(defn create-world-vec | |
([] (create-world-vec 10 10 0.5 true)) | |
([y x prob debg] | |
(pretty-print-world (generate-world-vec y x)) | |
(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