-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurrent_tetromino.py
48 lines (45 loc) · 1008 Bytes
/
current_tetromino.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
import numpy as np
shapes = np.array([
[
[0,1,1,0],
[0,1,1,0]
],[
[0,1,1,0],
[1,1,0,0]
],[
[1,1,1,0],
[0,0,1,0]
],[
[1,1,0,0],
[0,1,1,0]
],[
[1,1,1,0],
[1,0,0,0]
],[
[1,1,1,0],
[0,1,0,0]
],[
[1,1,1,1],
[0,0,0,0]
]
], dtype="bool")
shape_names = ["O","S","J","Z","L","T","I"]
def current_tetromino(tetris):
roi = tetris.game_area().base[1:3,3:7] != 47
matching = np.all(shapes == roi, axis=(1,2))
if matching.sum() < 1:
return None
return shape_names[np.argmax(matching)]
def get_shape(shape_name, crop=False):
ind = shape_names.index(shape_name)
if ind < 0:
return None
shape = shapes[ind]
if crop:
if shape[:,3].sum() == 0:
shape = shape[:,:3]
if shape[:,0].sum() == 0:
shape = shape[:,1:]
if shape[1,:].sum() == 0:
shape = shape[:1,:]
return shape