Clojure でテキスト アドベンチャーに挑戦しています。
これは私が苦労しているところです:
(ns records)
(defrecord Room [fdesc sdesc ldesc exit seen])
(defrecord Item [name location adjective fdesc ldesc sdesc flags action ])
(def bedroom (Room. "A lot of text."
nil
"some text"
'(( "west" hallway wearing-clothes? wear-clothes-f))
false))
(def hallway (Room. "description of room."
nil
"short desc of room."
'(("east" bedroom) ("west" frontdoor))
false))
(def location (ref bedroom))
(defn in?
"Check if sequence contains item."
[item lst]
(some #(= item %) lst))
(defn next-location
"return the location for a entered direction"
[direction ]
(second (first (filter #(in? direction %) (:exit @location)))))
(defn set-new-location
"set location parameter to new location."
[loc]
(dosync (ref-set location loc)))
私の問題は、変数の場所を更新することです。
入力する(set-new-location hallway)
と正しく動作します。場所が新しい部屋に設定され、そのフィールドにアクセスできます。ただし、部屋の出口フィールドから次の可能な出口を読み取る必要がありますが、(set-new-direction (next-exit "west"))
場所に入ると廊下と表示されますが、変数「廊下」を指していません。
CL では (symbol-value hallway) を使用します。Clojureでこれを行うにはどうすればよいですか?
編集: var-per-location を実際に使用したいのは、約 30 の場所をそれぞれ 20 行でスケッチしたため、1 つのマップに配置するには扱いにくいためです。