-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclasses.py
More file actions
342 lines (281 loc) · 12.4 KB
/
classes.py
File metadata and controls
342 lines (281 loc) · 12.4 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
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
import pygame as pg
pg.display.init()
w_size = ((pg.display.get_desktop_sizes()[0][0]) * 0.55, (pg.display.get_desktop_sizes()[0][1]) * 0.9)
class Tile(pg.sprite.Sprite):
tilesize = w_size[1] // 8
def __init__(self, x, y):
super().__init__()
self.x = x
self.y = y
self.color = (255, 255, 255) if (x + y) % 2 == 0 else (51, 102, 0)
self.rect = pg.Rect(x * self.tilesize, y * self.tilesize, self.tilesize, self.tilesize)
self.image = pg.Surface((self.tilesize, self.tilesize))
self.image.fill(self.color)
class Piece(pg.sprite.Sprite):
def __init__(self, x, y, color, type):
super().__init__()
self.x = x
self.y = y
self.color = color
self.image = pg.transform.scale_by(pg.image.load(f"images/{color}/{color}_{type}.png"),w_size[1] / 1440)
self.rect = pg.Rect(x * Tile.tilesize, y * Tile.tilesize, Tile.tilesize, Tile.tilesize)
self.movable_tiles = []
self.moved = False
def kill(self, live_pieces, dead_pieces):
dead_pieces.add(self)
live_pieces.remove(self)
def outline(self, screen):
pg.draw.rect(screen, (255, 0, 0), self.rect, 5)
def check_moves(self, turn, chess_grid, live_pieces):
mt = set()
self.legal_move(chess_grid, live_pieces)
original_x, original_y = self.x, self.y
for coord in self.movable_tiles:
simulated_grid = self.copy_chess_grid(chess_grid)
simulated_grid[original_x][original_y] = None
simulated_piece = simulated_grid[coord[0]][coord[1]] = self.__class__(coord[0], coord[1], self.color)
simulated_piece.moved = self.moved
simulated_pieces = []
for i in range(8):
for j in range(8):
if simulated_grid[i][j] is not None:
simulated_pieces.append(simulated_grid[i][j])
simulated_king = next((p for p in simulated_pieces if isinstance(p, King) and p.color == turn), None)
if not simulated_king.check(turn, simulated_pieces, simulated_grid):
mt.add((coord[0], coord[1]))
return mt
@staticmethod
def copy_chess_grid(chess_grid):
new_grid = {i: {j: None for j in range(8)} for i in range(8)}
for i in range(8):
for j in range(8):
if chess_grid[i][j] is not None:
piece = chess_grid[i][j]
new_grid[i][j] = piece.__class__(piece.x, piece.y, piece.color)
new_grid[i][j].movable_tiles = piece.movable_tiles.copy()
new_grid[i][j].moved = piece.moved
return new_grid
class King(Piece):
def __init__(self, x, y, color):
super().__init__(x, y, color, "king")
def kill(self, live_pieces, dead_pieces):
raise AttributeError("Kings cannot be killed")
def legal_move(self, chess_grid, live_pieces):
lst = set()
for i in range(self.x - 1, self.x + 2):
for j in range(self.y - 1, self.y + 2):
if 0 <= i <= 7 and 0 <= j <= 7 and (i, j) != (self.x, self.y):
if chess_grid[i][j] and chess_grid[i][j].color != self.color:
lst.add((i, j))
elif not chess_grid[i][j]:
lst.add((i, j))
if not self.moved:
try:
l_rook = next((p for p in live_pieces if isinstance(p, Rook) and p.color == self.color and p.x == 0),
None)
r_rook = next((p for p in live_pieces if isinstance(p, Rook) and p.color == self.color and p.x == 7),
None)
if not chess_grid[1][self.y] and not chess_grid[2][self.y] and not chess_grid[3][
self.y] and not l_rook.moved:
lst.add((1, self.y))
if not chess_grid[5][self.y] and not chess_grid[6][self.y] and not r_rook.moved:
lst.add((6, self.y))
except AttributeError:
pass
self.movable_tiles = lst
def castle(self, target, chess_grid):
if self.moved or target.moved:
return
if self.color == "white":
row = 7
else:
row = 0
if target.x == 7:
path = [5, 6]
king_new_pos, rook_new_pos = (6, row), (5, row)
elif target.x == 0:
path = [1, 2, 3]
king_new_pos, rook_new_pos = (2, row), (3, row)
else:
return
for x in path:
if chess_grid[x][row]:
return
chess_grid[self.x][self.y] = None
chess_grid[target.x][target.y] = None
self.x, self.y = king_new_pos
target.x, target.y = rook_new_pos
self.rect.topleft = (king_new_pos[0] * Tile.tilesize, king_new_pos[1] * Tile.tilesize)
target.rect.topleft = (target.x * Tile.tilesize, target.y * Tile.tilesize)
chess_grid[self.x][self.y] = self
chess_grid[target.x][target.y] = target
def check(self, turn, live_pieces, chess_grid):
enemy_pieces = [piece for piece in live_pieces if piece.color != turn]
king_pos = (self.x, self.y)
for piece in enemy_pieces:
piece.legal_move(chess_grid, live_pieces)
for piece in enemy_pieces:
if king_pos in piece.movable_tiles:
return True
return False
class Rook(Piece):
def __init__(self, x, y, color):
super().__init__(x, y, color, "rook")
def legal_move(self, chess_grid, live_pieces):
lst = set()
for i in range(self.x + 1, 8):
if chess_grid[i][self.y]:
if chess_grid[i][self.y].color != self.color:
lst.add((i, self.y))
break
else:
lst.add((i, self.y))
for i in range(self.x - 1, -1, -1):
if chess_grid[i][self.y]:
if chess_grid[i][self.y].color != self.color:
lst.add((i, self.y))
break
else:
lst.add((i, self.y))
for j in range(self.y + 1, 8):
if chess_grid[self.x][j]:
if chess_grid[self.x][j].color != self.color:
lst.add((self.x, j))
break
else:
lst.add((self.x, j))
for j in range(self.y - 1, -1, -1):
if chess_grid[self.x][j]:
if chess_grid[self.x][j].color != self.color:
lst.add((self.x, j))
break
else:
lst.add((self.x, j))
self.movable_tiles = lst
class Bishop(Piece):
def __init__(self, x, y, color):
super().__init__(x, y, color, "bishop")
def legal_move(self, chess_grid, live_pieces):
lst = set()
directions = [(1, 1), (1, -1), (-1, 1), (-1, -1)]
for dx, dy in directions:
i, j = self.x + dx, self.y + dy
while 0 <= i <= 7 and 0 <= j <= 7:
if chess_grid[i][j]:
if chess_grid[i][j].color != self.color:
lst.add((i, j))
break
else:
lst.add((i, j))
i += dx
j += dy
self.movable_tiles = lst
class Queen(Piece):
def __init__(self, x, y, color):
super().__init__(x, y, color, "queen")
def legal_move(self, chess_grid, live_pieces):
lst = set()
b = Bishop(self.x, self.y, self.color)
r = Rook(self.x, self.y, self.color)
b.legal_move(chess_grid, live_pieces)
r.legal_move(chess_grid, live_pieces)
self.movable_tiles = b.movable_tiles | r.movable_tiles
class Knight(Piece):
def __init__(self, x, y, color):
super().__init__(x, y, color, "knight")
def legal_move(self, chess_grid, live_pieces):
lst = set()
for i in range(-2, 3):
for j in range(-2, 3):
if abs(i) + abs(j) == 3:
if 0 <= self.x + i <= 7 and 0 <= self.y + j <= 7:
if chess_grid[self.x + i][self.y + j] and chess_grid[self.x + i][
self.y + j].color != self.color:
lst.add((self.x + i, self.y + j))
lst.add((self.x + i, self.y + j))
self.movable_tiles = lst
class Pawn(Piece):
def __init__(self, x, y, color):
super().__init__(x, y, color, "pawn")
self.en_passant = False
def legal_move(self, chess_grid, live_pieces):
lst = set()
try:
if not self.moved:
if self.color == 'white':
if not chess_grid[self.x][self.y - 1]:
lst.add((self.x, self.y - 1))
if not chess_grid[self.x][self.y - 2]:
lst.add((self.x, self.y - 2))
else:
if not chess_grid[self.x][self.y + 1]:
lst.add((self.x, self.y + 1))
if not chess_grid[self.x][self.y + 2]:
lst.add((self.x, self.y + 2))
else:
if self.color == 'white':
if not chess_grid[self.x][self.y - 1]:
lst.add((self.x, self.y - 1))
else:
if not chess_grid[self.x][self.y + 1]:
lst.add((self.x, self.y + 1))
if self.color == 'white':
if self.x + 1 <= 7 and self.y - 1 >= 0 and chess_grid[self.x + 1][self.y - 1] and \
chess_grid[self.x + 1][self.y - 1].color != self.color:
lst.add((self.x + 1, self.y - 1))
if self.x - 1 >= 0 and self.y - 1 >= 0 and chess_grid[self.x - 1][self.y - 1] and \
chess_grid[self.x - 1][self.y - 1].color != self.color:
lst.add((self.x - 1, self.y - 1))
else:
if self.x + 1 <= 7 and self.y + 1 <= 7 and chess_grid[self.x + 1][self.y + 1] and \
chess_grid[self.x + 1][self.y + 1].color != self.color:
lst.add((self.x + 1, self.y + 1))
if self.x - 1 >= 0 and self.y + 1 <= 7 and chess_grid[self.x - 1][self.y + 1] and \
chess_grid[self.x - 1][self.y + 1].color != self.color:
lst.add((self.x - 1, self.y + 1))
if self.x + 1 <= 7:
right_piece = chess_grid[self.x + 1][self.y]
if isinstance(right_piece, Pawn) and right_piece.color != self.color and right_piece.en_passant:
target_y = self.y - 1 if self.color == 'white' else self.y + 1
if 0 <= target_y <= 7:
lst.add((self.x + 1, target_y))
if self.x - 1 >= 0:
left_piece = chess_grid[self.x - 1][self.y]
if isinstance(left_piece, Pawn) and left_piece.color != self.color and left_piece.en_passant:
target_y = self.y - 1 if self.color == 'white' else self.y + 1
if 0 <= target_y <= 7:
lst.add((self.x - 1, target_y))
except (AttributeError, KeyError):
pass
self.movable_tiles = lst
@staticmethod
def promote(self, screen, live_pieces, dead_pieces, turn):
popup = True
selected = None
pieces = []
while popup:
promos = pg.sprite.Group()
promos.add(
Queen(2, 3, turn),
Bishop(3, 3, turn),
Knight(4, 3, turn),
Rook(5, 3, turn))
for event in pg.event.get():
if event.type == pg.QUIT:
pg.quit()
quit()
elif event.type == pg.MOUSEBUTTONUP:
mouse_pos = pg.mouse.get_pos()
for piece in promos:
if piece.rect.collidepoint(mouse_pos):
selected = type(piece).__name__
popup = False
promos.empty()
break
pieces = {"Queen": Queen, "Bishop": Bishop, "Knight": Knight, "Rook": Rook}
screen.fill((255, 87, 51))
promos.draw(screen)
pg.display.flip()
self.kill(live_pieces, dead_pieces)
self = pieces[selected](self.x, self.y, turn)
live_pieces.add(self)