-
Notifications
You must be signed in to change notification settings - Fork 0
/
tetromino.py
140 lines (133 loc) · 3.72 KB
/
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
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
import util
class Tetromino:
def __init__(self, tet_type):
self.type = tet_type
self.grid, self.pos = self.spawn_tet()
self.length = len(self.grid)
self.rotation = 0 # 0 is default, 1 is right, 2 is double, 3 is left
self.ghost_pos = [0, 0]
self.moving = []
self.size = 0
def spawn_tet(self):
_tet = self.type
if _tet == 's':
grid = [
[0, 1, 0],
[1, 1, 0],
[1, 0, 0]
]
_pos = [3, -2]
self.size = 3
elif _tet == 'z':
grid = [
[1, 0, 0],
[1, 1, 0],
[0, 1, 0]
]
_pos = [3, -2]
self.size = 3
elif _tet == 'j':
grid = [
[1, 1, 0],
[0, 1, 0],
[0, 1, 0]
]
_pos = [3, -2]
self.size = 3
elif _tet == 'l':
grid = [
[0, 1, 0],
[0, 1, 0],
[1, 1, 0]
]
_pos = [3, -2]
self.size = 3
elif _tet == 't':
grid = [
[0, 1, 0],
[1, 1, 0],
[0, 1, 0]
]
_pos = [3, -2]
self.size = 3
elif _tet == 'o':
grid = [
[1, 1],
[1, 1]
]
_pos = [4, -2]
self.size = 2
elif _tet == 'i':
grid = [
[0, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 0, 0],
[0, 1, 0, 0]
]
_pos = [3, -2]
self.size = 4
else:
raise Exception("Invalid tet type")
return grid, _pos
def rotate(self, direction):
new_grid = [x[:] for x in self.grid]
length = self.length
if direction == 'left':
for y in range(length):
for x in range(length):
new_grid[x][length - y - 1] = self.grid[y][x]
self.rotation -= 1
if self.rotation == -1:
self.rotation = 3
elif direction == 'right':
for y in range(length):
for x in range(length):
new_grid[length - x - 1][y] = self.grid[y][x]
self.rotation += 1
if self.rotation == 4:
self.rotation = 0
elif direction == 'double':
for y in range(length):
self.grid[y].reverse()
new_grid[length - y - 1] = self.grid[y]
self.rotation += 1
if self.rotation == 4:
self.rotation = 0
self.rotation += 1
if self.rotation == 4:
self.rotation = 0
self.grid = [x[:] for x in new_grid]
def get_pos(self):
return self.pos[0], self.pos[1]
def take_op(self, op, kick = None):
# 0: go down 1 grid
if op == 0:
self.pos[1] += 1
return
# 1: go left
if op == 1:
self.pos[0] -= 1
return
# 2: go right
if op == 2:
self.pos[0] += 1
return
# 3: turn left
if op == 3:
self.rotate("left")
if kick != None:
self.pos[0] += kick[0]
self.pos[1] += kick[1]
return
# 4: turn right
if op == 4:
self.rotate("right")
if kick != None:
self.pos[0] += kick[0]
self.pos[1] += kick[1]
return
# 5: turn down
if op == 5:
self.rotate('double')
return
return