ラベル Adventure の投稿を表示しています。 すべての投稿を表示
ラベル Adventure の投稿を表示しています。 すべての投稿を表示

2009年12月23日水曜日

How to make an adventure game built on Scheme vol.3

どうも、PLT Schemeの構造体のアクセサをズラズラ書き並べるのがあまり美しくないです。
make-scenarioだらけ。
やってみると、高階手続きでマッピングしながら構造体を作成する事も出来るっぽいんで、コードをそのように修正してみます。
本体はともかくとして、こっちの方がSchemeっぽいのではないでしょうか。

;; PLT Scheme 依存

(define-struct scenario
(iKind ;処理の種類(0 -> 通常文章 1-> 選択肢 2-> キー待ち)
pString ;文章
idx1 ;次のシナリオのインデックス(-1で終了)
idx2 ;次のシナリオのインデックス
))

(define *s-aScenario*
(list->vector
(map (lambda (x)
(apply make-scenario x))
'((0 "シナリオ1\n" 1 0)
(1 "選択肢1 -> 1 選択肢2 -> 2\n" 2 3)
(0 "シナリオ2\n" 9 0)
(0 "シナリオ3\n" 4 0)
(1 "選択肢1 -> 1 選択肢2 -> 2\n" 5 6)
(0 "シナリオ4\n" 9 0)
(0 "シナリオ5\n" 7 0)
(2 "Hit Any Key" 8 0)
(0 "シナリオ6\n" 9 0)
(0 "エンドシナリオ\n" -1 0)))))

(define (main)
(do ((index 0
(let ((it (vector-ref *s-aScenario* index)))
(case (scenario-iKind it)
((0) (scenario-idx1 it))
((1) (let ((getche (read)))
(if (= getche 1)
(scenario-idx1 it)
(scenario-idx2 it))))
((2) (let ((getche (read)))
(scenario-idx1 it)))))))
((< index 0))
(display (scenario-pString (vector-ref *s-aScenario* index)))))


vol.4があるかどうかは知らない。

How to make an adventure game built on Scheme vol.2

取りあえず何も考えずに上のリンクに従って実装。


;; PLT Scheme 依存

(define-struct scenario
(iKind ;処理の種類(0 -> 通常文章 1-> 選択肢 2-> キー待ち)
pString ;文章
idx1 ;次のシナリオのインデックス(-1で終了)
idx2 ;次のシナリオのインデックス
))

(define *s-aScenario*
(vector
(make-scenario 0 "シナリオ1\n" 1 0)
(make-scenario 1 "選択肢1 -> 1 選択肢2 -> 2\n" 2 3)
(make-scenario 0 "シナリオ2\n" 9 0)
(make-scenario 0 "シナリオ3\n" 4 0)
(make-scenario 1 "選択肢1 -> 1 選択肢2 -> 2\n" 5 6)
(make-scenario 0 "シナリオ4\n" 9 0)
(make-scenario 0 "シナリオ5\n" 7 0)
(make-scenario 2 "Hit Any Key" 8 0)
(make-scenario 0 "シナリオ6\n" 9 0)
(make-scenario 0 "エンドシナリオ\n" -1 0)))

(define (main)
(do ((index 0
(let ((it (vector-ref *s-aScenario* index)))
(case (scenario-iKind it)
((0) (scenario-idx1 it))
((1) (let ((getche (read)))
(if (= getche 1)
(scenario-idx1 it)
(scenario-idx2 it))))
((2) (let ((getche (read)))
(scenario-idx1 it)))))))
((< index 0))
(display (scenario-pString (vector-ref *s-aScenario* index)))))


個人的な好みで言うと、Cのコードってそんなに綺麗と思わない。重複多いし。
Schemeへの翻訳は結構大変だったりします。

vol.3があるかどうかは知らない。

How to make an adventure game built on Scheme vol.1

取りあえず何も疑問を抱かずに上のリンクの方針に従う。

(define (main)
(display "シナリオ1\n")
(display "選択肢1 -> 1 選択肢2 -> 2\n")
(let loop ()
(let ((i (read)))
(cond
((not (integer? i))
(display "間違った入力です\n")
(loop))
((= i 1)
(display "シナリオ4\n"))
(else
(display "シナリオ5\n")))))
(display "エンドシナリオ\n"))

vol.2があるかどうかは知らない。