Public » Miscellaneous » Twenty
Clone URL:  
Pushed to one repository · View In Graph Contained in tip

initial

Changeset 980cf9163ffd

by Benjamin Pollack

Changes to one file · Browse files at 980cf9163ffd Diff from another changeset...

Change 1 of 1 Show Entire File twenty.py Stacked
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
@@ -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()