-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblocks.py
277 lines (217 loc) · 8.34 KB
/
blocks.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
colors = [' ', 'cyan', 'yellow', 'magenta', 'green', 'red', 'blue', 'grey']
CLOCKWISE = 1
COUNTERCLOCKWISE = -1
def runtest(grid, px, test):
"""Tests if applying test to px moves px to unoccupied space"""
for p in px:
if(p[0]+test[0] >= grid.n or p[0]+test[0] < 0):
return False
if(p[1]-test[1] >= grid.m or p[1]-test[1] < 0):
return False
if(isoccupied([p[0]+test[0], p[1]-test[1]], grid)):
return False
return True
def isoccupied(pixel, grid):
p = grid(pixel[1], pixel[0])
if(p > 0 and p < 8):
return True
return False
class OccupiedError(Exception):
pass
class RotationError(Exception):
pass
class TetrisBlock:
"""Base for block classes. THIS CLASS SHOULD NOT BE INSTANTIATED."""
rotdict = {0:[]}
testdict = {0:[]}
col = 0
def __init__(self, grid):
"""Initiates the block on the given grid.\
Defines _grid, _center, _pixels, _col and _rot"""
self._grid = grid
self._center = [0,0]
self._pixels = []
self._rot = 0
def move(self, x, y):
"""Moves the block x times right and y times down.\
Raises OccupiedError if desired pixels are occupied."""
for p in self._pixels:
if(p[1]+y >= self._grid.m or p[0]+x >= self._grid.n):
raise IndexError("Cannot move block outside of grid.")
if(p[1]+y < 0 or p[0]+x < 0):
raise IndexError("Cannot move block outside of grid.")
self.remself()
for p in self._pixels:
if(isoccupied([p[0]+x, p[1]+y], self._grid)):
self.updategrid()
raise OccupiedError("Can't move block to ["+str(p[0]+x)+","+str(p[1]+y)+"].")
self._center[0] += x
self._center[1] += y
for p in self._pixels:
p[0] += x
p[1] += y
self.updategrid()
def rot(self, direction=CLOCKWISE):
"""Rotates the block clockwise.\
COUNTERCLOCKWISE (-1) should rotate counterclockwise.
Default:
* direction=CLOCKWISE"""
prev = self._rot
self._rot = (self._rot + direction)%4
tests = self.testdict[(prev, self._rot)]
self.remself()
x, y = self._center
pixels = [[int(p[0]+x), int(p[1]+y)] for p in self.rotdict[self._rot]]
for t in tests:
if(runtest(self._grid, pixels, t)):
self._center[0] += t[0]
self._center[1] -= t[1]
for p in pixels:
p[0] += t[0]
p[1] -= t[1]
break
else:
self._rot = prev
self.updategrid()
raise RotationError("Cannot rotate block.")
self._pixels = pixels
self.updategrid()
def updategrid(self):
"""Updates the grid with the current _pixels list."""
for p in self._pixels:
self._grid[p[1]][p[0]] = self.col
def remself(self):
"""Removes the current pixels from the grid. To be used with self.updategrid() to\
properly modify the grid without leaving remnants."""
for p in self._pixels:
self._grid[p[1]][p[0]] = 0
class IBlock(TetrisBlock):
rotdict = {0:[[-1.5, -0.5], [-0.5, -0.5], [0.5, -0.5], [1.5, -0.5]],
1:[[0.5, -1.5], [0.5, -0.5], [0.5, 0.5], [0.5, 1.5]],
2:[[-1.5, 0.5], [-0.5, 0.5], [0.5, 0.5], [1.5, 0.5]],
3:[[-0.5, -1.5], [-0.5, -0.5], [-0.5, 0.5], [-0.5, 1.5]]}
testdict = {(0, 1):[[0, 0], [-2, 0], [1, 0], [-2, -1], [1, 2]],
(1, 0):[[0, 0], [2, 0], [-1, 0], [2, 1], [-1, -2]],
(1, 2):[[0, 0], [-1, 0], [2, 0], [-1, 2], [2, -1]],
(2, 1):[[0, 0], [1, 0], [-2, 0], [1, -2], [-2, 1]],
(2, 3):[[0, 0], [2, 0], [-1, 0], [2, 1], [-1, -2]],
(3, 2):[[0, 0], [-2, 0], [1, 0], [-2, -1], [1, 2]],
(3, 0):[[0, 0], [1, 0], [-2, 0], [1, -2], [-2, 1]],
(0, 3):[[0, 0], [-1, 0], [2, 0], [-1, 2], [2, -1]]}
col = 1
def __init__(self, grid):
self._grid = grid
self._center = [4.5, 1.5]
self._pixels = [[3, 1], [4, 1], [5, 1], [6, 1]]
self._rot = 0
class OBlock(TetrisBlock):
col = 2
def __init__(self, grid):
self._grid = grid
self._center = [4, 1]
self._pixels = [[4, 0], [5, 0], [4, 1], [5, 1]]
def rot(self):
pass
globaltestdict = {(0, 1):[[0, 0], [-1, 0], [-1, 1], [0, -2], [-1, -2]],
(1, 0):[[0, 0], [1, 0], [1, -1], [0, 2], [1, 2]],
(1, 2):[[0, 0], [1, 0], [1, -1], [0, 2], [1, 2]],
(2, 1):[[0, 0], [-1, 0], [-1, 1], [0, -2], [-1, -2]],
(2, 3):[[0, 0], [1, 0], [1, 1], [0, -2], [1, -2]],
(3, 2):[[0, 0], [-1, 0], [-1, -1], [0, 2], [-1, 2]],
(3, 0):[[0, 0], [-1, 0], [-1, -1], [0, 2], [-1, 2]],
(0, 3):[[0, 0], [1, 0], [1, 1], [0, -2], [1, -2]]}
class TBlock(TetrisBlock):
rotdict = {0:[[0, -1], [-1, 0], [0, 0], [1, 0]],
1:[[0, -1], [0, 0], [1, 0], [0, 1]],
2:[[-1, 0], [0, 0], [1, 0], [0, 1]],
3:[[0, -1], [-1, 0], [0, 0], [0, 1]]}
testdict = globaltestdict
col = 3
def __init__(self, grid):
self._grid = grid
self._center = [4, 1]
self._pixels = [[4, 0], [3, 1], [4, 1], [5, 1]]
self._rot = 0
class SBlock(TetrisBlock):
rotdict = {0:[[0, -1], [1, -1], [-1, 0], [0, 0]],
1:[[0, -1], [0, 0], [1, 0], [1, 1]],
2:[[0, 0], [1, 0], [-1, 1], [0, 1]],
3:[[-1, -1], [-1, 0], [0, 0], [0, 1]]}
testdict = globaltestdict
col = 4
def __init__(self, grid):
self._grid = grid
self._center = [4, 1]
self._pixels = [[4, 0], [5, 0], [3, 1], [4, 1]]
self._rot = 0
class ZBlock(TetrisBlock):
rotdict = {0:[[-1, -1], [0, -1], [0, 0], [1, 0]],
1:[[1, -1], [0, 0], [1, 0], [0, 1]],
2:[[-1, 0], [0, 0], [0, 1], [1, 1]],
3:[[0, -1], [-1, 0], [0, 0], [-1, 1]]}
testdict = globaltestdict
col = 5
def __init__(self, grid):
self._grid = grid
self._center = [4, 1]
self._pixels = [[3, 0], [4, 0], [4, 1], [5, 1]]
self._rot = 0
class JBlock(TetrisBlock):
rotdict = {0:[[-1, -1], [-1, 0], [0, 0], [1, 0]],
1:[[0, -1], [1, -1], [0, 0], [0, 1]],
2:[[-1, 0], [0, 0], [1, 0], [1, 1]],
3:[[0, -1], [0, 0], [-1, 1], [0, 1]]}
testdict = globaltestdict
col = 6
def __init__(self, grid):
self._grid = grid
self._center = [4, 1]
self._pixels = [[3, 0], [3, 1], [4, 1], [5, 1]]
self._rot = 0
class LBlock(TetrisBlock):
rotdict = {0:[[1, -1], [-1, 0], [0, 0], [1, 0]],
1:[[0, -1], [0, 0], [0, 1], [1, 1]],
2:[[-1, 0], [0, 0], [1, 0], [-1, 1]],
3:[[-1, -1], [0, -1], [0, 0], [0, 1]]}
testdict = globaltestdict
col = 7
def __init__(self, grid):
self._grid = grid
self._center = [4, 1]
self._pixels = [[5, 0], [3, 1], [4, 1], [5, 1]]
self._rot = 0
def isin(pixel, pixels):
for p in pixels:
if(p[0] == pixel[0] and p[1] == pixel[1]):
return True
return False
class Ghost:
def __init__(self, block):
self._block = block
self._grid = block._grid
self._pixels = None
self.update()
def update(self):
if(self._pixels != None):
self.remself()
self._pixels = [p[:] for p in self._block._pixels]
ok = True
while True:
for p in self._pixels:
if(p[1]+1 >= self._grid.m):
ok = False
break
occ = isoccupied([p[0], p[1]+1], self._grid)
if(occ and not isin([p[0], p[1]+1], self._block._pixels)):
ok = False
break
if(ok == False): break
for p in self._pixels:
p[1] += 1
for p in self._pixels:
if(isin(p, self._block._pixels)): continue
self._grid[p[1]][p[0]] = 8
def remself(self):
for p in self._pixels:
if(isin(p, self._block._pixels)): continue
self._grid[p[1]][p[0]] = 0