Rcjp's Weblog

July 26, 2006

Twenty Questions

Filed under: lisp — rcjp @ 1:55 pm

;;; Classic 'fish-flower-fruit' game
;;; Questions are put to guess the object, and can only be
;;; answered 'y' or 'n'
;;; run (ask-questions) to start

(defun make-info-node (question-text afirmative negative)
  (list question-text afirmative negative))

(defun get-question (info-node) (first info-node))
(defun get-afirmative (info-node) (second info-node))
(defun get-negative (info-node) (third info-node))

(defun guess (info-node item)
  (format t "Is it a ~A ~%" item)
  (let ((reply (read)))
    (if (equal reply 'y)
        (format t "What is it ?~%")
        (let ((object (read)))
          (format t "What question distinguishes a ~A from a ~A ?" object item)
          (let ((question (read-line)))
            (setf (cdr (cdr info-node)) (list (make-info-node question object item)))

(defun continue-game? ()
  (format t "Continue game ?~%")
  (let ((reply (read)))
    (if (equal reply 'y)

(defun ask-question (info-node)
  (format t "~A  " (get-question info-node))
  (let ((reply (read)))
    (if (equal reply 'y)
        (check-answer info-node (get-afirmative info-node))
      (check-answer info-node (get-negative info-node)))))

(defun check-answer (info-node info-node-part)
  (if (listp info-node-part)
      (ask-question info-node-part)
    (guess info-node info-node-part)))

(defparameter *info* (list 'animal?
                     (list 'slimy? 'fish 'bear)

(defun ask-questions ()
  (ask-question *info*))

Leave a Comment »

No comments yet.

RSS feed for comments on this post.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

Blog at WordPress.com.

%d bloggers like this: