-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpieces.py
More file actions
211 lines (151 loc) · 6.34 KB
/
pieces.py
File metadata and controls
211 lines (151 loc) · 6.34 KB
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
import cfg
from board import Board_Panel
from point import Point
from IllegalMoveError import IllegalMoveError
class Piece:
def __init__(self) -> None:
self.name = None
self.center = Point(cfg.PIECE_STARTING_X, cfg.PIECE_STARTING_Y)
self.orientation = cfg.DEFAULT_ORIENTATION
self.piece_constants: list[list[Point]] = []
def copy(self):
copy = Piece()
copy.name = self.name
copy.center = Point(self.center.getX(), self.center.getY())
copy.orientation = self.orientation
# same reference is fine b/c piece constants never modified
copy.piece_constants = self.piece_constants
return copy
def set_center(self, x, y):
self.center.setX(x)
self.center.setY(y)
def set_orientation(self, orientation):
self.orientation = orientation
def is_action_possible(self, board: Board_Panel):
for i in range(len(self.piece_constants[self.orientation])):
try:
if board.get_value(self.center.getY() + self.piece_constants[self.orientation][i].getY(),
self.center.getX() + self.piece_constants[self.orientation][i].getX(),) != 0:
raise IllegalMoveError
except IndexError:
raise IllegalMoveError
def draw_on_board(self, board: Board_Panel):
for i in range(len(self.piece_constants[self.orientation])):
board.update_board(self.center.getY() + self.piece_constants[self.orientation][i].getY(),
self.center.getX() + self.piece_constants[self.orientation][i].getX(),
self.name)
def move_down(self, board: Board_Panel):
try:
self.remove_piece_from_board(board)
self.center.setY(self.center.getY() + 1)
self.is_action_possible(board)
self.draw_on_board(board)
return True
except IllegalMoveError:
self.center.setY(self.center.getY() - 1)
self.draw_on_board(board)
raise IllegalMoveError
def move_sideways(self, board: Board_Panel, move_right):
try:
self.remove_piece_from_board(board)
if move_right:
self.center.setX(self.center.getX() + 1)
else:
self.center.setX(self.center.getX() - 1)
self.is_action_possible(board)
self.draw_on_board(board)
return True
except IllegalMoveError:
if move_right:
self.center.setX(self.center.getX() - 1)
else:
self.center.setX(self.center.getX() + 1)
self.draw_on_board(board)
return False
def rotate(self, board: Board_Panel, rotate_right):
try:
cur_orientation = self.orientation
self.remove_piece_from_board(board)
if rotate_right:
self.orientation = (self.orientation + 1) % 4
else:
self.orientation = (self.orientation - 1 + 4) % 4
self.is_action_possible(board)
self.draw_on_board(board)
return True
except IllegalMoveError:
self.orientation = cur_orientation
self.draw_on_board(board)
return False
def remove_piece_from_board(self, board: Board_Panel):
for i in range(len(self.piece_constants[self.orientation])):
board.update_board(self.center.getY() + self.piece_constants[self.orientation][i].getY(),
self.center.getX() + self.piece_constants[self.orientation][i].getX(),
0)
class T(Piece):
def __init__(self) -> None:
super().__init__()
self.name = 1
self.piece_constants = [
[Point(0, 0), Point(1, 0), Point(-1, 0), Point(0, -1)],
[Point(0, 0), Point(0, 1), Point(0, -1), Point(1, 0)],
[Point(0, 0), Point(-1, 0), Point(1, 0), Point(0, 1)],
[Point(0, 0), Point(0, -1), Point(0, 1), Point(-1, 0)]
]
class L(Piece):
def __init__(self) -> None:
super().__init__()
self.name = 2
self.piece_constants = [
[Point(0, 0), Point(1, 0), Point(-1, 0), Point(1, -1)],
[Point(0, 0), Point(0, 1), Point(0, -1), Point(1, 1)],
[Point(0, 0), Point(-1, 0), Point(1, 0), Point(-1, 1)],
[Point(0, 0), Point(0, -1), Point(0, 1), Point(-1, -1)]
]
class J(Piece):
def __init__(self) -> None:
super().__init__()
self.name = 3
self.piece_constants = [
[Point(0, 0), Point(1, 0), Point(-1, 0), Point(-1, -1)],
[Point(0, 0), Point(0, 1), Point(0, -1), Point(1, -1)],
[Point(0, 0), Point(-1, 0), Point(1, 0), Point(1, 1)],
[Point(0, 0), Point(0, -1), Point(0, 1), Point(-1, 1)]
]
class O(Piece):
def __init__(self) -> None:
super().__init__()
self.name = 5
self.piece_constants = [
[Point(0, 0), Point(1, 0), Point(0, -1), Point(1, -1)],
]
class S(Piece):
def __init__(self) -> None:
super().__init__()
self.name = 6
self.piece_constants = [
[Point(0, 0), Point(1, -1), Point(-1, 0), Point(0, -1)],
[Point(0, 0), Point(1, 1), Point(0, -1), Point(1, 0)],
[Point(0, 0), Point(-1, 1), Point(1, 0), Point(0, 1)],
[Point(0, 0), Point(-1, -1), Point(0, 1), Point(-1, 0)]
]
class Z(Piece):
def __init__(self) -> None:
super().__init__()
self.name = 7
self.piece_constants = [
[Point(0, 0), Point(-1, -1), Point(1, 0), Point(0, -1)],
[Point(0, 0), Point(1, -1), Point(0, 1), Point(1, 0)],
[Point(0, 0), Point(1, 1), Point(-1, 0), Point(0, 1)],
[Point(0, 0), Point(-1, 1), Point(0, -1), Point(-1, 0)]
]
class I(Piece):
def __init__(self) -> None:
super().__init__()
self.name = 4
self.piece_constants = [
[Point(0, 0), Point(1, 0), Point(2, 0), Point(-1, 0)],
[Point(1, 0), Point(1, 1), Point(1, 2), Point(1, -1)],
[Point(1, 1), Point(0, 1), Point(-1, 1), Point(2, 1)],
[Point(0, 1), Point(0, 0), Point(0, -1), Point(0, 2)]
]