이제 각 방별로 해당 방에 들어갔을때 설명을 출력하도록 한다.
;;;; 게임의 Castle 구성하기..
;;; north south east west up down mobs
(defvar *castle*
'((0 2 0 0 0 0 0 ("You are in the hall way." "There is a door in south." "Through window to the north you can see secret herb garden"))
(1 3 3 3 0 0 0 ("This is the audience chamber" "There is a window to the south. By looking to the right" "Through it you can see the entrance of the castle" "Doors leave this room to the north, east, and south" ))
(2 0 5 2 0 0 0 ("You are in great hall, all-shaped room" "There is doors to the east and to the north" "In the alcove is a door to the west"))
(0 5 0 0 0 0 0 ("This is the monarch's private meeting room" "There is a single exit to the south"))
(4 0 0 3 15 13 0 ("This inner hallway contains a door to the north." "And one to the west, and a circular stairwell" "passes though the room" "You can see ornamental lake through here"))
(0 0 1 0 0 0 0 ("You are at the entrance to a fobidding-looking" "stone castle. You are facing east"))
(0 8 0 0 0 0 0 ("This is castle's kitchen. Though windows in" "The north wall you can see secret herb garden." "A door leave the kitchen to the south" ))
(7 10 0 0 0 0 0 ("You are in store room admist spices" "vegatables, and vast sacks of floud and" "Other provisions. There is a door to the north" "and one to the south"))
(0 19 0 8 0 8 0 ("You are slowly descends."))
(8 0 11 0 0 0 0 ("You are in the real vestibule." "There are windows to the south from which" "you can see ornamental lake" "There is an exit to the east and " "one to north"))
(0 0 10 0 0 0 0 () )
(0 0 0 13 0 0 0 ("you are in dank dark dungeon" "There is single exit, a small hole in" "wall towards to east"))
(0 0 12 0 5 0 0 ("You are in the prison guardroom in the " "basement of the castle. The staiwell" "ends in the room. There is one other" "exit, a small hole in the east wall") )
(0 15 17 0 0 0 0 ("You are in the master bedroom on the upper" "level of the castle..." "Looking down from the window to the west, you" "can see the entrance to the castle, while the" "secret herb garden is visible below to the north" "window. There are doors to the east and" "to the south"))
(14 0 0 0 0 5 0 ("This is L-shaped upper hallway." "To the north is a door, and there is a strairwell in the hall as well. You can see" "the lake through the south windows."))
(17 0 19 0 0 0 0 ("This room was used as the castle treasury in" "by-gone years..." "There are no windows, just exits to the" "north and to the east"))
(18 16 0 14 0 0 0 ("Ooooh.. you are in the chembermaids' bedroom" "There is an exit to the west and a door " "TO the south"))
(0 17 0 0 0 0 0 ("This tiny room on the upper level is the" "dressing chamber. There is a window to the" "north. with a view of the here garden down" "below. A door leaves to the south."))
(9 0 16 0 0 0 0 ("This is a small room outside the castle" "lift which can be entered by a door to the north" "another door leads to the west, you can see" "the lake through the southern windows"))))
설명을 출력할 부분을 만든다.
먼저 방에서 각 요소를 가져오는 함수에, 새로 설명을 출력할 부분을 추가한다.
;;; 방의 리스트에서 각 요소를 가져오는 함수
(defun get-rooms-element (room element)
(cond ((char= element #\n) (car room))
((char= element #\s) (nth 1 room))
((char= element #\e) (nth 2 room))
((char= element #\w) (nth 3 room))
((char= element #\u) (nth 4 room))
((char= element #\d) (nth 5 room))
((char= element #\m) (nth 6 room))
((char= element #\v) (nth 7 room))
(t
nil)))
다음으로 해당하는 방의 설명을 출력하는 함수를 만든다.
;;; 방의 모습을 설명하는 루틴
(defun describe-room ()
(format t "The room is ~A~%" *room*)
(print-room *room*)
(if (= *room* 9) (setf *room* 10)))
9번방에 들어온 경우에는 자동으로 10번방으로 이동한다.
;;; 방의 내역을 출력하기
(defun print-room (which-room)
(let ((desc-list (get-rooms-element (get-room which-room ) #\v)))
(labels ((iter (field)
(cond ((null field) )
(t
(format t "~a~%" (car field))
(iter (cdr field))))))
(iter desc-list))))
마지막으로 플레이어가 11번방으로 들어왔다면 게임을 종료한다.
;;; 전체 이벤트 루프
(defun event-loop ()
(let ((k (get-rooms-element (get-room *room*) #\m))
(game-over nil))
(loop
(status-report)
(cond ((= *room* 11)
(progn
(format t "Congratulation, you escape the castle")
(setf game-over t)))
(t
(let ((input-key (keyboard-event)))
(cond ((string= input-key "n") (go-direction #\n))
((string= input-key "s") (go-direction #\s))
((string= input-key "e") (go-direction #\e))
((string= input-key "w") (go-direction #\w))
((string= input-key "u") (go-direction #\u))
((string= input-key "d") (go-direction #\d))
((string= input-key "c") (consume-food))
((string= input-key "f") (fight))
((string= input-key "r") (runaway))
((string= input-key "q") (setf game-over t))
((string= input-key "m") (magic-amulet))
((string= input-key "p") (picking-up))
((string= input-key "i") (inventory))))))
(if (eq game-over t)
(return-from event-loop)))))
다음 부터는 완성된 버전을 LISP식으로 조금씩 변화시켜나가보도록 하겠다.