home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / max / max130 / animal.max next >
Text File  |  1988-10-30  |  5KB  |  203 lines

  1.  
  2. ; This program is based (loosely) on a program
  3. ; that was available on Dartmouth Time Sharing.
  4.  
  5. tclear ()
  6.  
  7. $replace ("TRUE"        "1")
  8. $replace ("FALSE"       "0")
  9. $replace ("ANIMAL_FILE" "animal.lst")
  10.  
  11. put ("loading animal()")
  12.  
  13. Function animal
  14.     Begin
  15.         print_usage ()
  16.         set (ld restore_knowledge ())
  17.         While
  18.             more_questions (ld)
  19.         EndWhile
  20.         save_knowledge (ld)
  21.     End
  22. EndFunction
  23.  
  24. put ("loading more_questions()")
  25.  
  26. Function more_questions
  27.     Begin
  28.         set (ld argv (1))
  29.         ask_question (ld)
  30.         If get_response ()
  31.             Then
  32.                 return (process_yes_response (ld))
  33.             EndThen
  34.             Else
  35.                 return (process_no_response (ld))
  36.             EndElse
  37.         EndIf
  38.     End
  39. EndFunction
  40.  
  41. put ("loading ask_question()")
  42.  
  43. Function ask_question
  44.     Begin
  45.         set (ld argv (1))
  46.         set (question lstcget (ld))
  47.         If ne (strsub (question strlen (question)) "?")
  48.             Then
  49.                 put (question "?")
  50.             EndThen
  51.             Else
  52.                 put (question)
  53.             EndElse
  54.         EndIf
  55.     End
  56. EndFunction
  57.  
  58. put ("loading get_response()")
  59.  
  60. Function get_response
  61.     Begin
  62.         Loop
  63.             get (response)
  64.             set (response struc (strsub (response 1 1)))
  65.             Case response
  66.                 When "Y"
  67.                     Do
  68.                         return (TRUE)
  69.                     EndDo
  70.                 EndWhen
  71.                 When "N"
  72.                     Do
  73.                         return (FALSE)
  74.                     EndDo
  75.                 EndWhen
  76.                 Default
  77.                     Do
  78.                         put ("Please answer yes or no.")
  79.                     EndDo
  80.                 EndDefault
  81.             EndCase
  82.         EndLoop
  83.     End
  84. EndFunction
  85.  
  86. put ("loading process_yes_response()")
  87.  
  88. Function process_yes_response
  89.     Begin
  90.         set (ld argv (1))
  91.         If lstnval (ld)
  92.             Then
  93.                 lstnext (ld)
  94.                 return (TRUE)
  95.             EndThen
  96.             Else
  97.                 put ("I guessed it.")
  98.                 return (FALSE)
  99.             EndElse
  100.         EndIf
  101.     End
  102. EndFunction
  103.  
  104. put ("loading process_no_response()")
  105.  
  106. Function process_no_response
  107.     Begin
  108.         set (ld argv (1))
  109.         If lstsval (ld)
  110.             Then
  111.                 lstsub (ld)
  112.                 return (TRUE)
  113.             EndThen
  114.             Else
  115.                 get_new_question (ld)
  116.                 return (FALSE)
  117.             EndElse
  118.         EndIf
  119.     End
  120. EndFunction
  121.  
  122. put ("loading get_new_question()")
  123.  
  124. Function get_new_question
  125.     Begin
  126.         set (ld argv (1))
  127.         put ("I give up.")
  128.         put ("What animal did you have in mind?")
  129.         get (animal_name)
  130.         put ("Enter a question that has a yes answer for")
  131.         put ("   " animal_name)
  132.         put ("and distinguishes it from other animals.")
  133.         get (animal_question)
  134.         lstspsh (ld animal_question)
  135.         set (animal_question format ("Is it a %s?", animal_name))
  136.         lstcpsh (ld animal_name)
  137.     End
  138. EndFunction
  139.  
  140. put ("loading restore_knowledge()")
  141.  
  142. Function restore_knowledge
  143.     Begin
  144.         set (fd open (ANIMAL_FILE "r"))
  145.         If lt (fd 0)
  146.             Then
  147.                 set (ld initialize_list ())
  148.             EndThen
  149.             Else
  150.                 set (ld lstread (fd))
  151.             EndElse
  152.         EndIf
  153.         close (fd)
  154.         lsthead (ld)
  155.         return (ld)
  156.     End
  157. EndFunction
  158.  
  159. put ("loading save_knowledge()")
  160.  
  161. Function save_knowledge
  162.     Begin
  163.         set (ld argv (1))
  164.         set (fd open (ANIMAL_FILE "wc"))
  165.         If lt (fd 0)
  166.             Then
  167.                 put ("Cannot open " ANIMAL_FILE " for writing.")
  168.             EndThen
  169.             Else
  170.                 lstwrite (ld fd)
  171.                 close (fd)
  172.             EndElse
  173.         EndIf
  174.         lstfre (ld)
  175.     End
  176. EndFunction
  177.  
  178. put ("loading initialize_list()")
  179.  
  180. Function initialize_list
  181.     Begin
  182.         set (ld lstcre ("Does it have wings?" "Is it a bird?"))
  183.         return (ld)
  184.     End
  185. EndFunction
  186.                               
  187. put ("loading print_usage()")
  188.  
  189. Function print_usage
  190.     Begin
  191.         put ("Think of an animal. I will ask simple yes or no")
  192.         put ("questions and try to guess it. If I can't guess it")
  193.         put ("then I will give up and ask you what your animal was")
  194.         put ("and what distinguishes it from other animals. I will")
  195.         put ("remember that information and use it next time.")
  196.         put ("\nWould you like to continue?")
  197.         If not (get_response ())
  198.             Then
  199.                 exit ()
  200.             EndThen
  201.         EndIf
  202.     End
  203. EndFunction