RCRPG/Clojure: Difference between revisions

refactoring
(clojure code)
 
(refactoring)
Line 29:
"name" "name-room"} #"\|"))
 
(def *base-valid-actions* #{:drop-item :take-item :inventory :move :alias-command :name-room :dig :help :equip :look})
 
(defn valid-actions
[aliases]
(into #{}
(concat
(map keyword (keys aliases))
*base-valid-actions*)))
 
;; room = (x y z [on-the-ground])
Line 79 ⟶ 86:
 
(defn in?
"returns true if k is in collsequence"
[coll k]
(some #{k} coll))
Line 93 ⟶ 100:
[(coord-at (world :current-room) direction) (hash-set (rand-nth [:gold :sledge :ladder]))])
 
(defn if-valid-direction
"fn if direction is valid, otherwise prints an error message, else fnelse"
[direction fn fnelse]
(if-not (in? *directions* direction))
(do
(println "Where?!")
fnelse)
fn))
 
;; ** describing rooms ** ;;
 
;; find adjacent exits
(defn find-exits
"returns a list of directions with the exit directions for room, eg. (:north :east)"
Line 147 ⟶ 149:
(let [room-content (room-ground room)]
(if (not-empty room-content)
(str " On the ground you can see: " (describe-items room-content) "."))))
(str
" On the ground you can see: " (describe-items room-content) "."))))
 
(defn describe
Line 156 ⟶ 157:
(str " (" r ")")
nil)]
(println (str "You are at " (join " " (room-position room)) room-name "." (describe-ground room)
(join " " (describeroom-exitsposition room world)))))
room-name "."
(describe-ground room)
(describe-exits room world))))
 
(defn perform-action
[world action args]
(let [result (apply (resolve (symbol action)) (conj args world ))]
(cond
(string? result) (do (println result) world)
(vector? result) (do (println (first result)) (second result) ))))
 
;; ** actions ** ;;
 
(defn look [world]
"describes the room the player is in"
(describe (current-room world) world))
world)
 
(defn dig
Line 169 ⟶ 180:
([world direction]
(let [dir (keyword direction)]
(if- (valid-direction dir)
(if (exit? (current-room world) world dir)
(do"There is already a room!"
(println "There is already a room!")
world)
(if (= (world :equipped) :sledge)
[(dostr "You dig a new room " direction "ward.")
(printlnassoc (strworld "You:maze dig(merge a(world new:maze) (new-room-at "world direction "ward."dir)))]
"You need (assocto worldequip :mazea (mergesledge (worldin :maze)order (new-room-atto worlddig dir))the wall!"))
(do"Where?!")))
(println[world] "YouWhere needdo toyou equip a sledge in orderwant to dig the wall!?"))
world)))
world)))
([world]
(println "Where do you want to dig?")
world))
 
(defn move
Line 191 ⟶ 195:
(let [dir (keyword direction)
target-coord (coord-at (room-position (current-room world)) dir)]
(if- (valid-direction dir)
(if (exit? (current-room world) world dir)
(if (and (= dir :up) (not ((room-ground (current-room world)) :ladder)))
"You cannot go up if there's no ladder in the room."
(do
(println "You cannot go up if there's no ladder in the room.")
world)
(let [updated-world (assoc world :current-room target-coord)]
[(describe (current-room updated-world) updated-world)
updated-world]))
(do"There's no exit in that direction!")
"Where?!")))
(println "There's no exit in that direction!")
([world] "Where do you want to worldgo?"))
world)))
([world]
(println "Where do you want to go?")
world))
 
(defn equip
Line 213 ⟶ 211:
(let [i (keyword item)]
(if (in-inv? world i)
(do["Equipped!"
(printlnassoc world :equipped "Equipped!"i)])
"You haven't (assocsuch worldan :equipped iitem"))
([world] "What do you want to equip?"))
(do
(println "You haven't such an item")
world))))
([world]
(println "What do you want to equip?")
world))
 
