-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinal.py
More file actions
406 lines (341 loc) · 11.8 KB
/
final.py
File metadata and controls
406 lines (341 loc) · 11.8 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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
'''
Alexander Sumner
FSUID: acs14k
'''
import enchant
import random
from copy import copy
from PyQt5 import QtWidgets, QtCore, QtGui
import time
import sys
import shelve
try:
import cPickle as pickle
except:
import pickle
#create the dictionary to search for valid words
global d
d = enchant.Dict("en_US")
#make a list to contain all die for the board
global BaseDice
BaseDice = []
#Dice one
BaseDice.append(["A","E","A","N","E","G"])
#Dice two
BaseDice.append(["A","H","S","P","C","O"])
#Dice three
BaseDice.append(["A","S","P","F","F","K"])
#Dice four
BaseDice.append(["O","B","J","O","A","B"])
#Dice five
BaseDice.append(["I","O","T","M","U","C"])
#Dice six
BaseDice.append(["R","Y","V","D","E","L"])
#Dice seven
BaseDice.append(["L","R","E","I","X","D"])
#Dice eight
BaseDice.append(["E","I","U","N","E","S"])
#Dice nine
BaseDice.append(["W","N","G","E","E","H"])
#Dice ten
BaseDice.append(["L","N","H","N","R","Z"])
#Dice eleven
BaseDice.append(["T","S","T","I","Y","D"])
#Dice twelve
BaseDice.append(["O","W","T","O","A","T"])
#Dice thirteen
BaseDice.append(["E","R","T","T","Y","L"])
#Dice fourteen
BaseDice.append(["T","O","E","S","S","I"])
#Dice fifteen
BaseDice.append(["T","E","R","W","H","V"])
#Dice sixteen
BaseDice.append(["N","U","I","H","M","Qu"])
#list of items that can seach to the top left for another dice
topleftable = [5,6,7,9,10,11,13,14,15]
#list of items that can seach to the top right for another dice
toprightable = [4,5,6,8,9,10,12,13,14]
#list of items that can seach to the top for another dice
topable = [4,5,6,7,8,9,10,11,12,13,14,15]
#list of items that can seach to the left for another dice
leftable = [1,2,3,5,6,7,9,10,11,13,14,15]
#list of items that can seach to the right for another dice
rightable = [0,1,2,4,5,6,8,9,10,12,13,14]
#list of items that can seach to the bottom left for another dice
bottomleftable = [1,2,3,5,6,7,9,10,11]
#list of items that can seach down for another dice
bottomable = [0,1,2,3,4,5,6,7,8,9,10,11]
#list of items that can seach to the bottom right for another dice
bottomrightable = [0,1,2,4,5,6,8,9,10]
class Die(QtWidgets.QLabel):
def __init__(self, parent, label):
QtWidgets.QLabel.__init__(self, parent)
self.myletter = label
self.setText(self.myletter)
self.setAlignment(QtCore.Qt.AlignCenter)
self.thefont = QtGui.QFont("Times", 32, QtGui.QFont.Bold)
self.setFont(self.thefont)
self.setMinimumSize(120,120)
class DiceBoard(QtWidgets.QWidget):
def __init__(self, parent, dice = None, foundwords = None):
QtWidgets.QWidget.__init__(self,parent)
self.parent = parent
self.mydice = []
self.grid = QtWidgets.QGridLayout()
self.setLayout(self.grid)
if foundwords == None:
self.alreadyfoundwords = []
else:
self.alreadyfoundwords = foundwords
if dice == None:
self.mydice = self.generateDice()
else:
self.mydice = dice
self.buildBoard(self.mydice)
def generateDice(self):
random.shuffle(BaseDice)
dummyboard = []
for x in range(0,16):
dummyboard.append(random.choice(BaseDice[x]))
return dummyboard
def buildBoard(self, dice):
x,y = 1,1
for each in dice:
if x == 5:
x = 1
y = y + 1
self.grid.addWidget(Die(self,each),x,y,1,1)
x = x + 1
def find_points(self, word):
if (not d.check(word)):
return 0
elif len(word) < 3:
return 0
elif len(word) == 3 or len(word) == 4:
return 1
elif len(word) == 5:
return 2
elif len(word) == 6:
return 3
elif len(word) == 7:
return 5
else:
return 11
def scan_board(self, word):
#each letter we need to find
letters = []
#the location of each found letter
used = []
#populate the letters list with each individual letter form the word
qfound = 0
for i in word:
if qfound and i == "U":
qfound = 0
continue
if i == "Q":
letters.append("Qu")
qfound = 1
else:
letters.append(i)
for i in range(0,16):
if self.mydice[i] == letters[0]:
if self.look_letter(i,letters[1:],copy(used)):
return 1
return 0
#look around the place parameter for the next letter in the word
def look_letter(self, place, findletters, usedplaces):
#if no more letters to look for return true
if not findletters:
return 1
#label the current place as used to not be able to use it again
usedplaces.append(place)
#use the top left space to continue finding the word
if (place in topleftable) and ((place - 5) not in usedplaces) and (self.mydice[place-5] == findletters[0]):
if self.look_letter(place-5,findletters[1:],copy(usedplaces)):
return 1
#use the top space to continue finding the word
if (place in topable) and ((place - 4) not in usedplaces) and (self.mydice[place-4] == findletters[0]):
if self.look_letter(place-4,findletters[1:],copy(usedplaces)):
return 1
#use the top right space to continue finding the word
if (place in toprightable) and ((place - 3) not in usedplaces) and (self.mydice[place-3] == findletters[0]):
if self.look_letter(place-3,findletters[1:],copy(usedplaces)):
return 1
#use the left space to continue finding the word
if (place in leftable) and ((place - 1) not in usedplaces) and (self.mydice[place-1] == findletters[0]):
if self.look_letter(place-1,findletters[1:],copy(usedplaces)):
return 1
#use the right space to continue finding the word
if (place in rightable) and ((place + 1) not in usedplaces) and (self.mydice[place+1] == findletters[0]):
if self.look_letter(place+1,findletters[1:],copy(usedplaces)):
return 1
#use the bottom left space to continue finding the word
if (place in bottomleftable) and ((place + 3) not in usedplaces) and (self.mydice[place+3] == findletters[0]):
if self.look_letter(place+3,findletters[1:],copy(usedplaces)):
return 1
#use the bottom space to continue finding the word
if (place in bottomable) and ((place + 4) not in usedplaces) and (self.mydice[place+4] == findletters[0]):
if self.look_letter(place+4,findletters[1:],copy(usedplaces)):
return 1
#use the top right space to continue finding the word
if (place in bottomrightable) and ((place + 5) not in usedplaces) and (self.mydice[place+5] == findletters[0]):
if self.look_letter(place+5,findletters[1:],copy(usedplaces)):
return 1
#return False because the next letter wasnt found anywhere
return 0
def check_word(self, word):
if word in self.alreadyfoundwords:
return
if self.scan_board(word):
self.alreadyfoundwords.append(word)
self.parent.score = self.parent.score + self.find_points(word)
class WordTable(QtWidgets.QTextEdit):
def __init__(self, parent, words = None):
QtWidgets.QWidget.__init__(self,parent)
self.parent = parent
self.displayedwords = []
if words != None:
for each in words:
self.displayedwords.append(each)
self.addWord(each)
def addWord(self, word):
self.displayedwords.append(word)
self.parent.board.check_word(word.upper())
self.append(word)
class WordEntry(QtWidgets.QLineEdit):
def __init__(self, parent):
QtWidgets.QLineEdit.__init__(self,parent)
self.parent = parent
self.returnPressed.connect(self.getword)
def getword(self):
self.theword = self.text()
self.clear()
self.parent.wordtable.addWord(self.theword)
class Timer(QtWidgets.QLCDNumber):
def __init__(self, parent, starttime = 180):
QtWidgets.QLCDNumber.__init__(self, parent)
self.starttime = starttime
self.parent = parent
self.setup()
def setup(self):
self.timer = QtCore.QTimer(self)
self.timer.timeout.connect(self.displaytimer)
self.timer.start(1000)
self.currenttime = self.starttime
self.displaytimer()
def displaytimer(self):
self.display(self.currenttime)
if self.currenttime:
self.currenttime = self.currenttime - 1
else:
self.buttonreply = QtWidgets.QMessageBox.question(self, "final.py", "Time's Up!\nScore: "+str(self.parent.score)+"\nWould you like to play again?", QtWidgets.QMessageBox.Yes | QtWidgets.QMessageBox.No, QtWidgets.QMessageBox.No)
if self.buttonreply == QtWidgets.QMessageBox.Yes:
self.parent.NewGame()
else:
QtWidgets.qApp.quit()
class BoggleGame(QtWidgets.QWidget):
def __init__(self, parent):
QtWidgets.QWidget.__init__(self, parent)
self.grid = QtWidgets.QGridLayout()
self.setLayout(self.grid)
def NewGame(self):
self.clearlayout(self.grid)
self.score = 0
self.board = DiceBoard(self)
self.wordtable = WordTable(self)
self.timer = Timer(self)
self.wordentry = WordEntry(self)
self.grid.addWidget(self.board,1,1,10,5)
self.grid.addWidget(self.wordtable,1,6,10,4)
self.grid.addWidget(self.wordentry,11,1,1,8)
self.grid.addWidget(self.timer,11,9,1,1)
def SaveGame(self):
self.savedata = []
self.savedata.append(self.score)
self.savedata.append(self.board.mydice)
self.savedata.append(self.board.alreadyfoundwords)
self.savedata.append(self.wordtable.displayedwords)
self.savedata.append(self.timer.currenttime)
timeobj = QtCore.QDateTime.currentDateTime()
timekey = timeobj.toString()
s = shelve.open("game_save_data.db")
s[str(timekey)] = self.savedata
s.close()
def LoadGame(self, key):
self.clearlayout(self.grid)
s = shelve.open("game_save_data.db")
self.selectedkey = key
self.loadbox.deleteLater()
self.loadbox = None
self.score = s[self.selectedkey][0]
self.board = DiceBoard(self,s[self.selectedkey][1], s[self.selectedkey][2])
self.wordtable = WordTable(self, s[self.selectedkey][3])
self.timer = Timer(self, s[self.selectedkey][4])
self.wordentry = WordEntry(self)
self.grid.addWidget(self.board,1,1,10,5)
self.grid.addWidget(self.wordtable,1,6,10,4)
self.grid.addWidget(self.wordentry,11,1,1,8)
self.grid.addWidget(self.timer,11,9,1,1)
def clearlayout(self, layout):
for i in reversed(range(layout.count())):
widgetToRemove = layout.itemAt( i ).widget()
# remove it from the layout list
layout.removeWidget( widgetToRemove )
# remove it from the gui
widgetToRemove.setParent( None )
def ChooseLoadedGame(self):
s = shelve.open("game_save_data.db")
self.loadbox = LoadGamePopupBox(self, s.keys())
self.loadbox.itemClicked.connect(self.loadbox.Clicked)
self.loadbox.show()
class LoadGamePopupBox(QtWidgets.QListWidget):
def __init__(self, parent, keys):
QtWidgets.QListWidget.__init__(self)
self.parent = parent
self.thekeys = keys
self.click = 0
self.setWindowTitle("Select Save to Load")
for each in self.thekeys:
self.addItem(str(each))
def Clicked(self,item):
self.parent.LoadGame(str(item.text()))
class FirstLoadMessage(QtWidgets.QMessageBox):
def __init__(self):
QtWidgets.QMessageBox.__init__(self)
self.setText("Would you like to start a new game\n or load a saved game?")
self.setStandardButtons(QtWidgets.QMessageBox.Yes|QtWidgets.QMessageBox.No)
newbutton = self.button(QtWidgets.QMessageBox.Yes)
newbutton.setText("Start New Game")
loadbutton = self.button(QtWidgets.QMessageBox.No)
loadbutton.setText("Load Game")
class BoggleMainWindow(QtWidgets.QMainWindow):
def __init__(self):
QtWidgets.QMainWindow.__init__(self)
self.setup()
def setup(self):
self.setWindowTitle('Boggle')
self.bogglegame = BoggleGame(self)
self.setCentralWidget(self.bogglegame)
initialquestion = FirstLoadMessage()
reply = initialquestion.exec_()
if reply == QtWidgets.QMessageBox.Yes:
self.bogglegame.NewGame()
else:
self.bogglegame.ChooseLoadedGame()
new_game_action = QtWidgets.QAction('New', self)
new_game_action.triggered.connect(self.bogglegame.NewGame)
save_game_action = QtWidgets.QAction('Save', self)
save_game_action.triggered.connect(self.bogglegame.SaveGame)
load_game_action = QtWidgets.QAction('Load', self)
load_game_action.triggered.connect(self.bogglegame.ChooseLoadedGame)
menu_bar = self.menuBar()
menu_bar.setNativeMenuBar(False)
game_menu = menu_bar.addMenu('Game')
game_menu.addAction(new_game_action)
game_menu.addAction(save_game_action)
game_menu.addAction(load_game_action)
self.show()
if __name__ == "__main__":
app = QtWidgets.QApplication(sys.argv)
main_window = BoggleMainWindow()
app.exec_()