-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpieces.py
326 lines (235 loc) · 9.92 KB
/
pieces.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
"""
Collection of base pieces used in Tetris game.
"""
from enum import Enum
import random
def get_positions_from_rotation(rotation_block, orientation_enum):
positions = list()
for y_offset, x_offset in orientation_enum.value:
new_position = (
rotation_block[0] + y_offset,
rotation_block[1] + x_offset
)
positions.append(new_position)
return positions
class AbstractPiece:
def __init__(self, initial_rotation_block_position=(1, 5)):
# Only position of rotation block will be constantly maintained - positions of other blocks can be
# calculated using rotation block and offsets relative to this block (encoded in orientation enum in this case)
self.rotation_block = initial_rotation_block_position
self.orientation = None
self.requested_rotation_block = None
self.requested_orientation = None
# Only for drawing - needs to be maintained in order to erase previous positions of the piece from window
self.previous_rotation_block = None
self.previous_orientation = None
def __hash__(self):
return hash(self.__class__.__name__)
def __eq__(self, other):
if isinstance(self, other.__class__):
return True
return False
def accept_move(self):
self.previous_rotation_block = self.rotation_block
if self.requested_rotation_block:
self.rotation_block = self.requested_rotation_block
self.previous_orientation = self.orientation
if self.requested_orientation:
self.orientation = self.requested_orientation
def reject_move(self):
self.requested_rotation_block = None
self.requested_orientation = None
@property
def previous_positions(self):
if self.previous_rotation_block:
return get_positions_from_rotation(self.previous_rotation_block, self.previous_orientation)
return None
@property
def current_positions(self):
return get_positions_from_rotation(self.rotation_block, self.orientation)
def move_right(self):
self.requested_rotation_block = (
self.rotation_block[0],
self.rotation_block[1] + 1
)
return get_positions_from_rotation(self.requested_rotation_block, self.orientation)
def move_left(self):
self.requested_rotation_block = (
self.rotation_block[0],
self.rotation_block[1] - 1
)
return get_positions_from_rotation(self.requested_rotation_block, self.orientation)
def advance(self):
self.requested_rotation_block = (
self.rotation_block[0] + 1,
self.rotation_block[1]
)
return get_positions_from_rotation(self.requested_rotation_block, self.orientation)
def rotate_clockwise(self):
raise NotImplementedError()
def rotate_anti_clockwise(self):
raise NotImplementedError()
class Square(AbstractPiece):
"""
0 1
2 3
Blocks numeration, rotation block = 1
"""
class _Orientation(Enum):
CONSTANT = ((0, -1), (0, 0), (1, -1), (1, 0))
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.orientation = Square._Orientation.CONSTANT
def rotate_clockwise(self):
# Square doesn't rotate so return current positions
return get_positions_from_rotation(self.rotation_block, self.orientation)
def rotate_anti_clockwise(self):
return self.rotate_clockwise()
class LongBar(AbstractPiece):
"""
0 1 2 3
Blocks numeration with orientation == _VERTICAL, rotation block = 2
"""
class _Orientation(Enum):
VERTICAL = ((0, -2), (0, -1), (0, 0), (0, 1))
HORIZONTAL = ((2, 0), (1, 0), (0, 0), (-1, 0))
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.orientation = LongBar._Orientation.VERTICAL
def rotate_clockwise(self):
if self.orientation == LongBar._Orientation.VERTICAL:
self.requested_orientation = LongBar._Orientation.HORIZONTAL
else:
self.requested_orientation = LongBar._Orientation.VERTICAL
return get_positions_from_rotation(self.rotation_block, self.requested_orientation)
def rotate_anti_clockwise(self):
return self.rotate_clockwise()
class L_Piece(AbstractPiece):
"""
1 2 3
0
Blocks numeration when facing down (_Orientation.DOWN, rotation block = 2)
"""
class _Orientation(Enum):
# abstract direction -> y, x offsets relative to rotation block
DOWN = ((1, -1), (0, -1), (0, 0), (0, 1))
LEFT = ((-1, -1), (-1, 0), (0, 0), (1, 0))
UP = ((-1, 1), (0, 1), (0, 0), (0, -1))
RIGHT = ((1, 1), (1, 0), (0, 0), (-1, 0))
_clockwise_rotation = {
_Orientation.DOWN: _Orientation.LEFT,
_Orientation.LEFT: _Orientation.UP,
_Orientation.UP: _Orientation.RIGHT,
_Orientation.RIGHT: _Orientation.DOWN,
}
_anti_clockwise_rotation = {v: k for k, v in _clockwise_rotation.items()}
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.orientation = L_Piece._Orientation.DOWN
def rotate_clockwise(self):
self.requested_orientation = L_Piece._clockwise_rotation[self.orientation]
return get_positions_from_rotation(self.rotation_block, self.requested_orientation)
def rotate_anti_clockwise(self):
self.requested_orientation = L_Piece._anti_clockwise_rotation[self.orientation]
return get_positions_from_rotation(self.rotation_block, self.requested_orientation)
class J_Piece(AbstractPiece):
"""
0 1 2
3
Blocks numeration when facing down (_Orientation.DOWN, rotation block = 1)
"""
class _Orientation(Enum):
# abstract direction -> y, x offsets relative to rotation block
DOWN = ((0, -1), (0, 0), (0, 1), (1, 1))
LEFT = ((-1, 0), (0, 0), (1, 0), (1, -1))
UP = ((0, 1), (0, 0), (0, -1), (-1, -1))
RIGHT = ((1, 0), (0, 0), (-1, 0), (-1, 1))
_clockwise_rotation = {
_Orientation.DOWN: _Orientation.LEFT,
_Orientation.LEFT: _Orientation.UP,
_Orientation.UP: _Orientation.RIGHT,
_Orientation.RIGHT: _Orientation.DOWN,
}
_anti_clockwise_rotation = {v: k for k, v in _clockwise_rotation.items()}
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.orientation = J_Piece._Orientation.DOWN
def rotate_clockwise(self):
self.requested_orientation = J_Piece._clockwise_rotation[self.orientation]
return get_positions_from_rotation(self.rotation_block, self.requested_orientation)
def rotate_anti_clockwise(self):
self.requested_orientation = J_Piece._anti_clockwise_rotation[self.orientation]
return get_positions_from_rotation(self.rotation_block, self.requested_orientation)
class Z_Piece(AbstractPiece):
"""
0 1
2 3
Blocks numeration when facing left (_Orientation.LEFT, rotation block = 1)
"""
class _Orientation(Enum):
# abstract direction -> y, x offsets relative to rotation block
LEFT = ((0, -1), (0, 0), (1, 0), (1, 1))
UP = ((-1, 0), (0, 0), (0, -1), (1, -1))
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.orientation = Z_Piece._Orientation.LEFT
def rotate_clockwise(self):
if self.orientation == Z_Piece._Orientation.UP:
self.requested_orientation = Z_Piece._Orientation.LEFT
else:
self.requested_orientation = Z_Piece._Orientation.UP
return get_positions_from_rotation(self.rotation_block, self.requested_orientation)
def rotate_anti_clockwise(self):
return self.rotate_clockwise()
class S_Piece(AbstractPiece):
"""
0 1
2 3
Blocks numeration when facing right (_Orientation.RIGHT, rotation block = 0)
"""
class _Orientation(Enum):
# abstract direction -> y, x offsets relative to rotation block
RIGHT = ((0, 0), (0, 1), (1, -1), (1, 0))
UP = ((0, 0), (-1, 0), (0, 1), (1, 1))
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.orientation = S_Piece._Orientation.RIGHT
def rotate_clockwise(self):
if self.orientation == S_Piece._Orientation.UP:
self.requested_orientation = S_Piece._Orientation.RIGHT
else:
self.requested_orientation = S_Piece._Orientation.UP
return get_positions_from_rotation(self.rotation_block, self.requested_orientation)
def rotate_anti_clockwise(self):
return self.rotate_clockwise()
class T_Piece(AbstractPiece):
"""
0 1 2
3
Blocks numeration when facing down (_Orientation.DOWN, rotation block = 1)
"""
class _Orientation(Enum):
# abstract direction -> y, x offsets relative to rotation block
DOWN = ((0, -1), (0, 0), (0, 1), (1, 0))
LEFT = ((-1, 0), (0, 0), (1, 0), (0, -1))
UP = ((0, 1), (0, 0), (0, -1), (-1, 0))
RIGHT = ((1, 0), (0, 0), (-1, 0), (0, 1))
_clockwise_rotation = {
_Orientation.DOWN: _Orientation.LEFT,
_Orientation.LEFT: _Orientation.UP,
_Orientation.UP: _Orientation.RIGHT,
_Orientation.RIGHT: _Orientation.DOWN,
}
_anti_clockwise_rotation = {v: k for k, v in _clockwise_rotation.items()}
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.orientation = T_Piece._Orientation.DOWN
def rotate_clockwise(self):
self.requested_orientation = T_Piece._clockwise_rotation[self.orientation]
return get_positions_from_rotation(self.rotation_block, self.requested_orientation)
def rotate_anti_clockwise(self):
self.requested_orientation = T_Piece._anti_clockwise_rotation[self.orientation]
return get_positions_from_rotation(self.rotation_block, self.requested_orientation)
all_pieces = (Square, LongBar, L_Piece, J_Piece, Z_Piece, S_Piece, T_Piece)
def get_random_piece():
return random.choice(all_pieces)