-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
542 lines (502 loc) · 21.1 KB
/
main.py
File metadata and controls
542 lines (502 loc) · 21.1 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
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
import pyautogui
import time
import cv2
from PIL import Image
class Solver:
# 15*23
tile_data = []
# make data base of color and rgb
color = [
[97,205,203], # sky blue0
[7,202,58], # green1
[205,97,37], # brown2
[255,148,54], # orange3
[255,97,107], # red4
[253,141,249], # pink5
[186,188,188], # grey6
[204,201,116], # yellow7
[202,107,199], # purple8
[0,116,246], # blue9
[245,247,247], # null white10
[235,237,237], # null grey11
[0,0,0] # null black12
]
thresh = 10
hit_call = False
hit_result = [] # [y, x]
pass_tile = [[0]*23 for i in range(15)] # tile that already checked
def __init__(self):
self.img = cv2.imread('screenshot.png')
#crop image by coordinates
self.img = self.img[158:533, 185:760]
# print img size
print(self.img.shape)
cv2.imwrite('crop.png', self.img)
im = Image.open('crop.png')
self.pix = im.load()
self.find_color()
print(self.tile_data)
cv2.imwrite('result.png', self.img)
def find_color(self):
# loop for each tile
for i in range(15):
tile_row = []
for j in range(23):
# get pixel color
r, g, b = self.pix[((j+1)*25-13), ((i+1)*25-13)]
print(r, g, b, j , i)
# calculate distance to each color
distance = []
for color in self.color:
distance.append(abs(r-color[0])+abs(g-color[1])+abs(b-color[2]))
# find the minimum distance
min_distance = min(distance)
# find the index of minimum distance
index = distance.index(min_distance)
print(index)
# if distance is less than threshold
if min_distance < self.thresh:
# append index to tile_data
tile_row.append(index)
else:
# append 9 to tile_data
tile_row.append(12)
# plot a colored dot on the tile
self.img = cv2.circle(self.img, ((j+1)*25-13, (i+1)*25-13), radius=0, color=(list(reversed(self.color[index]))), thickness=-1)
if (tile_row[j] == 11) or (tile_row[j] == 10):
tile_row[j] = 12
self.img = cv2.circle(self.img, ((j+1)*25-13, (i+1)*25-13), radius=0, color=(list(reversed(self.color[12]))), thickness=-1)
# adding row to tile_data
if j == 22:
self.tile_data.append(tile_row)
print (len(tile_row))
tile_row = []
# write to txt file
with open('tile_data.txt', 'w') as f:
for item in self.tile_data:
f.write("%s\n" % item)
def tile_hit(self,y,x):
self.hit_call = True
self.hit_result.append(y)
self.hit_result.append(x)
def check_tile_decider(self):
checking_color = []
print('[decider]enter')
print('[decider]',self.pass_tile)
# find color to check
for i in range(15):
for j in range(23):
# check if tile is already checked
if self.pass_tile[i][j] == True:
print('[decider]passing tile',[i,j])
continue
if self.tile_data[i][j] == 12:
self.pass_tile[i][j] = True
print('[decider]passing tile',[i,j])
pass
else:
checking_color.append(i) # row
checking_color.append(j) # column
checking_color.append(self.tile_data[i][j]) # color
print('[decider]', checking_color)
return checking_color
# no tile any more
if i == 14 and j == 22:
print('[decider]no tile any more')
return False
def cross_point_calc(self, hit_result, checking_color):
cross_point_1 = [ hit_result[0], checking_color[1] ]
cross_point_2 = [ checking_color[0], hit_result[1] ]
print('[cross check]',checking_color)
print('[cross check]',hit_result)
print('[cross check]',cross_point_1)
print('[cross check]',cross_point_2)
# check for each cross point
# first have to check if hit_result and checking_color is not in the same row or column
while True:
if hit_result[0] == checking_color[0]:
print('[cross check]same row')
if hit_result[1] < checking_color[1]:
cross_point_1[1] -= 1
# no need to check cross point 2 since it is in the same row
else:
cross_point_1[1] += 1
# no need to check cross point 2 since it is in the same row
break
elif hit_result[1] == checking_color[1]:
print('[cross check]same column')
if hit_result[0] < checking_color[0]:
cross_point_1[0] += 1
# no need to check cross point 2 since it is in the same column
else:
cross_point_1[0] -= 1
# no need to check cross point 2 since it is in the same column
break
else:
break
# cros point 1
counter = 0
print('[cross check]cross point 1',cross_point_1)
# check left
if cross_point_1[1] != 0:
for i in range(cross_point_1[1])[::-1]:
if self.tile_data[cross_point_1[0]][i] == 12:
pass
elif self.tile_data[cross_point_1[0]][i] == checking_color[2]:
counter += 1
break
else:
break
# check right
if cross_point_1[1] != 22:
for i in range(cross_point_1[1]+1, 23):
if self.tile_data[cross_point_1[0]][i] == 12:
pass
elif self.tile_data[cross_point_1[0]][i] == checking_color[2]:
counter += 1
break
else:
break
# check up
if cross_point_1[0] != 0:
for i in range(cross_point_1[0])[::-1]:
if self.tile_data[i][cross_point_1[1]] == 12:
pass
elif self.tile_data[i][cross_point_1[1]] == checking_color[2]:
counter += 1
break
else:
break
# check down
if cross_point_1[0] != 14:
for i in range(cross_point_1[0]+1, 15):
if self.tile_data[i][cross_point_1[1]] == 12:
pass
elif self.tile_data[i][cross_point_1[1]] == checking_color[2]:
counter += 1
break
else:
break
# count check
if counter == 2 or counter == 3 or counter == 4:
print('[cross check]counter',counter)
if self.tile_data[cross_point_1[0]][cross_point_1[1]] == 12:
print('[cross check]cross point 1 hit')
return [cross_point_1[0], cross_point_1[1]]
# croos point 2
print('[cross check]cross point 2',cross_point_2)
counter = 0
# check left
if cross_point_2[1] != 0:
for i in range(cross_point_2[1])[::-1]:
if self.tile_data[cross_point_2[0]][i] == 12:
pass
elif self.tile_data[cross_point_2[0]][i] == checking_color[2]:
counter += 1
break
else:
break
# check right
if cross_point_2[1] != 22:
for i in range(cross_point_2[1]+1, 23):
if self.tile_data[cross_point_2[0]][i] == 12:
pass
elif self.tile_data[cross_point_2[0]][i] == checking_color[2]:
counter += 1
break
else:
break
# check up
if cross_point_2[0] != 0:
for i in range(cross_point_2[0])[::-1]:
if self.tile_data[i][cross_point_2[1]] == 12:
pass
elif self.tile_data[i][cross_point_2[1]] == checking_color[2]:
counter += 1
break
else:
break
# check down
if cross_point_2[0] != 14:
for i in range(cross_point_2[0]+1, 15):
if self.tile_data[i][cross_point_2[1]] == 12:
pass
elif self.tile_data[i][cross_point_2[1]] == checking_color[2]:
counter += 1
break
else:
break
# # count check
if counter == 2 or counter == 3 or counter == 4:
print('[cross check]cross point 2 hit')
return [cross_point_2[0], cross_point_2[1]]
def tile_solver(self):
self.hit_call = False
while True:
# define checking color
checking_color = self.check_tile_decider()
if checking_color == False:
print('[tile solver]no tile any more')
return False
# find the same color
while True:
# check left
if checking_color[1] != 0:
for i in range(checking_color[1])[::-1]:
print('[tile solver]checking left', i)
if self.tile_data[checking_color[0]][i] == checking_color[2]:
# if its next to the checking tile
if i == checking_color[1]-1:
break
self.tile_hit(checking_color[0], i)
break
elif self.tile_data[checking_color[0]][i] == 12:
# check up
if checking_color[0] != 0:
for j in range(checking_color[0])[::-1]:
if self.tile_data[j][i] == checking_color[2]:
self.tile_hit(j, i)
break
elif self.tile_data[j][i] == 12:
pass
else:
break
# check down
if checking_color[0] != 14:
for j in range(checking_color[0]+1, 15):
if self.tile_data[j][i] == checking_color[2]:
self.tile_hit(j, i)
break
elif self.tile_data[j][i] == 12:
pass
else:
break
else:
break
# break on hit
if self.hit_call:
break
if self.hit_call:
break
# check right
if checking_color[1] != 22:
for i in range(checking_color[1]+1, 23):
print('[tile solver]checking right', i)
if self.tile_data[checking_color[0]][i] == checking_color[2]:
# if its next to the checking tile
if i == checking_color[1]+1:
break
self.tile_hit(checking_color[0], i)
break
elif self.tile_data[checking_color[0]][i] == 12:
# check up
if checking_color[0] != 0:
for j in range(checking_color[0])[::-1]:
if self.tile_data[j][i] == checking_color[2]:
self.tile_hit(j, i)
break
elif self.tile_data[j][i] == 12:
pass
else:
break
# check down
if checking_color[0] != 14:
for j in range(checking_color[0]+1, 15):
if self.tile_data[j][i] == checking_color[2]:
self.tile_hit(j, i)
break
elif self.tile_data[j][i] == 12:
pass
else:
break
else:
break
# break on hit
if self.hit_call:
break
if self.hit_call:
break
# check up
if checking_color[0] != 0:
for i in range(checking_color[0])[::-1]:
print('[tile solver]checking up', i)
if self.tile_data[i][checking_color[1]] == checking_color[2]:
# if its next to the checking tile
if i == checking_color[0]-1:
break
self.tile_hit(i, checking_color[1])
break
elif self.tile_data[i][checking_color[1]] == 12:
# check left
if checking_color[1] != 0:
for j in range(checking_color[1])[::-1]:
if self.tile_data[i][j] == checking_color[2]:
self.tile_hit(i, j)
break
elif self.tile_data[i][j] == 12:
pass
else:
break
# check right
if checking_color[1] != 22:
for j in range(checking_color[1]+1, 23):
if self.tile_data[i][j] == checking_color[2]:
self.tile_hit(i, j)
break
elif self.tile_data[i][j] == 12:
pass
else:
break
else:
break
# break on hit
if self.hit_call:
break
if self.hit_call:
break
# check down
if checking_color[0] != 14:
for i in range(checking_color[0]+1, 15):
print('[tile solver]checking down', i)
if self.tile_data[i][checking_color[1]] == checking_color[2]:
# if its next to the checking tile
if i == checking_color[0]+1:
break
self.tile_hit(i, checking_color[1])
break
elif self.tile_data[i][checking_color[1]] == 12:
# check left
if checking_color[1] != 0:
for j in range(checking_color[1])[::-1]:
if self.tile_data[i][j] == checking_color[2]:
self.tile_hit(i, j)
break
elif self.tile_data[i][j] == 12:
pass
else:
break
# check right
if checking_color[1] != 22:
for j in range(checking_color[1]+1, 23):
if self.tile_data[i][j] == checking_color[2]:
self.tile_hit(i, j)
break
elif self.tile_data[i][j] == 12:
pass
else:
break
else:
break
# break on hit
if self.hit_call:
break
if self.hit_call:
break
else:
print('[tile solver]no hit')
self.pass_tile[checking_color[0]][checking_color[1]] = True
break
# break on hit
if self.hit_call:
print('[tile solver]hit', self.hit_result)
break
# callculate cross point
return self.cross_point_calc(self.hit_result, checking_color)
def tile_data_update(self,click_tile):
cross_color = [] # left right up down
cross_tile = [] # left right up down
deleted_color = []
# check left
if click_tile[1] != 0:
for i in range(click_tile[1])[::-1]:
if self.tile_data[click_tile[0]][i] == 12:
pass
else:#check length of cross_color
cross_color.append(self.tile_data[click_tile[0]][i])
cross_tile.append([click_tile[0],i])
print('[update]append')
break
# check right
if click_tile[1] != 22:
for i in range(click_tile[1]+1, 23):
if self.tile_data[click_tile[0]][i] == 12:
pass
else:
cross_color.append(self.tile_data[click_tile[0]][i])
cross_tile.append([click_tile[0],i])
print('[update]append')
break
# check up
if click_tile[0] != 0:
for i in range(click_tile[0])[::-1]:
if self.tile_data[i][click_tile[1]] == 12:
pass
else:
cross_color.append(self.tile_data[i][click_tile[1]])
cross_tile.append([i,click_tile[1]])
print('[update]append')
break
# check down
if click_tile[0] != 14:
for i in range(click_tile[0]+1, 15):
if self.tile_data[i][click_tile[1]] == 12:
pass
else:
cross_color.append(self.tile_data[i][click_tile[1]])
cross_tile.append([i,click_tile[1]])
print('[update]append')
break
# check which color is the same
print('[update]',cross_color)
for i in range(len(cross_color)):
for j in range(len(cross_color)):
if i==j:
continue
if cross_color[i] == cross_color[j]:
deleted_color.append(cross_color[i])
self.tile_data[cross_tile[i][0]][cross_tile[i][1]] = 12
self.tile_data[cross_tile[j][0]][cross_tile[j][1]] = 12
# over 3 same color then delete
for j in deleted_color:
if cross_color[i] == j:
self.tile_data[cross_tile[i][0]][cross_tile[i][1]] = 12
# write to txt file
with open('tile_data.txt', 'w') as f:
for item in self.tile_data:
f.write("%s\n" % item)
def init_variables(self):
self.hit_call = False
self.hit_result = []
self.pass_tile = [[0]*23 for i in range(15)]
# start main
if __name__ == '__main__':
print(pyautogui.size()) # Get the screen size
# put delay to 5 seconds
time.sleep(2)
# take current mouse position
print(pyautogui.position())
pyautogui.moveTo(456, 357) # Move the mouse to XY coordinates over num_second seconds
# click the mouse
pyautogui.click()
time.sleep(1)
# take screenshot
pyautogui.screenshot('screenshot.png')
solver = Solver()
result = True
n=0
while result:
result = solver.tile_solver()
print(result)
if result == False:
break
time.sleep(0.01)
pyautogui.moveTo(185+result[1]*25+13, 158+result[0]*25+13)
time.sleep(0.01)
pyautogui.click()
solver.tile_data_update(result)
solver.init_variables()
n+=1
if n == 100:
break
# print end showy
print('end')