-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoggle.py
More file actions
241 lines (173 loc) · 6.06 KB
/
Boggle.py
File metadata and controls
241 lines (173 loc) · 6.06 KB
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
"""
Boggle Game and Solver
"""
from random import randint
from itertools import product
from multiprocessing import Pool
from functools import partial
# Puts the dict into a set and a list.
# Set allows for O(1) lookup.
# List allows for use of indexes to split it up for multiprocessing later.
with open('boggleDict.txt', 'r') as wordstxt:
words_set = frozenset([x[:-1] for x in wordstxt.readlines()])
words_lst = list(words_set)
def roll_boggle_die():
"""generates a list of random letters
returns a list of random letters"""
dieSides = ['AAEEGN',
'ELRTTY',
'AOOTTW',
'ABBJOO',
'EHRTVW',
'CIMOTU',
'DISTTY',
'EIOSST',
'DELRVY',
'ACHOPS',
'HIMNQU',
'EEINSU',
'EEGHNW',
'AFFKPS',
'HLNNRZ',
'DELIRX']
randIntList = []
for x in range(16):
randIntList.append(randint(0, 5))
dieSidesGame = []
for x in range(16):
dieSidesGame.append(dieSides[x][randIntList[x]])
return dieSidesGame
def order_letters(listToOrder):
""" orders a list of random letters to form a boggle board
no return"""
for x in range(3, 19, 4):
print(listToOrder[x - 3] + ' ' + listToOrder[x - 2] + ' ' + listToOrder[x - 1] + ' ' + listToOrder[x])
def find_score(listOfWords):
"""returns a list of scores based on a list of words
listOfWords --> totalScores"""
scores = {3: 1, 4: 1, 5: 2, 6: 3, 7: 5, 8: 11, 9: 11, 10: 11, 11: 11, 12: 11, 13: 11, 14: 11, 15: 11, 16: 11}
totalScores = []
for word in listOfWords:
totalScores.append(scores[len(word)])
return totalScores
def spot_adjacent(spot1, spot2):
"""checks if 2 spots are adjacent
returns True or False"""
adjacentSpots = {1: [2, 5, 6],
2: [1, 3, 5, 6, 7],
3: [8, 2, 4, 6, 7],
4: [8, 3, 7],
5: [1, 2, 10, 6, 9],
6: [1, 2, 3, 5, 7, 9, 10, 11],
7: [2, 3, 4, 6, 8, 10, 11, 12],
8: [11, 3, 4, 12, 7],
9: [10, 13, 5, 6, 14],
10: [5, 6, 7, 9, 11, 13, 14, 15],
11: [6, 7, 8, 10, 12, 14, 15, 16],
12: [8, 16, 11, 15, 7],
13: [9, 10, 11],
14: [9, 10, 11, 13, 15],
15: [16, 10, 11, 12, 14],
16: [11, 12, 15]}
if spot2 in adjacentSpots[spot1]:
return True
else:
return False
def index_letter(letter, gridx):
"""returns the spots in which a letter is in a grid
returns -1 if not in grid"""
letterInGrid = []
for x in range(gridx.count(letter)):
letterInGrid.append(gridx.index(letter))
gridx.insert(gridx.index(letter), 0)
gridx.remove(letter)
if letterInGrid:
for n, i in enumerate(gridx):
if i == 0:
gridx[n] = letter
return letterInGrid
else:
return -1
def valid_path(path):
"""checks if a patha of spots in a grid are valid
returns True or False"""
for i, spot in enumerate(path):
if path.count(spot) >= 2:
return False
elif i + 1 == len(path):
continue
elif not spot_adjacent(spot + 1, path[i + 1] + 1):
return False
return True
def word_in_grid(word, grid):
"""checks if a word is in a boggle grid
returns True or False"""
indexWord = []
word = word.upper()
for letter in word:
if index_letter(letter, grid) == -1:
return False
indexWord.append(index_letter(letter, grid))
for items in product(*indexWord):
if valid_path(items):
return True
return False
def in_list(word):
"""checks if a word is in a file
returns True or False"""
word = word.lower()
if word in words_set:
return True
return False
def find_longest_word(letterList):
"""Return the longest valid word for the given board.
If no valid words exist an empty string is returned instead of raising a
``ValueError``.
"""
valid_words = all_valid_words(letterList)
if not valid_words:
return ""
return max(valid_words, key=len)
def all_valid_words(letterList):
with Pool(3) as p:
results = p.map(partial(word_in_grid, grid=letterList), words_lst)
return [s[1] for s in list(filter(lambda s: results[s[0]], enumerate(words_lst)))]
def play_boggle():
"""plays a game of boggle"""
letterList = roll_boggle_die()
wordsFound = []
End = False
while not End:
order_letters(letterList)
wordToCheck = input("Enter your word(leave blank to quit) : ").upper()
if wordToCheck == "":
End = True
continue
elif len(wordToCheck) < 3:
print("Your word must be at least 3 letters long.")
continue
elif not word_in_grid(wordToCheck, letterList):
print(wordToCheck + " is not on the grid.")
continue
elif not in_list(wordToCheck):
print(wordToCheck + " is not a valid word.")
continue
elif wordToCheck in wordsFound:
print(wordToCheck + " has been found.")
continue
else:
print(wordToCheck + " is a valid word!")
wordsFound.append(wordToCheck)
scores = find_score(wordsFound)
total = 0
for i, word in enumerate(wordsFound):
print(word + " is worth " + str(scores[i]))
total += scores[i]
print("Your total score is " + str(total))
print("I bet I can find the longest word...")
longest_word = find_longest_word(letterList)
print("I found " + longest_word + "!")
print("You found " + str(len(wordsFound)) + " out of " + str(len(all_valid_words(letterList))) + " possible words!")
print("THANKS FOR PLAYING!")
if __name__ == "__main__":
play_boggle()