我在CL中玩耍,在尝试处理完整的二维版本之前制作战舰的一维版本,并且我已经打了一个挂断。为了检查船是否在那里,我用零表示它,当一个点击中时,我用星号替换它,所以我可以用numberp
检查这个列表。但是,当我运行(new-game)
时,它立即结束,这告诉我,我没有正确输入零,因此它们被识别为数字。我究竟做错了什么?我知道这肯定是一个新秀的错误。将列表值设置为CL中的数字,然后检查它们
;;;; Suez-Canal.lisp
;;;;
;;;; A simple, 1-Dimensional version of Battleship
;;;; The computer places a boat randomly, and you must sink it.
(setf *random-state* (make-random-state t))
(defparameter *boat-length* 3)
(defparameter *canal-length* 10)
(defparameter *shots-fired* 0)
(defun new-game()
(init-canal *canal-length*)
(place-boat)
(game-loop)
(format t "It took you ~a shots to sink the boat." *shots-fired*))
(defun init-canal (len)
(defparameter *canal* (make-list len)))
(defun place-boat()
(let ((pos (random-spot)))
(setf (nth pos *canal*) 'O)
(setf (nth (+ pos 1) *canal*) 'O)
(setf (nth (+ pos 2) *canal*) 'O)))
(defun random-spot()
(let ((x (random 7)))
x))
(defun game-loop()
(loop until (notany #'numberp *canal*)
do (progn
(prompt-for-guess)
(check-guess (read-guess))
(incf *shots-fired*))))
(defun prompt-for-guess()
(format t "~&Enter in a number between 1 and 10 to fire a shot.~&"))
(defun read-guess()
(parse-integer (read-line *query-io*) :junk-allowed t))
(defun check-guess (guess)
(if (and (<= guess 9)
(>= guess 0))
(fire-shot guess)
(progn
(format t "~&Invalid selection~&")
(check-guess (read-guess)))))
(defun fire-shot (pos)
(if (= (nth (- pos 1) *canal*) 0)
(progn
(setf (nth (- pos 1) *canal*) #\*)
(print "Hit!"))
(print "Miss!")))
+1你的游戏的名称。 :-) – 2010-12-17 12:04:33