home *** CD-ROM | disk | FTP | other *** search
/ Power-Programmierung / CD1.mdf / euphoria / animal.ex < prev    next >
Text File  |  1994-01-31  |  3KB  |  110 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.  
  12. constant
  13.     TRUE = 1,
  14.     FALSE = 0
  15.  
  16. constant KEYBOARD = 0, SCREEN = 1
  17.  
  18. object database, p, prev_p, prev_answer
  19.  
  20. function Yes()  -- returns TRUE if the answer is yes
  21.     object answer
  22.  
  23.     answer = '?'
  24.     while answer = '?' do
  25.     answer = gets(KEYBOARD)
  26.     if length(answer) > 0 then
  27.         if answer[1] = 'y' or answer[1] = 'Y' then
  28.         answer = TRUE
  29.         elsif answer[1] = 'n' or answer[1] = 'N' then
  30.         answer = FALSE
  31.         else 
  32.         answer = '?'
  33.         end if
  34.     else
  35.         answer = '?'   
  36.     end if
  37.     if answer = '?' then
  38.         puts(SCREEN, "\nPlease answer y or n: ")
  39.     end if
  40.     end while
  41.     return answer
  42. end function
  43.  
  44. procedure guess()  -- narrows it down to one animal
  45.     while TRUE do
  46.     if database[p][node_type] = ANIMAL then
  47.         return
  48.     end if
  49.     printf(SCREEN, "\n%s? ", {database[p][question]})
  50.     prev_p = p
  51.     if Yes() then
  52.         p = database[p][when_true]
  53.         prev_answer = when_true
  54.     else 
  55.         p = database[p][when_false]
  56.         prev_answer = when_false 
  57.     end if
  58.     end while
  59. end procedure
  60.  
  61. global procedure animal()
  62. -- updates database of animal information
  63.  
  64.     object new, new_question, correct, answer
  65.  
  66.     -- initial database:
  67.     --           record type     text         Y  N
  68.     database = {{DESCRIPTION, "\tCan it fly", 2, 3},
  69.         {ANIMAL,      "an eagle"},
  70.         {ANIMAL,      "a dog"   }}
  71.     while TRUE do
  72.     p = 1    
  73.     printf(SCREEN, "%s\n", 
  74.       {"\nThink of an animal. Hit <cr> when you are ready, q to quit"})
  75.     answer = gets(KEYBOARD)
  76.     if length(answer) > 0 then
  77.         if answer[1] = 'q' then
  78.         return
  79.         end if
  80.     end if
  81.     guess()
  82.     printf(SCREEN, "\n\tIs it %s? ", {database[p][question]})
  83.     if not Yes() then
  84.         puts(SCREEN, "\nI give up. What was it? ")
  85.         correct = gets(KEYBOARD)
  86.         correct = correct[1..length(correct)-1]
  87.         database = append(database, {ANIMAL, correct})
  88.         new = length(database)
  89.         printf(SCREEN, "\n%s%s%s%s\n", 
  90.         {"Please give me a question that would distinguish ",
  91.         database[p][question], " from ", correct})
  92.         new_question = '\t' & gets(KEYBOARD)
  93.         new_question = new_question[1..length(new_question)-1]
  94.         if new_question[length(new_question)] = '?' then
  95.         new_question = new_question[1..length(new_question)-1]
  96.         end if
  97.         printf(SCREEN, "\nFor %s the answer would be: ", {correct})
  98.         if Yes() then
  99.         database = append(database, {DESCRIPTION, new_question, new, p})
  100.         else
  101.         database = append(database, {DESCRIPTION, new_question, p, new})
  102.         end if
  103.         database[prev_p][prev_answer] = length(database)
  104.     end if
  105.     end while
  106. end procedure
  107.  
  108. animal()
  109.  
  110.