-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathtetris.com-bot.py
563 lines (467 loc) · 16.1 KB
/
tetris.com-bot.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
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
# Tetris.com
import pyautogui
from PIL import Image
import numpy as np
import time
import random
import keyboard
import math
# Weights for the criteria
HEIGHT_SIMPLE = -3 # Unit of height (overall)
HEIGHT_COEFF = 1.4 # For progressive weght of height i**coeff for each next row
WIDTH_COEFF = 3 # For regressive count of width on certain height (more for less effect)
HEIGHT_DIFFERENCE = -5 # Each step between neighbouring rows
HOLE = -500
OVERHEAD = -10 # Cells above the hole
LINE_1 = 0
LINE_2 = 0
LINE_3 = 1000
LINE_4 = 10000
UNWANTED_LINE = -1000
DIVERSITY = 50 # Has a spot for all kinds of tetraminos
EXTRA_WELLS = -300 # More than one well (hole >2 deep)
HAS_LAST_COLUMN = -2000
PENULTIMATE_COLUMN_PROBLEM = -1000
mode = 2 # control the behaviour
# 0 fixing the holes
# 1 ready for a stick
# 2 building a monolith with a well
show_scores = False
# return pixel's brightness
def brightness(px):
return int((px[0]+px[1]+px[2])/3)
# Is a pixel of that color that we use to find the border?
def is_border_color(px):
if px[0]==36 and px[1]==35 and px[2]==35:
return True
return False
# get the inner top border
def get_top_border(im):
px = im.load()
found_borders = []
for i in range(0, im.size[0], im.size[0]//30):
for j in range(2, im.size[1]):
if not is_border_color(px[i,j]) \
and is_border_color(px[i,j-1]) \
and is_border_color(px[i,j-2]):
if len(found_borders)>0 and abs(j-found_borders[-1])<2:
found_borders.append(found_borders[-1])
else:
found_borders.append(j)
break
#print (found_borders)
for found_border in found_borders:
if found_borders.count(found_border) > len(found_borders)//2:
return found_border
# Get four borders
def get_borders(im):
top = get_top_border(im)
left = get_top_border(im.transpose(Image.ROTATE_270))
bottom = im.size[1] - get_top_border(im.transpose(Image.ROTATE_180))
right = im.size[0] - get_top_border(im.transpose(Image.ROTATE_90))
return (left, top, right, bottom)
# Read square, return 1 (has block), 0 (empty)
def read_square(im):
treshold = 5
total = 0
px = im.load()
for i in range(im.size[0]):
for j in range(im.size[1]):
total += brightness(px[i,j])
if total // ( im.size[0] * im.size[1] ) > treshold:
return 1
return 0
# Read whole the image return NP array
def read_field(im):
field = np.zeros((10,20), dtype=int)
delta = 5
for i in range(10):
for j in range(20):
x = int(im.size[0]*i/10+im.size[0]/20)
y = int(im.size[1]*j/20+im.size[1]/40)
cell_block = im.crop(((x-delta, y-delta, x+delta, y+delta)))
field[i,j] = read_square(cell_block)
return field
# if the line N of the field all empty
def is_empty_line(field, n):
for i in range(10):
if field [i,n] != 0:
return False
return True
# Add empty column to the right of the piece
def add_empty_line_from_right(piece_input):
piece = np.copy(piece_input)
dim1 = len(piece)
dim2 = len(piece[0])
to_append = np.zeros((dim1, 1), dtype=int)
piece = np.append(piece, to_append, 1)
return piece
# Make a piece a square array
def make_it_square(piece_input):
piece = np.copy(piece_input)
while len(piece)!=len(piece[0]):
piece = add_empty_line_from_right(piece)
return piece
# Get a piece from the read field (from 2 lines before the empty line)
def get_piece(field):
for j in range(1,20):
if is_empty_line(field, j) and not is_empty_line(field, j-1):
top = j-2
break
piece_list = []
left = -1
leftover_flag = False
for i in range(10):
# This is a messy bit. This is a protectin for cases when some leftover lines mistakenly identified as a piece. And worng decisions are made.
if leftover_flag and i <9 and \
(field[i+1, top] != 0 or field[i+1, top+1] != 0) and \
(field[i, top] == 0 and field[i, top+1] == 0):
print ("Leftover lines")
return False
if i > 0 and \
(field[i-1, top] != 0 or field[i-1, top+1] != 0) and \
(field[i, top] == 0 and field[i, top+1] == 0):
leftover_flag = True
# End of messy bit
if field[i, top] != 0 or field[i, top+1] != 0:
if left == -1:
left = i
piece_list.append(field[i, top])
piece_list.append(field[i, top+1])
if piece_list.count(1)!=4:
return False
piece = np.asarray(piece_list, dtype = int)
piece = np.reshape(piece, (len(piece_list)//2, 2))
piece = make_it_square(piece)
return piece, left
# Return array with 4 versions of a piece (rotations)
def get_rotations(piece):
rotations = []
for i in [0,1,2,3]:
rotations.append(np.rot90(piece, k=i))
return rotations
# Return only bottom part of the field (minus the piece)
def get_floor(field):
''' gett the botttom "landscape" + 4 empty lines '''
for j in range(19,0, -1):
if is_empty_line(field, j):
break
return np.append(np.zeros((10,4), dtype=int), field[0:10, j+1:20], axis=1)
def is_legit_position(piece, position):
''' See if the piece seps out of the field'''
if position < 0:
for i in range (abs(position)):
if max(piece[i]) != 0:
return False
if position + len (piece) > 10:
for i in range( position + len (piece)-10 ):
if max(piece[-i-1]) != 0:
return False
return True
def get_new_floor(floor, orig_piece, orig_position):
''' Resulting new "landscape" when piece is dropped '''
#print (floor)
#print ("1. Original:\n", orig_piece, orig_position)
piece = orig_piece.copy()
position = orig_position
# trim sides
if position < 0:
for i in range(abs(position)):
piece = np.delete(piece, 0, 0)
position = 0
if position + len (piece) > 10:
for i in range( position + len (piece)-10 ):
piece = np.delete(piece, -1, 0)
#print ("2: Trimmed out of glass\n", piece, position)
# Exteend to 0
if position > 0:
piece = np.append(\
np.zeros ( (position, len(piece[0])), dtype=int ),
piece,
0, )
#print ("3. Extented to the top:\n", piece, position)
if orig_position +len (orig_piece) < 10:
piece = np.append(\
piece,
np.zeros ( (10-orig_position-len(orig_piece), len(piece[0])), dtype=int ),
0, )
#print ("4. Extented to the bottom:\n", piece, position)
while is_empty_line(piece, len(piece[0])-1):
piece = piece [0:10, 0:len(piece[0])-1]
#print ("5. Trim from the right:\n", piece, position)
# deep it in, see when itt starts ovelapping
for depth in range(0, len(floor[0])-len(piece[0])+1):
# extent it from the left and right
temp_piece = piece.copy()
temp_piece = np.append(\
np.zeros ( (10, depth), dtype=int ),
temp_piece,
1, )
temp_piece = np.append(\
temp_piece,
np.zeros ( (10, len(floor[0])-depth-len(piece[0])), dtype=int ),
1, )
#print ("6. Piece ready to merge:\n", temp_piece)
merged = np.add(floor, temp_piece)
#print ("7. Floor and piece:\n", merged)
if np.amax(merged)==2:
return last_valid
last_valid = merged.copy()
return last_valid
def collapse_floor(floor):
cell_counts = np.sum(floor, axis=0)
new_floor = floor.copy()
lines = 0
for i in range(len(cell_counts))[::-1]:
if cell_counts[i]==10:
lines +=1
new_floor = np.delete(new_floor, i, 1)
return (new_floor, lines)
def get_height(floor):
cell_counts = np.sum(floor, axis=0)
height = np.count_nonzero(cell_counts)
return height
def get_height_adv(floor):
height_list = []
cell_counts = np.sum(floor, axis=0)
return cell_counts
## for i in range(len(floor[0]))[::-1]:
## if cell_counts[i] > 0:
## height_list.append(1)
## else:
## height_list.append(0)
## return height_list
def get_holes(floor):
holes = 0
overhead = 0
for i in range(10):
found_cell = False
over_in_row = 0
for j in range(len(floor[0])):
if floor[i,j]==1:
found_cell = True
over_in_row+=1
if found_cell and floor[i,j]==0:
holes += 1
overhead += over_in_row
return holes, overhead
def get_hgt_differences(floor):
h_diffs = 0
heights = np.sum(floor, axis=1)
for i in range(1, 10):
h_diffs += abs(heights[i]-heights[i-1])
return h_diffs
def get_diversity(floor):
a, b, c = False, False, False
heights = np.sum(floor, axis=1)
for i in range(1, 10):
if heights[i]-heights[i-1] == 0:
a = True
if heights[i]-heights[i-1] == 1:
b = True
if heights[i]-heights[i-1] == -1:
c = True
return a and b and c
def last_column(floor):
if np.sum(floor[9]) > 0:
return True
return False
def count_wells(floor):
wells = 0
heights = np.sum(floor, axis=1)
if heights[1]-heights[0]>2:
wells +=1
if mode !=2 and heights[8]-heights[9]>2:
wells +=1
for i in range(1, 8):
if heights[i-1]-heights[i]>2 and heights[i+1]-heights[i]>2:
wells +=1
return wells
def get_score(orig_floor):
score = 0
sc_lines = 0
sc_holes = 0
sc_overhead = 0
sc_height = 0
sc_diff = 0
sc_diverse = 0
sc_wells = 0
# Check for full lines
floor, lines = collapse_floor(orig_floor)
# Completed lines
if mode != 2:
if lines == 1:
sc_lines = LINE_1
if lines == 2:
sc_lines =sc_lines = LINE_2
if lines == 3:
sc_lines = LINE_3
if lines == 4:
sc_lines = LINE_4
elif lines>0:
score += UNWANTED_LINE
score += sc_lines
# Holes (less is beter)
holes, overhead = get_holes(floor)
sc_holes = holes * HOLE
sc_overhead = overhead * OVERHEAD
score += sc_holes
score += sc_overhead
# Height (less is beter)
#score += get_height(floor) * HEIGHT_SIMPLE
# Height advanced (progressive weight)
height_list = get_height_adv(floor)
#print (height_list)
for i in range(len(floor[0])):
if height_list[-i-1]>0:
#print (i, height_list[-i-1], HEIGHT_SIMPLE*HEIGHT_COEFF**i, math.log(height_list[-i-1],WIDTH_COEFF)+1)
height_score = height_list[-i-1] * HEIGHT_SIMPLE * (HEIGHT_COEFF**i) * (math.log(height_list[-i-1],WIDTH_COEFF)+1)
if mode ==2:
sc_height += height_score/2
else:
sc_height += height_score
score += sc_height
# Height differences
sc_diff = get_hgt_differences(floor) * HEIGHT_DIFFERENCE
score += sc_diff
if get_diversity(floor):
sc_diverse = DIVERSITY
score += sc_diverse
wells = count_wells(floor)
if wells >0:
sc_wells = wells * EXTRA_WELLS
score += sc_wells
## # Has stuff in last column (only for holeless situation)
if mode == 2:
if last_column(floor):
score += HAS_LAST_COLUMN
heights = np.sum(floor, axis=1)
if heights[7]-heights[8] > 2:
score += PENULTIMATE_COLUMN_PROBLEM
if show_scores:
print ("Lines:", sc_lines, "Holes:", sc_holes, "Overhead", sc_overhead, "Height:", sc_height)
print ("Diff:", sc_diff, "Divers:", sc_diverse, "Wells", sc_wells)
# Add dither
score -= random.random()
return score
def set_mode(floor):
global mode
holes, _ = get_holes(floor)
if holes > 0:
mode = 0 # fix a hole
return
heights = np.sum(floor, axis=1).tolist()
if heights.count(0) == 1:
heights.remove(0)
if min(heights)>3:
mode = 1 # ready for a stick
return
mode = 2 # building a thing
def do_permutations(piece, floor):
rotations = get_rotations(piece)
best_posiion = -1000000
best_rotation = -1000000
best_score = -1000000
for position in range (-2, 9): # position of a piece
for rotation in range (4): # rotation option off a piece
rotated_piece = rotations[rotation]
if is_legit_position(rotated_piece, position):
new_floor = get_new_floor(floor, rotated_piece, position)
score = get_score(new_floor)
if show_scores:
print (new_floor)
print ("Pos:", position, "Rot:", rotation, "Score:", score)
print("-"*40)
if score > best_score:
best_score = score
best_posiion = position
best_rotation = rotation
print ("Best pos:", best_posiion, "Best rot:", best_rotation, "Best score:", best_score)
return (best_posiion, best_rotation)
def do_the_move(start_x, best_posiion, best_rotation):
def press(button):
delay = 0.02
if True: #need_to_click:
#pyautogui.press(button)
keyboard.send(button)
time.sleep(delay)
else:
print (button)
for i in range(best_rotation):
press("up")
if start_x < best_posiion:
for i in range(best_posiion-start_x):
press("right")
if start_x > best_posiion:
for i in range(start_x-best_posiion):
press("left")
press("space")
def main(im=""):
fail_count = 0
if im=="":
need_to_click = True
im = pyautogui.screenshot()
else:
need_to_click = False
im = Image.open(im)
try:
borders = get_borders(im)
except:
print("Can't find the game")
return
while True:
im = pyautogui.screenshot()
#im.save("screenshots/"+str(time.time())+".png")
cropped = im.crop(borders)
field = read_field(cropped)
#print (field)
try:
piece, start_x = get_piece(field)
#print (start_x)
floor = get_floor(field)
set_mode(floor)
print (mode)
best_posiion, best_rotation = do_permutations(piece, floor)
do_the_move(start_x, best_posiion, best_rotation)
time.sleep(0.05)
fail_count = 0
except:
fail_count += 1
print("Skipping frame")
if fail_count ==10:
print("Game appears to be over")
return
print ("huh")
##
##
##show_scores = True
##filename = "6.png"
##print (filename)
##im = Image.open(filename)
##borders = get_borders(im)
##print (borders)
##
##cropped = im.crop(borders)
##field = read_field(cropped)
##print (field)
##
##piece, start_x = get_piece(field)
####print (piece)
####print (start_x)
##
##rotations = get_rotations(piece)
###print (rotations)
##
##floor = get_floor(field)
###print (floor)
##
##
##best_posiion, best_rotation = do_permutations(piece, floor)
##print ("Best pos:", best_posiion, "Best rot:", best_rotation)
##
##get_score(floor)
##
##
## do_the_move(start_x, best_posiion, best_rotation)
keyboard.add_hotkey('f10', main)
keyboard.wait('esc')