(defn drop-item
Line 234 ⟶ 227:
equipped-item (world :equipped)]
(if (= i :all)
(do["Everything dropped!"
(println "Everythingassoc dropped!")world
(assoc world
:equipped nil
:inventory #{}
:maze (update-room (clojure.set/union current-ground current-inventory))))]
(doif (in-inv? world i)
(if["Item (in-inv? world i)dropped!"
(doassoc world
:equipped (printlnif "Item(= equipped-item i) nil dropped!"equipped-item)
:inventory (assocdisj worldcurrent-inventory i)
:equippedmaze (ifupdate-room (=conj equippedcurrent-itemground i) nil equipped-item))]
"You haven't such an :inventory (disj current-inventory iitem"))))
([world] "What do you want to drop?"))
:maze (update-room (conj current-ground i))))
(do
(println "You haven't such an item")
world))))))
([world]
(println "What do you want to drop?")
world))
 
(defn take-item
Line 266 ⟶ 252:
equipped-item (world :equipped)]
(if (= i :all)
(do["Everything taken!"
(println "Everythingassoc taken!")world
:inventory (clojure.set/union current-inventory current-ground)
(assoc world
:inventorymaze (clojure.set/union currentupdate-inventoryroom current-ground#{}))]
(if (in? :maze (updatecurrent-roomground #{}))i)
(do ["Item taken!"
(if (in? current-groundassoc i)world
:inventory (doconj current-inventory i)
:maze (printlnupdate-room "Item(disj taken!"current-ground i)))]
"There is not such (associtem worldon the ground!"))))
([world] "What do you want to pick up?"))
:inventory (current-inventory i)
:maze (update-room (disj current-ground i))))
(do
(println "There is not such item on the ground!")
world))))))
([world]
(println "What do you want to pick up?")
world))
 
(defn inventory
Line 289 ⟶ 268:
[world]
(let [inv (world :inventory)]
(println (if (empty? inv)
"You are not carrying anything"
(str "You are carrying: " (describe-items inv)))))
world)
 
(defn equip
Line 298 ⟶ 276:
(let [i (keyword item)]
(if (in-inv? world i)
["Item equipped." (assoc world :equipped i)]
(do
"You haven't (println "Itemsuch equipped.item")))
([world] "What do you want to equip?"))
(assoc world :equipped i))
(do
(println "You haven't such item")
world))))
([world]
(do
(println "What do you want to equip?")
world)))
 
(defn alias-command
Line 314 ⟶ 285:
(let [command (join " " commands)
current-aliases (world :aliases)]
[(str "Alias created for the command " command) (assoc world :aliases (assoc current-aliases alias command))]))
(do
(println (str[world] "Alias created for the command what?" command))
([world alias] (str "Alias '" alias "' to what?")))
(assoc world :aliases (assoc current-aliases alias command)))))
([world]
(do
(println "Alias what?")
world))
([world alias]
(do
(println (str "Alias '" alias "' to what?"))
world)))
 
(defn name-room
Line 332 ⟶ 295:
current-named-room (world :named-rooms)
current-location (room-position (current-room world))]
["Done!" (assoc world :named-rooms (assoc current-named-room current-location a))]))
(do
([world] "What name?"))
(println "Done!")
 
(assoc world :named-rooms (assoc current-named-room current-location a)))))
(defn help
([world]
"prints an help message"
(do
[world]
(println "What name?")
(str
world)))
"Welcome to the dungeon!\n"
"You need a sledge to dig rooms and ladders to go upwards.\n"
"Valid commands are: directions (north, south...), dig, take, drop, equip, inventory and look.\n"
"Additionally you can tag rooms with the 'name' command and alias commands with 'alias'.\n"
"Have fun!\n"))
 
;; ** user input ** ;;
Line 354 ⟶ 322:
command (translate (first sanitized-input) (world :aliases))
i (concat command (rest sanitized-input))
[action (first& args] i)
argscurrent-valid-actions (restworld i:aliases)]
(cond
(= (first i) "exit") nil
(contains? *(valid-actions* current-valid-actions) (keyword action)) (try
(applytry (resolveperform-action (symbolworld action)) (conj args world ))
(catch IllegalArgumentException e (println "Invalid arguments") world))
:else (do (println "What do you mean?") world)))
(do
(println "Hm?!")
world)))
 
(defn help
"prints an help message"
[world]
(do
(println "Welcome to the dungeon!")
(println "You need a sledge to dig rooms and ladders to go upwards.")
(println "Valid commands are: directions (north, south...), dig, take, drop, equip, inventory and look.")
(println "Additionally you can tag rooms with the 'name' command and alias commands with 'alias'." )
(println "Have fun!")
world))
 
;; main loop
53

edits