-
Notifications
You must be signed in to change notification settings - Fork 0
/
scrabble-gui.py
executable file
·145 lines (124 loc) · 4.19 KB
/
scrabble-gui.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
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
#!/usr/bin/env python3
import sys
from PyQt4 import QtCore, QtGui, uic
import scrabble
import pickle
class ScrabbleGUI(QtGui.QMainWindow):
def __init__(self, parent=None, filename="data"):
QtGui.QMainWindow.__init__(self, parent)
# Load layout
uic.loadUi("scrabble-gui.ui", self)
# Create solver instance
self.solver = scrabble.Solver(scrabble.WORDSWITHFRIENDS)
# save file name
self.filename = filename
# Create tile backgrounds
for x in range(0, 15):
for y in range(0, 15):
self.board.setItem(y, x, QtGui.QTableWidgetItem(' '))
self.board.item(y, x).setTextAlignment(QtCore.Qt.AlignHCenter | QtCore.Qt.AlignVCenter)
self.board.item(y, x).setFont(QtGui.QFont("Sans", 10, QtGui.QFont.Bold))
# Double letter
if self.solver.letter_mult[y][x] == 2:
self.board.item(y, x).setBackground(QtGui.QColor(0, 0, 127, 100))
# Triple letter
elif self.solver.letter_mult[y][x] == 3:
self.board.item(y, x).setBackground(QtGui.QColor(0, 170, 0, 100))
# Double word
elif self.solver.word_mult[y][x] == 2:
self.board.item(y, x).setBackground(QtGui.QColor(170, 0, 0, 100))
# Triple word
elif self.solver.word_mult[y][x] == 3:
self.board.item(y, x).setBackground(QtGui.QColor(255, 170, 0, 100))
# Center
self.board.item(7, 7).setBackground(QtGui.QColor(0, 0, 0, 100))
# Event handlers
self.connect(self.solve_btn, QtCore.SIGNAL("clicked()"), self.solve)
self.connect(self.save_btn, QtCore.SIGNAL("clicked()"), self.save)
self.connect(self.load_btn, QtCore.SIGNAL("clicked()"), self.load)
self.load()
# Show window
self.show()
def add(self, text):
self.results.append(text)
def solve(self):
self.solve_btn.setEnabled(False)
self.solve_btn.setText("Solving...")
self.results.clear()
self.solver.blanks = []
for x in range(0, 15):
for y in range(0, 15):
char = self.board.item(y, x).text()
if char in "abcdefghijklmnopqrstuvwxyz":
self.solver.board[y][x] = char
elif char in "ABCDEFGHIJKLMNOPQRSTUVWXYZ":
self.solver.board[y][x] = char.lower()
self.solver.blanks.append((x,y))
else:
self.board.item(y, x).setText(' ')
self.solver.board[y][x] = ' '
# Start solver thread
self.thread = WorkThread(self.solver, self.letters.text())
self.connect( self.thread, QtCore.SIGNAL("update(QString)"), self.add )
self.connect( self.thread, QtCore.SIGNAL("finished()"), self.solve_done )
self.thread.start()
def solve_done(self):
self.solve_btn.setEnabled(True)
self.solve_btn.setText("Solve")
def save(self):
data = {
"board": self.solver.board,
"tiles": ""
}
for x in range(0, 15):
for y in range(0, 15):
char = self.board.item(y, x).text()
if char in "abcdefghijklmnopqrstuvwxyz":
data["board"][y][x] = char
else:
self.board.item(y, x).setText(' ')
data["board"][y][x] = ' '
data["tiles"] = self.letters.text()
# TODO choose save file
save_file = open(self.filename + ".pkl", 'wb')
pickle.dump(data, save_file)
save_file.close()
def load(self):
# TODO choose save file
try:
save_file = open(self.filename + ".pkl", 'rb')
data = pickle.load(save_file)
save_file.close()
self.letters.setText(data["tiles"])
for x in range(0, 15):
for y in range(0, 15):
self.board.item(y, x).setText(data["board"][y][x])
except:
print("Couldn't load file")
class WorkThread(QtCore.QThread):
def __init__(self, solver, tiles):
QtCore.QThread.__init__(self)
self.solver = solver
self.tiles = tiles
def __del__(self):
self.wait()
def run(self):
valid_words = self.solver.solve(self.tiles)
for word in valid_words:
if word[3] == scrabble.HORIZONTAL:
self.emit(QtCore.SIGNAL("update(QString)"),
"Horz: x: %2i y: %2i weight: %i score: %i word: %s" % (word[1]+1, word[2]+1, word[4], word[5], word[0]))
if word[3] == scrabble.VERTICAL:
self.emit(QtCore.SIGNAL("update(QString)"),
"Vert: x: %2i y: %2i weight: %i score: %i word: %s" % (word[1]+1, word[2]+1, word[4], word[5], word[0]))
return
def main():
app = QtGui.QApplication(sys.argv)
if len( sys.argv ) == 1:
window = ScrabbleGUI()
else:
window = ScrabbleGUI(filename=sys.argv[1])
window.show()
sys.exit(app.exec_())
if __name__ == '__main__':
main()