RCRPG/Clojure: Difference between revisions
refactoring
Pistacchio (talk | contribs) (clojure code) |
Pistacchio (talk | contribs) (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
[coll k]
(some #{k} coll))
Line 93 ⟶ 100:
[(coord-at (world :current-room) direction) (hash-set (rand-nth [:gold :sledge :ladder]))])
(defn
"fn if direction is valid, otherwise prints an error message, else fnelse"
[direction
;; ** describing rooms ** ;;
(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) "."))))
(defn describe
Line 156 ⟶ 157:
(str " (" r ")")
nil)]
(join "
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))
(defn dig
Line 169 ⟶ 180:
([world direction]
(let [dir (keyword direction)]
(if
(if (exit? (current-room world) world dir)
(if (= (world :equipped) :sledge)
[(
(
"You need
(defn move
Line 191 ⟶ 195:
(let [dir (keyword direction)
target-coord (coord-at (room-position (current-room world)) dir)]
(if
(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."
(let [updated-world (assoc world :current-room target-coord)]
[(describe (current-room updated-world) updated-world)
updated-world]))
"Where?!")))
([world] "Where do you want to
(defn equip
Line 213 ⟶ 211:
(let [i (keyword item)]
(if (in-inv? world i)
"You haven't
([world] "What do you want to equip?"))
(defn drop-item
Line 234 ⟶ 227:
equipped-item (world :equipped)]
(if (= i :all)
:equipped nil
:inventory #{}
:maze (update-room (clojure.set/union current-ground current-inventory)))
(
:equipped (
:inventory (
"You haven't such an
([world] "What do you want to drop?"))
(defn take-item
Line 266 ⟶ 252:
equipped-item (world :equipped)]
(if (= i :all)
:inventory (clojure.set/union current-inventory current-ground)
(if (in?
:inventory (
:maze (
"There is not such
([world] "What do you want to pick up?"))
(defn inventory
Line 289 ⟶ 268:
[world]
(let [inv (world :inventory)]
(defn equip
Line 298 ⟶ 276:
(let [i (keyword item)]
(if (in-inv? world i)
["Item equipped." (assoc world :equipped i)]
"You haven't
([world] "What do you want to equip?"))
(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))]))
([world alias] (str "Alias '" alias "' to what?")))
(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))]))
([world] "What name?"))
(defn help
"prints an help message"
[world]
(str
"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
(cond
(= (first i) "exit") nil
(contains?
:else (do (println "What do you mean?") world)))
(do
(println "Hm?!")
world)))
;; main loop
|