Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Mire: allow grab and drop of items

  • Loading branch information...
commit 42f14dbb28a7982049e5f3defc56b9f0d881e9c8 1 parent e5b892d
@apauley authored
View
7 PeepCode/Clojure/mire/src/mire.clj
@@ -1,7 +1,7 @@
(add-classpath (str "file://" (.getParent (java.io.File. *file*)) "/"))
(ns mire
- (:use [mire commands rooms util])
+ (:use [mire commands rooms player util])
(:use [clojure.contrib server-socket duck-streams]))
(def port 3333)
@@ -18,8 +18,9 @@
*out* (writer out)]
(print-flush "\nWhat is your name? ")
- (binding [*current-room* (ref (rooms :start))
- player-name (read-line)]
+ (binding [player-name (read-line)
+ *current-room* (ref (rooms :start))
+ *inventory* (ref #{})]
(move-player-to (current-room))
(println (look))
(print-prompt)
View
46 PeepCode/Clojure/mire/src/mire/commands.clj
@@ -1,5 +1,5 @@
(ns mire.commands
- (:use [mire rooms])
+ (:use [mire rooms player util])
(:use [clojure.contrib str-utils]))
(defn look []
@@ -20,14 +20,42 @@
(look))
"No way.")))
-(def commands {:move move,
- :north (fn [] (move :north)),
- :east (fn [] (move :east)),
- :south (fn [] (move :south)),
- :west (fn [] (move :west)),
- :look look,
- :wtf (fn []
- "Yeah sure, I'll do that right after you grow a brain.")})
+(defn grab [thing]
+ "Pick up something in the room"
+ (if (current-room-contains? thing)
+ (dosync
+ (move-between-refs (keyword thing)
+ (:items (current-room))
+ *inventory*)
+ (str "You picked up " thing "."))
+ (str "There isn't any " thing " here.")))
+
+(defn discard [thing]
+ "Drop something you carry"
+ (if (carrying? thing)
+ (dosync
+ (move-between-refs (keyword thing)
+ *inventory*
+ (:items (current-room)))
+ (str "You are dropped the " thing "."))
+ (str "You are not carrying any " thing ".")))
+
+(defn inventory []
+ "See what you are carrying"
+ (str-join "\n " (conj @*inventory* "You are carrying:")))
+
+(def commands
+ {:move move,
+ :north (fn [] (move :north)),
+ :east (fn [] (move :east)),
+ :south (fn [] (move :south)),
+ :west (fn [] (move :west)),
+ :look look,
+ :grab grab,
+ :discard discard,
+ :inventory inventory,
+ :wtf (fn []
+ "Yeah sure, I'll do that right after you grow a brain.")})
(defn execute
"Execute a command passed from the client"
View
7 PeepCode/Clojure/mire/src/mire/player.clj
@@ -0,0 +1,7 @@
+(ns mire.player
+ (:use clojure.contrib.seq-utils))
+
+(def *inventory*)
+
+(defn carrying? [thing]
+ (includes? @*inventory* (keyword thing)))
View
6 PeepCode/Clojure/mire/src/mire/rooms.clj
@@ -32,6 +32,12 @@
"Returns the items available in the current room"
@(:items (current-room)))
+(defn room-contains? [room thing]
+ (@(:items room) (keyword thing)))
+
+(defn current-room-contains? [thing]
+ (room-contains? (current-room) thing))
+
(defn move-player-to [target]
(dosync
(move-between-refs player-name
Please sign in to comment.
Something went wrong with that request. Please try again.