2

私はCLで遊んでいて、完全な2次元バージョンに取り組む前に、1次元バージョンの戦艦を作成していますが、ハングアップしました。ボートがそこにあるかどうかを確認するために、ゼロで表し、スポットがヒットした場合はアスタリスクに置き換えるので、リストを。で確認できますnumberp。ただし、を実行する(new-game)とすぐに終了します。これは、ゼロが正しく入力されていないため、数値として認識されていることを示しています。私は何が間違っているのですか?私はそれが新人の間違いであるに違いないことを知っています。

;;;; 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!")))
4

2 に答える 2

4

ゼロをまったく入力していませんが、文字 ' O ' を入力しています。

その他の注意事項:

DEFUN内でDEFPARAMETERを使用しないでください。最上位で変数を定義し、初期化関数内でそれをSETFするだけです。

ランダム アクセスにリストを使用しないでください。配列を使用します。

数値比較演算子は、数値以外の値を指定するとエラーを通知します。一般的な比較にはEQLを使用します。

于 2010-12-17T12:52:34.190 に答える
2

修正版は次のとおりです。

(setf *random-state* (make-random-state t))
(defparameter *boat-length* 3)
(defparameter *canal-length* 10)
(defparameter *shots-fired* 0)

;;; you need to declare *canal* at toplevel.
(defparameter *canal* nil)

(defun new-game ()
  (init-canal *canal-length*)
  (place-boat)
  (game-loop)
  (format t "It took you ~a shots to sink the boat." *shots-fired*))

;;; just set the the variable.
(defun init-canal (length)
  (setq *canal* (make-list length)))

;;; you need to set those positions to 0 and not to O
(defun place-boat ()
  (let ((pos (random-spot)))
    (setf (nth pos       *canal*) 0)
    (setf (nth (+ pos 1) *canal*) 0)
    (setf (nth (+ pos 2) *canal*) 0)))

;;; no need for a LET
(defun random-spot ()
  (random 7))

;;; no need for progn
;;; you could also replace UNTIL NOTANY with WHILE SOME
(defun game-loop ()
  (loop until (notany #'numberp *canal*)
       do
       (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))

;;; <= can take more than two arguments
;;; typically this recursive version might be replaced with a LOOP
(defun check-guess (guess)
  (if (<= 0 guess 9)
      (fire-shot guess)
    (progn
      (format t "~&Invalid selection~&")
      (check-guess (read-guess)))))

;;; use EQL, = only compares numbers
(defun fire-shot (pos)
  (if (eql (nth (- pos 1) *canal*) 0)
      (progn
        (setf (nth (- pos 1) *canal*) #\*)
        (print "Hit!"))
      (print "Miss!")))
于 2010-12-17T12:52:43.057 に答える