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

  1. Path: sparky!uunet!stanford.edu!rutgers!cbmvax!yoda!ag
  2. From: ag@yoda.omnicron.com (Keith Gabryelski)
  3. Newsgroups: gnu.emacs.sources
  4. Subject: Re: elisp `animal' program
  5. Message-ID: <1009@yoda.omnicron.com>
  6. Date: 24 Dec 92 17:52:53 GMT
  7. References: <1007@yoda.omnicron.com>
  8. Organization: Omnicron Data Systems
  9. Lines: 147
  10.  
  11. ag@yoda.omnicron.com (Keith Gabryelski) writes:
  12. > Probably written too many times in lisp and yet here I am posting this
  13. > one.  The ancient game of ``guess the animal''.
  14.  
  15. And yet here is some more hacks to the animal game.
  16.  
  17. Denis supplied the code to parse the question for `it' and possibly
  18. reverse the tree depending on which animal the user was describing.
  19.  
  20. He also supplied the code to add the articles `a' and `an' if not supplied
  21. to the animal name.
  22.  
  23. Pax, Keith
  24.  
  25. ;;;
  26. ;;;From: ag@yoda.omnicron.com (Keith Gabryelski)
  27. ;;;Date: 23 Dec 92 00:59:40 GMT
  28. ;;;
  29. ;;;Written for GZ (wherever she is these days).
  30. ;;;
  31. ;;;Various hacks from: Denis Howe <dbh@doc.ic.ac.uk>
  32. ;;;
  33.  
  34. (defvar animals-file "~/.animals" "Animal game data file")
  35.  
  36. (defvar animals-default-data '("a cat" nil nil) "Default data for animal game")
  37.  
  38. (defvar animals nil
  39.   "Animal game data.  A recursive list of the form
  40. (STRING TRUE-LIST FALSE_LIST) where TRUE-LIST and FALSE-LIST are lists of
  41. the this form and STRING is either a question posed to the user or a name
  42. of an animal.")
  43.  
  44. (defun animal ()
  45.   "Play the game \"Guess The Animal\" using the list \"animals\" as data."
  46.   (interactive)
  47.   (let (animal-buffer)
  48.     (save-window-excursion
  49.       (if (file-readable-p (expand-file-name animals-file))
  50.       (setq animal-buffer
  51.         (find-file-noselect (expand-file-name animals-file)))
  52.     (progn
  53.       (setq animal-buffer
  54.         (create-file-buffer (expand-file-name animals-file)))
  55.       (set-buffer animal-buffer)
  56.       (erase-buffer)
  57.       (insert (format "%s" animals-default-data))))
  58.       (set-buffer animal-buffer)
  59.       (goto-char (point-min))
  60.       (setq animals (read animal-buffer)))
  61.     (animal-read-string
  62.      "This is the game called animal where you think of an animal and I try to guess which one you are thinking of.
  63.  
  64. Please think of an animal and answer the questions I give you." nil)
  65.     (setq animals (parse-animal-tree animals))
  66.     (save-window-excursion
  67.       (set-buffer animal-buffer)
  68.       (erase-buffer)
  69.       (insert (format "%s" animals))
  70.       (write-file (expand-file-name animals-file)))))
  71.  
  72. (defun play-animal ()
  73.   "Play the games of animal lots of times"
  74.   (interactive)
  75.   (let (done)
  76.     (while (not done)
  77.       (progn
  78.     (animal)
  79.     (setq done (not (y-or-n-p "Would you like to play again? ")))))))
  80.  
  81. (defun parse-animal-tree (animal-list)
  82.   "Ask questions about a list (STRING TRUE-LIST FALSE-LIST) until we reach a
  83. terminal"
  84.   (if (animal-query-user (car animal-list) (null (car (cdr animal-list))))
  85.       (if (null (car (cdr animal-list)))
  86.       (progn 
  87.         (message "Thanks for playing.")
  88.         animal-list)            
  89.     (list (car animal-list)
  90.           (parse-animal-tree
  91.            (car (cdr animal-list))) (car (cdr (cdr animal-list)))))
  92.     (if (null (car (cdr (cdr animal-list))))
  93.     (animal-give-up animal-list)
  94.       (list (car animal-list)
  95.         (car (cdr animal-list))
  96.         (parse-animal-tree (car (cdr (cdr animal-list))))))))
  97.  
  98. (defun animal-query-user (question terminal)
  99.   "Query user about information on a particular animal"
  100.   (y-or-n-p (if terminal
  101.         (format "Is it %s? " question)
  102.           (format "%s? " question))))
  103.  
  104. (defun animal-give-up (animal-list)
  105.   "Given an animal list, query the user for information on a new type of
  106. animal"
  107.   (let (new-animal new-question)
  108.     (progn
  109.       (setq new-animal
  110.         (animal-read-string "I give up.  I need to know a little bit about the animal you are thinking of
  111. so I won't miss this one next time." "What animal where you thinking of? "))
  112.       (setq new-animal (animal-add-article new-animal))
  113.       (setq new-question
  114.         (animal-read-string
  115.          (format
  116.           "I also need to know how to ask a yes or no question that distinguishes \"%s\" from \"%s\".
  117.  
  118. Please type in such a question (ie, \"Does it have wings\" would be a suitable question to distinguish a bird from a cat)."
  119.           new-animal (car animal-list))  "Please type in a question: "))
  120.       (if (animal-query-user (animal-replace-it new-question new-animal) nil)
  121.       (list new-question (list new-animal nil nil) animal-list)
  122.     (list new-question animal-list (list new-animal nil nil))))))
  123.  
  124. (defun animal-read-string (buffer-string prompt-string)
  125.   "Open a buffer up, spew some helpul advice and accept a string"
  126.   (let (animal-help-buffer)
  127.     (progn
  128.       (setq animal-help-buffer (get-buffer "*Animal-Help*"))
  129.       (if animal-help-buffer
  130.       nil
  131.     (setq animal-help-buffer (generate-new-buffer "*Animal-Help*")))
  132.       (set-buffer animal-help-buffer)
  133.       (erase-buffer)
  134.       (insert buffer-string)
  135.       (fill-region (point-min) (point-max))
  136.       (goto-char (point-min))
  137.       (display-buffer animal-help-buffer)
  138.       (if (not (null prompt-string))
  139.       (read-string prompt-string)))))
  140.  
  141. (defun animal-replace-it (question animal)
  142.   "Replace the string `it' in QUESTION, if any, with ANIMAL."
  143.   (let ((it (string-match "it" question)))
  144.     (if it
  145.     (concat (substring question 0 it)
  146.         animal
  147.         (substring question (+ it 2)))
  148.       question)))
  149.  
  150. (defun animal-add-article (animal)
  151.   "Make sure ANIMAL starts with `a ' or 'an '."
  152.   (cond ((string-match "^a " (downcase animal)) animal)
  153.     ((string-match "^an " (downcase animal)) animal)
  154.     ((string-match "^the " (downcase animal)) animal)
  155.     ((string-match "^[A-Z]" animal) animal)
  156.     ((string-match "^[aeiou]" (downcase animal)) (concat "an " animal))
  157.     (t (concat "a " animal))))
  158.