-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbulls_n_cows.py
executable file
·67 lines (57 loc) · 2.01 KB
/
bulls_n_cows.py
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
#!/usr/bin/env python
import sys
class Game(object):
def __init__(self, master_type, guesser_type, guess_len=4):
try:
self.master = master_type(guess_len)
except TypeError:
if guess_len == 4:
self.master = master_type()
else:
raise ValueError("Master doesn't support lengths other than 4")
try:
self.guesser = guesser_type(guess_len)
except TypeError:
if guess_len == 4:
self.guesser = guesser_type()
else:
raise ValueError("Master doesn't support lengths other than 4")
self.guess_len = guess_len
self.last_answer = None
def __iter__(self):
return self
def next(self):
if self.last_answer is None:
guess = self.guesser.guess()
else:
if self.last_answer[0] == self.guess_len:
raise StopIteration
guess = self.guesser.guess(*self.last_answer)
answer = self.master.check(guess)
self.last_answer = answer
return guess, answer
class Guesser(object):
def __init__(self, guess_len=4):
self.guess_len = guess_len
def guess(self, bulls=None, cows=None):
raise NotImplementedError
class Master(object):
def __init__(self, guess_len=4):
self.guess_len = guess_len
def check(self, guess):
raise NotImplementedError
def load_class(cls_path):
module_name, class_name = cls_path.rsplit('.', 1)
__import__(module_name)
module = sys.modules[module_name]
return getattr(module, class_name)
if __name__ == '__main__':
if len(sys.argv) != 3:
print >>sys.stderr, \
"Syntax: bulls_n_cows.py module.MasterType module.GuesserType"
sys.exit(1)
master_type = load_class(sys.argv[1])
guesser_type = load_class(sys.argv[2])
game = Game(master_type, guesser_type)
for guess, (bulls, cows) in game:
print "Guess: %s Bulls: %s Cows: %s" % (guess, bulls, cows)