I probably just don't understand Lisp scoping, but I'm struggling to understand what's going on in this tic-tac-toe game, which I've stubbed down to demonstrate the problem:
(defun tic-tac-toe ()
(let ((board '((- - -) (- - -) (- - -))))
(draw-board board)
(format t "~%")
(make-move board)
(draw-board board)))
(defun draw-board (board)
(dolist (row board)
(apply #'format (cons t (cons "~A ~A ~A~%" row)))))
(defun make-move (board)
(setf (caar board) 'x))
The tic-tac-toe
function is intended to be the main entry point, and each time I run it the game should start from scratch. However, look what happens when I run it in CLISP:
[2]> (tic-tac-toe)
- - -
- - -
- - -
X - -
- - -
- - -
NIL
[3]> (tic-tac-toe)
X - -
- - -
- - -
X - -
- - -
- - -
NIL
The first time, the board starts out blank (all dashes), but after that the board always starts out with the old values left over in it. How is this happening? Shouldn't the let
re-initialize the board each time?