home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #31 / NN_1992_31.iso / spool / gnu / emacs / sources / 905 < prev    next >
Encoding:
Text File  |  1992-12-22  |  4.3 KB  |  129 lines

  1. Path: sparky!uunet!cbmvax!yoda!ag
  2. From: ag@yoda.omnicron.com (Keith Gabryelski)
  3. Newsgroups: gnu.emacs.sources
  4. Subject: elisp `animal' program
  5. Message-ID: <1007@yoda.omnicron.com>
  6. Date: 23 Dec 92 00:59:40 GMT
  7. Organization: Omnicron Data Systems
  8. Lines: 119
  9.  
  10. Probably written too many times in lisp and yet here I am posting this
  11. one.  The ancient game of ``guess the animal''.
  12.  
  13. There is nothing special about this version of animal.  It saves its
  14. state to ~/.animals.
  15.  
  16. Interactive entry points are `animal' and `play-animal'.  Play-animal
  17. calls `animal' repeatedly with a boolean request to continue playing.
  18.  
  19. Written for GZ (where ever she is these days).
  20.  
  21. Pax, Keith
  22.  
  23.         *--------------------------------------*
  24.  
  25. (defvar animals-file "~/.animals" "Animal game data file")
  26.  
  27. (defvar animals-default-data '("a cat" nil nil) "Default data for animal game")
  28.  
  29. (defvar animals nil
  30.   "Animal game data.  A recursive list of the form
  31. (STRING TRUE-LIST FALSE_LIST) where TRUE-LIST and FALSE-LIST are lists of
  32. the this form and STRING is either a question posed to the user or a name
  33. of an animal.")
  34.  
  35. (defun animal ()
  36.   "Play the game of animal using the variable `animals'"
  37.   (interactive)
  38.   (let (animal-buffer)
  39.     (save-window-excursion
  40.       (if (file-readable-p (expand-file-name animals-file))
  41.       (setq animal-buffer
  42.         (find-file-noselect (expand-file-name animals-file)))
  43.     (progn
  44.       (setq animal-buffer
  45.         (create-file-buffer (expand-file-name animals-file)))
  46.       (set-buffer animal-buffer)
  47.       (erase-buffer)
  48.       (insert (format "%s" animals-default-data))))
  49.       (set-buffer animal-buffer)
  50.       (goto-char (point-min))
  51.       (setq animals (read animal-buffer)))
  52.     (animal-read-string
  53.      "This is the game called animal where you think of an animal
  54. and I try to guess which one you are thinking of.
  55.  
  56. Please think of an animal and answer the questions I give you." nil)
  57.     (setq animals (parse-animal-tree animals))
  58.     (save-window-excursion
  59.       (set-buffer animal-buffer)
  60.       (erase-buffer)
  61.       (insert (format "%s" animals))
  62.       (write-file (expand-file-name animals-file)))))
  63.  
  64. (defun play-animal ()
  65.   "Play the games of animal lots of times"
  66.   (interactive)
  67.   (let (done)
  68.     (while (not done)
  69.       (progn
  70.     (animal)
  71.     (setq done (not (y-or-n-p "Would you like to play again? ")))))))
  72.  
  73. (defun parse-animal-tree (animal-list)
  74.   "Ask questions about a list (STRING TRUE-LIST FALSE-LIST) until we reach a
  75. terminal"
  76.   (if (animal-query-user (car animal-list) (null (car (cdr animal-list))))
  77.       (if (null (car (cdr animal-list)))
  78.       (progn 
  79.         (message "Thanks for playing.")
  80.         animal-list)            
  81.     (list (car animal-list)
  82.           (parse-animal-tree
  83.            (car (cdr animal-list))) (car (cdr (cdr animal-list)))))
  84.     (if (null (car (cdr (cdr animal-list))))
  85.     (animal-give-up animal-list)
  86.       (list (car animal-list)
  87.         (car (cdr animal-list))
  88.         (parse-animal-tree (car (cdr (cdr animal-list))))))))
  89.  
  90. (defun animal-query-user (question terminal)
  91.   "Query user about information on a particular animal"
  92.   (y-or-n-p (if terminal
  93.         (format "Is it %s? " question)
  94.           (format "%s? " question))))
  95.  
  96. (defun animal-give-up (animal-list)
  97.   "Given an animal list, query the user for information on a new type
  98. of animal"
  99.   (let (new-animal new-question)
  100.     (progn
  101.       (setq new-animal
  102.         (animal-read-string "I give up.  I need to know a little bit about the animal you are thinking of
  103. so I won't miss this one next time." "What animal where you thinking of? "))
  104.       (setq new-question
  105.         (animal-read-string
  106.          (format
  107.           "I also need to know how to ask a yes or no question that
  108. distinguishes %s from %s.  Please type in such
  109. a question (ie, \"Does it have wings\" would be a suitable question to
  110. distinguish a bird from a cat)."
  111.     new-animal (car animal-list))  "Please type in a question: "))
  112.       (list new-question (list new-animal nil nil) animal-list))))
  113.  
  114. (defun animal-read-string (buffer-string prompt-string)
  115.   "Open a buffer up, spew some helpul advice and accept a string"
  116.   (let (animal-help-buffer)
  117.     (progn
  118.       (setq animal-help-buffer (get-buffer "*Animal-Help*"))
  119.       (if animal-help-buffer
  120.       nil
  121.     (setq animal-help-buffer (generate-new-buffer "*Animal-Help*")))
  122.       (set-buffer animal-help-buffer)
  123.       (erase-buffer)
  124.       (insert buffer-string)
  125.       (goto-char (point-min))
  126.       (display-buffer animal-help-buffer)
  127.       (if (not (null prompt-string))
  128.       (read-string prompt-string)))))
  129.