;; pangolins game ;; © 2004 Sam Morris ;; 13 December 2004 (defvar *tree* '("red panda")) (defun prompt-read (prompt) (format *query-io* "~a: " prompt) (force-output *query-io*) (let ((input (read-line *query-io*))) (if (equal input "") (prompt-read prompt) input))) (defun save-tree (filename) (with-open-file (out filename :direction :output :if-exists :supersede) (with-standard-io-syntax (print *tree* out)))) (defun load-tree (filename) (with-open-file (in filename) (with-standard-io-syntax (setf *tree* (read in))))) (defun create-node (question yes-object no-object) (list question (list yes-object) (list no-object))) (defun ask-leaf-node (node) (let ((old-object (first node))) (if (y-or-n-p (format nil "I guess your object is: ~a" old-object)) node (let* ((new-object (prompt-read "What object were you thinking of?")) (question (prompt-read (format nil "Enter a question that distinguishes between ~a and ~a" new-object old-object)))) (if (y-or-n-p (format nil "Concerning ~a: ~a" new-object question)) (create-node question new-object old-object) (create-node question old-object new-object)))))) (defun ask-node (node) (if (cdr node) ;if a node has only one element then it is a leaf (if (y-or-n-p (first node)) (list (first node) (ask-node (second node)) (third node)) (list (first node) (second node) (ask-node (third node)))) (ask-leaf-node node))) (defun play-game () (setf *tree* (ask-node *tree*)) (if (y-or-n-p "Again?") (play-game))) (defun main () (if (y-or-n-p "Load data from a file?") (load-tree (prompt-read "Name of file"))) (play-game) (if (y-or-n-p "Save data to file?") (save-tree (prompt-read "Name of file"))))