home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / pyth_os2.zip / python-1.0.2 / Demo / stoffel / Animal.py < prev    next >
Text File  |  1992-09-20  |  4KB  |  132 lines

  1. # Animal.Py: translated from Logo (92/9)
  2.  
  3. import string
  4.  
  5. def Explain():
  6.   Clr_scr()
  7.   print 'In this game, the computer accumulates'
  8.   print 'knowledge about animals by trying to'
  9.   print 'guess what animal you are thinking of.'
  10.   print 'If it doesn\'t know, it will ask you to'
  11.   print 'teach it something and incorporate'
  12.   print 'this knowledge in the next stage of'
  13.   print 'the game.'
  14.   print
  15.   print 'When you start the game, the computer'
  16.   print 'already knows about a few animals.'
  17.   print
  18.  
  19.  
  20. def Initialize_knowledge():
  21.   if 'yes' == Ask_yes_or_no('Do you want to start from scratch?'):
  22.     Knowledge = 'hippo'
  23.   else:
  24.     file = open('Animal.Lst', 'r')
  25.     Knowledge = eval(file.read())
  26.     file.close()
  27.     #--print 'Read in from disk:\n', Knowledge #Temp
  28.   return Knowledge
  29.  
  30.  
  31. def Save_the_data():
  32.   global Knowledge
  33.   print '\nInfo:\n', Knowledge  # debug
  34.   if 'yes' == Ask_yes_or_no('Do you want to save the info to disk?'):
  35.     file = open('Animal.Lst', 'w')
  36.     file.write(`Knowledge`)
  37.     file.close()
  38.     print 3*'\t', '>>> 1 list/string written to disk <<<'
  39.  
  40.  
  41. def Play_animal():
  42.   global Knowledge
  43.   Clr_scr()
  44.   print
  45.   print 'Think of an animal...\n'
  46.   print 'I will try to guess it by asking questions.'
  47.   print
  48.   Guess(Knowledge)
  49.   if 'yes' == Ask_yes_or_no('Do you want to try again?'):
  50.     Play_animal()
  51.   else:
  52.     print '\256\256\256 Ok, till some other time then. \257\257\257'
  53.  
  54.  
  55. def Guess(choices):
  56.   if type(choices) == type(''):
  57.      Final_guess(choices)
  58.      return 
  59.   response =  Ask_yes_or_no(Next_question(choices))
  60.   if response == 'yes':
  61.     Guess(Yes_branch(choices))
  62.     return
  63.   Guess(No_branch(choices))
  64.  
  65.  
  66. def Next_question(tree):  return first(tree)
  67. def No_branch(tree):      return last(tree)
  68. def Yes_branch(tree):     return tree[1]  # 2nd elt
  69.  
  70.  
  71. def Ask_yes_or_no(question):
  72.   resp  =  raw_input('\n'+question+' ')
  73.   if first(resp) in 'yY': return 'yes'
  74.   if first(resp)  in 'nN': return 'no'
  75.   print 'Please type yes or no: ',  # (in lowercase)?
  76.   return Ask_yes_or_no(question)
  77.  
  78.  
  79. def Final_guess(choice):
  80.   final_question =  'Is it '+ Add_a_or_an(choice)+'?'
  81.   response =  Ask_yes_or_no(final_question)
  82.   if response == 'yes':
  83.     print 3*'\t', '\257\257\257\257\257\257\257  Look how smart I am! \n'
  84.     return
  85.   Get_smarter(choice)
  86.  
  87.  
  88. def Add_a_or_an(word):
  89.   if first(word) in 'aeiou':
  90.     return ('an '+word)
  91.   else:
  92.     return ('a '+word)
  93.  
  94.  
  95. def Get_smarter(Wrong_answer):
  96.   Right_answer = raw_input('\nOh well, I was wrong. What was it? ')
  97.   if Right_answer[:2] == 'a ': Right_answer = Right_answer[2:]
  98.   elif Right_answer[:3] == 'an ': Right_answer = Right_answer[3:]
  99.   print '\nPlease type in a question whose answer is'
  100.   print 3*' ', vdu_bold('yes'), 'for', Add_a_or_an(Right_answer), 'and'
  101.   print 3*' ', vdu_bold('no'), 'for', Add_a_or_an(Wrong_answer)+':'
  102.   question = raw_input()
  103.   question = string.upper(question[0]) + question[1:]
  104.   if last(question) != '?': question = question + '?'
  105.   Expand_knowledge(question, Right_answer, Wrong_answer)
  106.  
  107.  
  108. def Expand_knowledge(New_question, Right_answer, Wrong_answer):
  109.   global Knowledge
  110.   Knowledge = Replace(Knowledge, Wrong_answer, \
  111.                       [New_question,Right_answer,Wrong_answer])
  112.   print '\n', Knowledge  # debug
  113.  
  114.  
  115. def Replace(Data, word, New_branch):
  116.   if Data == word: return New_branch
  117.   if type(Data) == type(''): return Data
  118.   return [Next_question(Data), Replace(Yes_branch(Data), word, New_branch), \
  119.                                Replace( No_branch(Data), word, New_branch)]
  120.  
  121.  
  122. def first(x):  return x[0]
  123. def last(x):   return x[-1]
  124. def Clr_scr():     print  '\033[2J',
  125. def vdu_bold(s):   return '\033[1m'+s+'\033[0m'
  126.  
  127. Explain()
  128. Knowledge = Initialize_knowledge()
  129. Play_animal()
  130. Save_the_data()
  131.  
  132.