-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathfinder.py
300 lines (236 loc) · 10.5 KB
/
finder.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
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
from utils import Trie
import numpy as np
import multiprocessing as mp
import tkinter as tk
class SpellCastSolver():
def __init__(self, board, multiplier_board):
self.board = np.array(board)
self.multipler_board = multiplier_board
self.word_search = Trie()
words = open("words.txt", "r").read().split("\n")
for word in words:
self.word_search.insert(word)
self.directions = [
(-1, -1),
(-1, 0),
(-1, 1),
(0, -1),
(0, 1),
(1, -1),
(1, 0),
(1, 1)
]
self.directions = [np.array(direction) for direction in self.directions]
self.multipliers = [] # (location, type)
for y in range(5):
for x in range(5):
if self.multipler_board[y][x] != '':
self.multipliers.append(((y, x), self.multipler_board[y][x]))
self.multiplier_locations = [i[0] for i in self.multipliers]
print(self.multipliers)
self.letter_values = np.array([1, 4, 5, 3, 1, 5, 3, 4, 1, 7, 3, 3, 4, 2, 1, 4, 8, 2, 2, 2, 4, 5, 5, 7, 4, 8])
self.upper_bound = np.array((4, 4))
self.lower_bound = np.array((0, 0))
def _get_location_value(self, location):
value = self.letter_values[ord(self.board[location]) - ord('a')]
# todo : add a modifier here
return value
def _get_path_value(self, path, where_swaps):
total = 0
double_word = False
swapped_locations, swapped_values = [i[0] for i in where_swaps], [i[1] for i in where_swaps]
for i in path:
letter = self.board[i]
if i in swapped_locations:
letter = swapped_values[swapped_locations.index(i)]
letter_value = self.letter_values[ord(letter) - ord('a')]
if i in self.multiplier_locations:
multiplier = self.multipliers[self.multiplier_locations.index(i)][1]
if multiplier == "d":
letter_value *= 2
elif multiplier == "t":
letter_value *= 3
elif multiplier == "2":
double_word = True
total += letter_value
if double_word:
total *= 2
return total
# return sum([self._get_location_value(location) for location in path])
def _get_word_value(self, word):
return sum([self.letter_values[ord(i) - ord('a')] for i in word])
def set_board(self, board):
self.board = np.array(board)
def _in_bounds(self, location):
return all(self.upper_bound >= location) and all(self.lower_bound <= location)
def _recursively_search(self, starting_node, already_found, used, swaps_used, where_swap, found_words):
next_following_characters = self.word_search.find_children(already_found)
if not next_following_characters:
return
for direction in self.directions:
new_node = tuple(starting_node + direction)
if not self._in_bounds(new_node) or new_node in used:
continue
normal_char = self.board[new_node]
for useful_char in next_following_characters:
needs_swap = (normal_char != useful_char)
if swaps_used >= 2 and needs_swap: # don't look for useful swaps if we can't swap
continue
new_word = already_found + useful_char
new_used = used + [new_node]
new_swaps_used = swaps_used + needs_swap
if needs_swap:
new_where_swap = where_swap + [(new_node, useful_char)]
else:
new_where_swap = where_swap.copy()
if self.word_search.find_word(new_word):
found_words.append((new_word, new_used, self._get_word_value(new_word), new_swaps_used, new_where_swap, self._get_path_value(new_used, new_where_swap)))
self.word_search.remove(new_word)
self._recursively_search(new_node, new_word, new_used, new_swaps_used, new_where_swap, found_words)
def find_all_words(self, available_swaps, do_multiprocessing=False):
swaps = 2 - available_swaps
if do_multiprocessing:
manager = mp.Manager()
all_found_words = manager.list()
procs = []
else:
all_found_words = []
for first in range(0, 5):
for second in range(0, 5):
starting_node = (first, second)
if do_multiprocessing:
p = mp.Process(target=self._recursively_search, args=(starting_node, board[starting_node], [starting_node], swaps, [], all_found_words))
procs.append(p)
p.start()
else:
self._recursively_search(starting_node, self.board[starting_node], [starting_node], swaps, [], all_found_words)
if do_multiprocessing:
for p in procs:
p.join()
return all_found_words
if __name__ == "__main__":
frame = tk.Tk()
frame.title("SpellCastSolver")
frame.geometry("600x200")
solver = None
board = None
def cause_error(error_message):
error_window = tk.Tk()
error_window.geometry("200x100")
def close():
error_window.destroy()
error_label = tk.Label(error_window, text=error_message)
error_label.pack(pady=20)
error_button = tk.Button(error_window, text="Ok", command=close)
error_button.pack()
error_window.mainloop()
def clear_labels():
label.config(text="")
for i in range(5):
for j in range(5):
boxes[i][j].config(bg="white")
def set_board():
global board
global solver
board = np.array([[i.get("1.0", "end-1c") for i in row] for row in boxes])
multiplier_board = np.array([[i.get("1.0", "end-1c") for i in row] for row in upgrade_boxes])
solver = SpellCastSolver(board, multiplier_board)
label.config(text="Board set!")
def highlight_by_path(path, color="light blue"):
for i in path:
boxes[i[0]][i[1]].config(bg=color)
def find_zero_solution():
if not all([all(i) for i in board]):
cause_error("Character boxes not full")
else:
label.config(text="Finding no swap solution")
zero_swap_words = solver.find_all_words(0)
# word, path, value, swapcount, where swaps used, path value
zero_swap_words.sort(key=lambda x: x[5])
best_zero_swap_word = zero_swap_words[-1][0]
best_zero_swap_path = zero_swap_words[-1][1]
highlight_by_path(best_zero_swap_path)
print(zero_swap_words[-1])
label.config(text=f"Zero swap solution '{best_zero_swap_word}'")
def find_one_solution():
if not all([all(i) for i in board]):
cause_error("Character boxes not full")
else:
label.config(text="Finding one swap solution")
zeros = solver.find_all_words(0)
zeros.sort(key=lambda x: x[5])
zero = zeros[-1]
print(f"Zero word {zero}")
one_swap_words = solver.find_all_words(1)
# word, path, value, swapcount, where swaps used
one_swap_words.sort(key=lambda x: x[5])
best_one_swap_word = one_swap_words[-1][0]
best_one_swap_path = one_swap_words[-1][1]
best_one_swap_where = one_swap_words[-1][4]
print(one_swap_words[-1])
highlight_by_path(best_one_swap_path)
highlight_by_path([i[0] for i in best_one_swap_where], "red")
label.config(text=f"One swap solution '{best_one_swap_word}'")
def find_two_solution():
if not all([all(i) for i in board]):
cause_error("Character boxes not full")
else:
label.config(text="Finding two swap solution")
zeros = solver.find_all_words(0)
ones = solver.find_all_words(1)
zeros.sort(key=lambda x: x[5])
ones.sort(key=lambda x: x[5])
zero = zeros[-1]
one = ones[-1]
print(f"Zero word {zero}")
print(f"One word {one}")
two_swap_words = list(solver.find_all_words(2, True))
# word, path, value, swapcount, where swaps used
two_swap_words.sort(key=lambda x: x[5])
best_two_swap_word = two_swap_words[-1][0]
best_two_swap_path = two_swap_words[-1][1]
best_two_swap_where = two_swap_words[-1][4]
print(two_swap_words[-1])
highlight_by_path(best_two_swap_path)
highlight_by_path([i[0] for i in best_two_swap_where], "red")
label.config(text=f"Two swap solution '{best_two_swap_word}'")
def add_multipliers():
label.config(text="add location of double word")
def auto_tab(event):
event.widget.delete("1.0", "end")
event.widget.tk_focusNext().focus()
boxes = []
spacing = 20
# make a grid 5x5
for x in range(0, 5):
row = []
for y in range(0, 5):
input_txt = tk.Text(frame, height=1, width=2)
input_txt.place(x=y*spacing + 5, y=x*spacing + 5)
input_txt.bind("<KeyPress>", auto_tab)
row.append(input_txt)
boxes.append(row)
upgrade_boxes = []
spacing = 20
# make a grid 5x5
for x in range(0, 5):
row = []
for y in range(0, 5):
input_txt = tk.Text(frame, height=1, width=2)
input_txt.place(x=495 + y*spacing, y=x*spacing + 5)
input_txt.bind("<KeyPress>", auto_tab)
row.append(input_txt)
upgrade_boxes.append(row)
set_board = tk.Button(frame, text="set board", command=set_board)
set_board.pack()
find_zero_solution_button = tk.Button(frame, text="find no swap solution", command=find_zero_solution)
find_zero_solution_button.pack()
find_one_solution_button = tk.Button(frame, text="find one swap solution", command=find_one_solution)
find_one_solution_button.pack()
find_two_solution_button = tk.Button(frame, text="find two swap solution", command=find_two_solution)
find_two_solution_button.pack()
clear_labels_button = tk.Button(frame, text="clear labels", command=clear_labels)
clear_labels_button.pack()
label = tk.Label(frame, text="", wraplength=200)
label.pack()
frame.mainloop()