1

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 つのマップに配置するには扱いにくいためです。

4

1 に答える 1

3

@(resolve sym)似たようなものとして使用できますsymbol-value。実際に行うことはsym、現在の名前空間のシンボルによって名前が付けられた Var を検索し ( use/で取り込まれた Var である可能性がありますrequire :refer)、その値を抽出することです。ns-resolveVar が検索される名前空間を制御するかどうかを確認してください。

Var-per-location を使用せずに、場所を地図のどこかに保存することもできます。

(def locations {:hallway ... :bedroom ...})

(実行時に新しい場所を追加するのに便利なように、このマップを Ref に入れることもできます。)

于 2013-07-06T10:02:10.027 に答える