-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathauto_scrabble.py
209 lines (177 loc) · 5.4 KB
/
auto_scrabble.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
from _thread import start_new_thread
from copy import copy
from math import ceil
from PIL import Image, ImageOps
import pyautogui as screen
screen.FAILSAFE = False
from time import sleep
def refocus():
screen.click(0, 0)
x = int(screen.size()[0] / 2)
y = int(screen.size()[1] / 2)
screen.moveTo(x, y)
# Read in letters and mouse input
def input_semimanual_letters():
letter_data = []
print("\nType letter and press [Enter] while hovering over it,")
print("Or immediately press [Enter] to continue")
while 1:
new_letter = {}
new_letter["letter"] = str(input(" - Letter: "))
if "" is new_letter["letter"]:
print("\nFinished recording data. Solving...")
break
coordinates = screen.position()
new_letter["x"] = coordinates[0]
new_letter["y"] = coordinates[1]
letter_data.append(new_letter)
return letter_data
# Read in button coordinates for future use
def input_semimanual_next_level():
print("\nPress [Enter] while hovering over button")
input(" ")
coordinates = screen.position()
next_level_data["x"] = coordinates[0]
next_level_data["y"] = coordinates[1]
def auto_letter(letter, invert):
if invert:
letter_image = Image.open('images/' + letter + '.png')
else:
letter_image = Image.open('images/' + letter + '_i.png')
letter_boxes = list(screen.locateAllOnScreen(letter_image, confidence=0.92, grayscale=True, region=(845, 670, 1065, 880)))
for letter_box in letter_boxes:
new_letter = {}
new_letter["letter"] = letter
new_letter["x"] = letter_box[0] + int(letter_box[2] / 2)
new_letter["y"] = letter_box[1] + int(letter_box[3] / 2)
l = new_letter
valid = True
for d in letter_data:
if d["letter"] == l["letter"] and abs(d["x"] - l["x"]) < 5 and abs(d["y"] - l["y"]) < 5:
valid = False
break
if valid:
letter_data.append(new_letter)
done_threads.append(1)
def auto_letters():
alphabet = 'abcdefghijklmnopqrstuvwxyz'
global letter_data, done_threads
letter_data = []
done_threads = []
for letter in alphabet:
start_new_thread(auto_letter, (letter, False))
start_new_thread(auto_letter, (letter, True))
while len(done_threads) < len(alphabet) * 2:
pass
return letter_data
def auto_next_box(box_data):
boxes = list(screen.locateAllOnScreen('images/' + box_data[0] + '.png', confidence=0.8, grayscale=True, region=box_data[1]))
try:
level_boxes += boxes
except:
level_boxes = boxes
for level_box in level_boxes:
x = level_box[0] + int(level_box[2] / 2)
y = level_box[1] + int(level_box[3] / 2)
screen.click(x, y)
done_threads.append(1)
def auto_next_level():
global level_boxes, done_threads
level_boxes = []
done_threads = []
boxes = [('level1', (880, 695, 1030, 730)), ('level2', (880, 505, 1035, 545)), ('collect', (880, 695, 1030, 730)), ('close', (1050, 320, 1085, 355))]
for box in boxes:
start_new_thread(auto_next_box, (box,))
while len(done_threads) < len(boxes):
pass
# For each of <= 5000 words, check if word fits with rules
def scrabble_search(i_start):
for i in range(i_start, i_start + 5000):
if i > len(dictionary) - 1:
break
word = dictionary[i]
valid = True
for char in word:
if word.count(char) > charset.count(char) or len(word) <= 2 or "'" in char:
valid = False
break
if valid:
valid_words.append(word)
done_threads.append(1)
# Manages reading through dictionary
def scrabble():
global valid_words, done_threads, dictionary
valid_words, done_threads = [], []
with open("./dictionary_full.txt") as f_dictionary:
dictionary = f_dictionary.read().split("\n")
num_words = len(dictionary)
max_range = ceil(num_words / 5000)
for i in range(0, max_range):
start_new_thread(scrabble_search, (i * 5000,))
while len(done_threads) < max_range:
pass
valid_words.sort(key = lambda item: (len(item), item))
try:
valid_words.remove("")
except:
pass
return valid_words
# Drags mouse over letters to make words!
def solve_level(letter_data):
global charset
charset = ""
for letter in letter_data:
charset += letter["letter"]
if charset: print(' Charset: ' + ''.join(sorted(charset)))
valid_words = scrabble()
for word in valid_words:
word_coordinates = []
letter_data_tmp = copy(letter_data)
for i, letter in enumerate(word):
letter_datum = list(filter(lambda data: (data["letter"] == letter), letter_data_tmp))[0]
x, y = letter_datum["x"], letter_datum["y"]
letter_data_tmp.remove(letter_datum)
if i == 0:
screen.moveTo(x, y)
screen.mouseDown()
elif i == len(word) - 1:
screen.moveTo(x, y)
screen.mouseUp()
else:
screen.moveTo(x, y)
# Moves past a menu screen
def next_level():
x, y = next_level_data["x"], next_level_data["y"]
screen.click(x, y)
# Organizes all the functions
def main():
global next_level_data
next_level_data = {}
print("--- Autosolve Wordscapes ---")
print("Type [a] for auto or [m] for manual mode")
choice = str(input(" Mode: "))
if choice is "m":
while choice is not "":
print("\nType [s] to solve or [n] for next level,")
print("Or immediately press [Enter] to exit")
choice = str(input(" Choice: "))
if choice is "s":
solve_level(input_semimanual_letters())
elif choice is "n":
if not next_level_data:
input_semimanual_next_level()
next_level()
refocus()
print("\nExiting...")
elif choice is "a":
try:
auto_next_level()
while True:
refocus()
sleep(1 / 2)
solve_level(auto_letters())
refocus()
auto_next_level()
except KeyboardInterrupt:
print("\nExiting...")
main()