Skip to content

Instantly share code, notes, and snippets.

@dlongmuir
Created January 6, 2014 01:37
Show Gist options
  • Save dlongmuir/8276820 to your computer and use it in GitHub Desktop.
Save dlongmuir/8276820 to your computer and use it in GitHub Desktop.
(ns wizard)
(def nodes {:living-room "You are in the living room. A wizard is snoring loudly on the couch. "
:garden "You are in a beautiful garden. There is a well in front of you. "
:attic "You are in the attic. There is a giant welding torch in the corner. "})
;(nodes :living-room)
(defn describe-location [loc node-list]
(loc node-list))
;(describe-location :living-room nodes)
(def edges {:living-room {:garden [:west :door]
:attic [:upstairs :ladder]}
:garden {:living-room [:east :door]}
:attic {:living-room [:downstairs :ladder]}})
;(def living (first (:living-room edges)))
;(second living)
(defn describe-path [edge]
(let [[where [direction object]] edge]
(str "There is a " (name object) " going " (name direction) " from here. ")))
;(describe-path (first (:living-room edges)))
(defn describe-paths [loc edge-list]
(apply str (map #(describe-path %) (loc edge-list))))
;(describe-paths :living-room edges)
(def objects [:whiskey :bucket :chain :frog])
(def object-locations (atom {:whiskey :living-room
:bucket :living-room
:chain :garden
:frog :garden}))
(defn objects-at [loc objs obj-locs]
(let [object-there? (fn [obj] (= loc (obj-locs obj)))]
(filter object-there? objs)) )
;(objects-at :living-room objects @object-locations)
(defn describe-objects [loc objs obj-locs]
(apply str (map #(str "You see a " (name %) " on the floor. ") (objects-at loc objs obj-locs))))
;(describe-objects :living-room objects @object-locations)
(def location (atom :living-room))
;; non-functional 'commands'
(defn look []
(str (describe-location @location nodes)
(describe-paths @location edges)
(describe-objects @location objects @object-locations)) )
(look)
;(@location edges)
;(vals (@location edges))
;(vec (map #(first %) (vals (@location edges))))
;(some #{:west} (map #(first %) (vals (@location edges))) )
;(def a (atom :east))
;(reset! a (some #{:upstairs} (map #(first %) (vals (@location edges))) ))
;@a
;(first (for [l (@location edges)
; :when (= @a (first (second l))) ]
; (first l) ))
(defn walk [dir]
(let [new-loc
(first (for [l (@location edges)
:when (= dir (first (second l))) ]
(first l) )) ]
(if new-loc
(do
(reset! location new-loc)
(look))
(str "sorry there is no way " (name dir))) ))
;(walk :east)
;(walk :upstairs)
;(walk :north)
;(walk :downstairs)
(objects-at @location objects @object-locations)
(defn pickup [object]
(let [object (some #{object} (objects-at @location objects @object-locations))]
(if object
(do
(swap! object-locations assoc object :body)
(str "You are now carrying the " (name object) ". "))
(str "You cannot get that. ")) ) )
;(pickup :dog)
;(pickup :whiskey)
;object-locations
;(pickup :bucket)
(defn inventory []
(str "Items: " (apply str (interpose ", "(map #(name (first %)) (filter #(= :body (second %)) @object-locations))))) )
(inventory)
;; game repl
;(defn game-repl []
; (while true (println (eval (read)))))
(defn game-read []
(let [cmd (read-string (str "(" (read-line) ")"))]
(cons (first cmd)
(map keyword (rest cmd)))))
(def allowed-commands-set #{'look 'walk 'pickup 'inventory})
(defn game-eval [sexp]
(if (allowed-commands-set (first sexp))
(eval sexp)
"I do not know that command."))
(defn game-repl []
(let [cmd (game-read)]
(when-not (= :quit (keyword (first cmd)))
(println (game-eval cmd))
(game-repl)
)
)
)
(game-repl)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment