-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.py
74 lines (62 loc) · 2.07 KB
/
game.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
68
69
70
71
72
73
74
"""
Plays the 'pick a 4 letter word starting with the last letter of my word'
game with you.
Just run the program.
"""
class Game(object):
def __init__(self, word_length=4):
self.word_length = word_length
self.chain = []
self.used = set()
self.dictionary = self._load_words()
def _letter(self):
if not self.chain:
return None
return self.chain[-1][-1]
def _load_words(self):
return {word
for word in open('words.txt').read().split()
if len(word) == self.word_length}
def play(self):
while True:
self.your_turn()
self.my_turn()
def _is_valid(self, word):
if len(word) != self.word_length:
print "Not a %d-letter word." % self.word_length
elif word in self.used:
print "Already used that word."
elif word not in self.dictionary:
print "Not a word."
elif self._letter() and word[0] != self._letter():
print "That doesn't start with '%s'." % self._letter()
else:
return True
return False
def your_turn(self):
while True:
if not self._letter():
prompt = "Enter any %d-letter word: " % self.word_length
else:
prompt = "Enter a %d-letter '%s' word: " % (
self.word_length, self._letter().upper())
guess = raw_input(prompt).lower()
if self._is_valid(guess):
self.chain.append(guess)
self.used.add(guess)
return
print
def my_turn(self):
letter = self._letter()
for word in self.dictionary:
if word.startswith(letter) and word not in self.used:
self.chain.append(word)
self.used.add(word)
print "My word: %s." % word
return
print "I can't find a word starting with '%s'. You win!" % letter
def main():
game = Game(4)
game.play()
if __name__ == '__main__':
main()