Permalink
Browse files

Mire: room inhabitants

  • Loading branch information...
1 parent e838abe commit 0e842a451f0819235f497d2015cd9dfde3cb6aa0 Andreas Pauley committed Dec 22, 2009
@@ -6,24 +6,30 @@
(def port 3333)
+(defn- print-flush [text]
+ (print text)
+ (flush))
+
(defn- print-prompt []
(println)
- (print "> ")
- (flush))
+ (print-flush "> "))
(defn- mire-handle-client [in out]
"Private function to handle client requests"
; Re-assign stdin and stdout
(binding [*in* (reader in)
- *out* (writer out)
- *current-room* (ref (rooms :start))]
- (println (look))
- (print-prompt)
- (loop [input (read-line)]
- (println (execute input))
+ *out* (writer out)]
+
+ (print-flush "Player name: ")
+ (binding [*current-room* (ref (rooms :start))
+ player-name (read-line)]
+ (println (look))
(print-prompt)
- (recur (read-line)))))
+ (loop [input (read-line)]
+ (println (execute input))
+ (print-prompt)
+ (recur (read-line))))))
(def server (create-server port mire-handle-client))
@@ -5,15 +5,16 @@
(defn look []
"Get a description of the current room"
(str (:desc (current-room))
- "\nExits: " (keys (:exits (current-room)))))
+ "\nExits: " (keys (:exits (current-room)))
+ "\nInhabitants: " (current-inhabitants)))
(defn move
"We gotta get out of this place... Give a direction."
[direction]
(let [target-name ((:exits (current-room)) (keyword direction))
target (rooms target-name)]
(if target
- (do (set-current-room target)
+ (do (move-player-to target)
(look))
"No way.")))
@@ -2,15 +2,25 @@
(def rooms
{:start {:desc "You are in an old, dusty room."
- :exits {:north :closet}}
+ :exits {:north :closet}
+ :inhabitants (ref #{})}
:closet {:desc "You are in a cramped closet. You feel a slight breeze."
- :exits {:south :start}}})
+ :exits {:south :start}
+ :inhabitants (ref #{})}})
(def *current-room*)
+(def player-name)
(defn current-room []
"Returns the current room"
@*current-room*)
-(defn set-current-room [target]
- (dosync (ref-set *current-room* target)))
+(defn current-inhabitants []
+ "Returns the inhabitants in the current room"
+ @(:inhabitants (current-room)))
+
+(defn move-player-to [target]
+ (dosync
+ (alter (:inhabitants (current-room)) disj player-name)
+ (alter (:inhabitants target) conj player-name)
+ (ref-set *current-room* target)))

0 comments on commit 0e842a4

Please sign in to comment.