home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / progmisc / euphor10.zip / ANIMAL.EX < prev    next >
Text File  |  1993-05-26  |  3KB  |  105 lines

  1.             ----------------------
  2.             -- GUESS THE ANIMAL --
  3.             ----------------------
  4. constant 
  5.     node_type = 1,
  6.        DESCRIPTION = 1,
  7.        ANIMAL = 2,
  8.     question = 2,
  9.     when_true = 3,
  10.     when_false = 4,
  11.     TRUE = 1,
  12.     FALSE = 0
  13.  
  14. object database, p, prev_p, prev_answer
  15.  
  16. function Yes()  -- returns TRUE if the answer is yes
  17.     object answer
  18.  
  19.     answer = '?'
  20.     while answer = '?' do
  21.     answer = gets(0)
  22.     if length(answer) > 0 then
  23.         if answer[1] = 'y' or answer[1] = 'Y' then
  24.         answer = TRUE
  25.         elsif answer[1] = 'n' or answer[1] = 'N' then
  26.         answer = FALSE
  27.         else 
  28.         answer = '?'
  29.         end if
  30.     else
  31.         answer = '?'   
  32.     end if
  33.     if answer = '?' then
  34.         puts(1, "\nPlease answer y or n: ")
  35.     end if
  36.     end while
  37.     return answer
  38. end function
  39.  
  40.  
  41. procedure guess()  -- narrows it down to one animal
  42.     while 1 do
  43.     if database[p][node_type] = ANIMAL then
  44.         return
  45.     end if
  46.     printf(1, "\n%s? ", {database[p][question]})
  47.     prev_p = p
  48.     if Yes() then
  49.         p = database[p][when_true]
  50.         prev_answer = when_true
  51.     else 
  52.         p = database[p][when_false]
  53.         prev_answer = when_false 
  54.     end if
  55.     end while
  56. end procedure
  57.  
  58. global procedure animal()
  59. -- updates database of animal information
  60.  
  61.     object new, new_question, correct, answer
  62.  
  63.     -- initial database:
  64.     database = {{DESCRIPTION, "Can it fly", 2, 3},
  65.         {ANIMAL,      "an eagle"},
  66.         {ANIMAL,      "a dog"   }}
  67.     while 1 do
  68.     p = 1    
  69.     printf(1, "%s\n", 
  70.       {"\nThink of an animal. Hit <cr> when you are ready, q to quit"})
  71.     answer = gets(0)
  72.     if length(answer) > 0 then
  73.         if answer[1] = 'q' then
  74.         return
  75.         end if
  76.     end if
  77.     guess()
  78.     printf(1, "\nIs it %s? ", {database[p][question]})
  79.     if not Yes() then
  80.         puts(1, "\nI give up. What was it? ")
  81.         correct = gets(0)
  82.         correct = correct[1..length(correct)-1]
  83.         database = append(database, {ANIMAL, correct})
  84.         new = length(database)
  85.         printf(1, "\n%s%s%s%s\n", 
  86.         {"Please give me a question that would distinguish ",
  87.         database[p][question], " from ", correct})
  88.         new_question = gets(0)
  89.         new_question = new_question[1..length(new_question)-1]
  90.         if new_question[length(new_question)] = '?' then
  91.         new_question = new_question[1..length(new_question)-1]
  92.         end if
  93.         printf(1, "For %s the answer would be: ", {correct})
  94.         if Yes() then
  95.         database = append(database, {DESCRIPTION, new_question, new, p})
  96.         else
  97.         database = append(database, {DESCRIPTION, new_question, p, new})
  98.         end if
  99.         database[prev_p][prev_answer] = length(database)
  100.     end if
  101.     end while
  102. end procedure
  103.  
  104. animal()
  105.