Changeset 980cf9163ffd…
by
Changes to one file · Browse files at 980cf9163ffd Diff from another changeset...
|
|
@@ -0,0 +1,124 @@ + #!/usr/bin/env python
+
+class Determiner(object):
+ def __init__(self, questioner, question, true_question, false_question):
+ self.questioner = questioner
+ self.question = question
+ self.true_question = true_question
+ self.false_question = false_question
+
+ def ask(self):
+ if ask(self.question):
+ self.questioner.advance_yes()
+ else:
+ self.questioner.advance_no()
+
+
+class Thing(object):
+ def __init__(self, questioner, what):
+ self.questioner = questioner
+ self.what = what
+
+ def ask(self):
+ if ask('Are you thinking of a %s?' % self.what):
+ self.questioner.mark_succeeded()
+ else:
+ self.questioner.mark_failed()
+
+
+class Questioner(object):
+ def __init__(self, database):
+ self.root = Determiner(self, 'Does it have four legs?', None, None)
+
+ def start(self):
+ self.current_question = self.root
+ self._solved = False
+ self._failed = False
+
+ def next_question(self):
+ return self.current_question
+
+ def advance_yes(self):
+ self.last_branch = True
+ self.last_question = self.current_question
+ self.current_question = self.current_question.true_question
+
+ def advance_no(self):
+ self.last_branch = False
+ self.last_question = self.current_question
+ self.current_question = self.current_question.false_question
+
+ def out_of_ideas(self):
+ return self.current_question is None or self._failed
+
+ def solved(self):
+ return self._solved
+
+ def learn_thing_and_question(self, question, thing):
+ true_answer = Thing(self, thing)
+ false_answer = self.last_question.true_question
+ new_question = Determiner(self, question, true_answer, false_answer)
+ if self.last_branch == True:
+ self.last_question.true_question = new_question
+ else:
+ self.last_question.false_question = new_question
+
+ def learn_thing(self, thing):
+ new_thing = Thing(self, thing)
+ if self.last_branch == True:
+ self.last_question.true_question = new_thing
+ else:
+ self.last_question.false_question = new_thing
+
+ def needs_clarification(self):
+ return self.current_question is not None
+
+ def mark_succeeded(self):
+ self._solved = True
+
+ def mark_failed(self):
+ self._failed = True
+
+ def best_guess(self):
+ return self.current_question.what
+
+def ask(question):
+ while True:
+ print question,
+ answer = raw_input().strip().lower()
+ if answer in ('y', 'yes', 'yeah', 'si'):
+ return True
+ elif answer in ('n', 'no', 'nope'):
+ return False
+ else:
+ print 'Please type "yes" or "no"'
+
+def play():
+ questioner = Questioner('questions.db')
+ questioner.start()
+ while True:
+ question = questioner.next_question()
+ question.ask()
+ if questioner.solved():
+ print 'I got it!'
+ if ask('Play again? '):
+ questioner.start()
+ else:
+ exit()
+ else:
+ if questioner.out_of_ideas():
+ print 'I give up. What were you thinking of?'
+ thing = raw_input()
+ if questioner.needs_clarification():
+ print 'What is a question whose answer is "true" for %s, but "false" for %s?' % (thing, questioner.best_guess())
+ question = raw_input()
+ questioner.learn_thing_and_question(question, thing)
+ else:
+ questioner.learn_thing(thing)
+ if ask('Play again?'):
+ questioner.start()
+ else:
+ exit()
+
+if __name__ == '__main__':
+ play()
|
Loading...