Tuesday 15 March 2011

allegro cl - Common-Lisp Code was working fine, now "Error: Attempt to take the value of the unbound variable `*OPPONENT*'." -



allegro cl - Common-Lisp Code was working fine, now "Error: Attempt to take the value of the unbound variable `*OPPONENT*'." -

i'm baffled. i'm playing around tic-tac-toe game found in ch 10 of mutual lisp: gentle introduction symbolic computation https://www.cs.cmu.edu/~dst/lispbook/book.pdf . worked in ide, saved , compiled+loaded it. ran several games no problem. so, copied known-working file , started tweaking. again, no problems -- working fine. now, however, when run (play-one-game) next error:

error: effort take value of unbound variable '*opponent**'

i error in both original , copy. closed allegrocl , restarted computer, problem persisted after reboot. updated programme , ran ./update.sh in it's app directory.

finally, decided re-create illustration right pdf in brand-new file in different directory, , same problem. don't know changed, it's got me plussed least.

(defun make-board () (list 'board 0 0 0 0 0 0 0 0 0)) (defun convert-to-letter (v) (cond ((equal v 1) "o") ((equal v 10) "x") (t " "))) (defun print-row (x y z) (format t "~& ~a | ~a | ~a" (convert-to-letter x) (convert-to-letter y) (convert-to-letter z))) (defun print-board (board) (format t "~%") (print-row (nth 1 board) (nth 2 board) (nth 3 board)) (format t "~& -----------") (print-row (nth 4 board) (nth 5 board) (nth 6 board)) (format t "~& -----------") (print-row (nth 7 board) (nth 8 board) (nth 9 board)) (format t "~%~%")) (defun make-move (player pos board) (setf (nth pos board) player) board) (setf *triplets* '((1 2 3) (4 5 6) (7 8 9) ;horizontal triplets. (1 4 7) (2 5 8) (3 6 9) ;vertical triplets. (1 5 9) (3 5 7))) ;diagonal triplets. (defun sum-triplet (board triplet) (+ (nth (first triplet) board) (nth (second triplet) board) (nth (third triplet) board))) (defun compute-sums (board) (mapcar #'(lambda (triplet) (sum-triplet board triplet)) *triplets*)) (defun winner-p (board) (let ((sums (compute-sums board))) (or (member (* 3 *computer*) sums) (member (* 3 *opponent*) sums)))) (defun play-one-game () (if (y-or-n-p "would go first? ") (opponent-move (make-board)) (computer-move (make-board)))) (defun opponent-move (board) (let* ((pos (read-a-legal-move board)) (new-board (make-move *opponent* pos board))) (print-board new-board) (cond ((winner-p new-board) (format t "~&you win!")) ((board-full-p new-board) (format t "~&tie game.")) (t (computer-move new-board))))) (defun read-a-legal-move (board) (format t "~&your move: ") (let ((pos (read))) (cond ((not (and (integerp pos) (<= 1 pos 9))) (format t "~&invalid input.") (read-a-legal-move board)) ((not (zerop (nth pos board))) (format t "~&that space occupied.") (read-a-legal-move board)) (t pos)))) (defun board-full-p (board) (not (member 0 board))) (defun computer-move (board) (let* ((best-move (choose-best-move board)) (pos (first best-move)) (strategy (second best-move)) (new-board (make-move *computer* pos board))) (format t "~&my move: ~s" pos) (format t "~&my strategy: ~a~%" strategy) (print-board new-board) (cond ((winner-p new-board) (format t "~&i win!")) ((board-full-p new-board) (format t "~&tie game.")) (t (opponent-move new-board))))) (defun random-move-strategy (board) (list (pick-random-empty-position board) "random move")) (defun pick-random-empty-position (board) (let ((pos (+ 1 (random 9)))) (if (zerop (nth pos board)) pos (pick-random-empty-position board)))) (defun make-three-in-a-row (board) (let ((pos (win-or-block board (* 2 *computer*)))) (and pos (list pos "make 3 in row")))) (defun block-opponent-win (board) (let ((pos (win-or-block board (* 2 *opponent*)))) (and pos (list pos "block opponent")))) (defun win-or-block (board target-sum) (let ((triplet (find-if #'(lambda (trip) (equal (sum-triplet board trip) target-sum)) *triplets*))) (when triplet (find-empty-position board triplet)))) (defun find-empty-position (board squares) (find-if #'(lambda (pos) (zerop (nth pos board))) squares)) (defun choose-best-move (board) ;second version. (or (make-three-in-a-row board) (block-opponent-win board) (random-move-strategy board)))

if you're next instructions book, tells invoke

(setf *computer* 10) (setf *opponent* 1)

apparently didn't include in code listing.

in general -- if error unbound, obvious thing look code should binding value.

common-lisp allegro-cl

No comments:

Post a Comment