-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpieces.py
580 lines (438 loc) · 20.3 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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
import pygame
class Piece:
def __init__(self, color: str, piece_type: str):
self.color = color
self.type = piece_type
self.image_path = f"Sprites/{color} {piece_type}.png"
self.image = pygame.image.load(self.image_path)
self.move_count = 0
if self.type == "pawn":
self.en_passant = False
def copy(self):
return Piece(self.color, self.type)
# b -- board, selected_p -- selected_piece, p_pos -- piece_pos, n -- number_of_repeats
def possible_moves(b, selected_p, p_pos, n=0):
pos_moves = [] # pos_moves -- possible_moves
if selected_p.type == "king":
pos_moves = [(p_pos[0], p_pos[1] - 1), (p_pos[0] + 1, p_pos[1] - 1), (p_pos[0] + 1, p_pos[1]),
(p_pos[0] + 1, p_pos[1] + 1), (p_pos[0], p_pos[1] + 1), (p_pos[0] - 1, p_pos[1] + 1),
(p_pos[0] - 1, p_pos[1]), (p_pos[0] - 1, p_pos[1] - 1)]
if selected_p.move_count == 0:
if b[p_pos[1]][p_pos[0] + 3] != "'":
if b[p_pos[1]][p_pos[0] + 3].type == "rook":
if b[p_pos[1]][p_pos[0] + 3].move_count == 0:
if b[p_pos[1]][p_pos[0] + 1] == "'" and b[p_pos[1]][p_pos[0] + 2] == "'":
cell_under_attack = check_around_for_pieces(b, selected_p.color, (p_pos[0] + 1, p_pos[1]))
king_in_check = check_around_for_pieces(b, selected_p.color, (p_pos[0], p_pos[1]))
if not cell_under_attack and not king_in_check:
pos_moves.append((p_pos[0] + 2, p_pos[1]))
if b[p_pos[1]][p_pos[0] - 4] != "'":
if b[p_pos[1]][p_pos[0] - 4].type == "rook":
if b[p_pos[1]][p_pos[0] - 4].move_count == 0:
if b[p_pos[1]][p_pos[0] - 3] == "'" and b[p_pos[1]][p_pos[0] - 2] == "'" and \
b[p_pos[1]][p_pos[0] - 1] == "'":
cell_under_attack = check_around_for_pieces(b, selected_p.color,
(p_pos[0] - 1, p_pos[1]))
king_in_check = check_around_for_pieces(b, selected_p.color, (p_pos[0], p_pos[1]))
if not cell_under_attack and not king_in_check:
pos_moves.append((p_pos[0] - 2, p_pos[1]))
elif selected_p.type == "knight":
pos_moves = [(p_pos[0] - 1, p_pos[1] - 2), (p_pos[0] + 1, p_pos[1] - 2), (p_pos[0] + 2, p_pos[1] - 1),
(p_pos[0] + 2, p_pos[1] + 1), (p_pos[0] + 1, p_pos[1] + 2), (p_pos[0] - 1, p_pos[1] + 2),
(p_pos[0] - 2, p_pos[1] + 1), (p_pos[0] - 2, p_pos[1] - 1)]
elif selected_p.type == "rook":
i = 1
try:
while b[p_pos[1]][p_pos[0] + i] == "'" and p_pos[0] + i < 8:
pos_moves.append((p_pos[0] + i, p_pos[1]))
i += 1
if b[p_pos[1]][p_pos[0] + i] != "'" and p_pos[0] + i < 8:
if b[p_pos[1]][p_pos[0] + i].color != selected_p.color:
pos_moves.append((p_pos[0] + i, p_pos[1]))
except IndexError:
pass
i = 1
try:
while b[p_pos[1] + i][p_pos[0]] == "'" and p_pos[1] + i < 8:
pos_moves.append((p_pos[0], p_pos[1] + i))
i += 1
if b[p_pos[1] + i][p_pos[0]] != "'" and p_pos[1] + i < 8:
if b[p_pos[1] + i][p_pos[0]].color != selected_p.color:
pos_moves.append((p_pos[0], p_pos[1] + i))
except IndexError:
pass
i = -1
try:
while b[p_pos[1]][p_pos[0] + i] == "'" and p_pos[0] + i >= 0:
pos_moves.append((p_pos[0] + i, p_pos[1]))
i -= 1
if b[p_pos[1]][p_pos[0] + i] != "'" and p_pos[0] + i >= 0:
if b[p_pos[1]][p_pos[0] + i].color != selected_p.color:
pos_moves.append((p_pos[0] + i, p_pos[1]))
except IndexError:
pass
i = -1
try:
while b[p_pos[1] + i][p_pos[0]] == "'" and p_pos[1] + i >= 0:
pos_moves.append((p_pos[0], p_pos[1] + i))
i -= 1
if b[p_pos[1] + i][p_pos[0]] != "'" and p_pos[1] + i >= 0:
if b[p_pos[1] + i][p_pos[0]].color != selected_p.color:
pos_moves.append((p_pos[0], p_pos[1] + i))
except IndexError:
pass
elif selected_p.type == "bishop":
i = 1
j = 1
try:
while b[p_pos[1] + j][p_pos[0] + i] == "'" and p_pos[0] + i < 8 and p_pos[1] + j < 8:
pos_moves.append((p_pos[0] + i, p_pos[1] + j))
i += 1
j += 1
if b[p_pos[1] + j][p_pos[0] + i] != "'" and p_pos[0] + i < 8 and p_pos[1] + j < 8:
if b[p_pos[1] + j][p_pos[0] + i].color != selected_p.color:
pos_moves.append((p_pos[0] + i, p_pos[1] + j))
except IndexError:
pass
i = -1
j = 1
try:
while b[p_pos[1] + j][p_pos[0] + i] == "'" and p_pos[0] + i >= 0 and p_pos[1] + j < 8:
pos_moves.append((p_pos[0] + i, p_pos[1] + j))
i -= 1
j += 1
if b[p_pos[1] + j][p_pos[0] + i] != "'" and p_pos[0] + i >= 0 and p_pos[1] + j < 8:
if b[p_pos[1] + j][p_pos[0] + i].color != selected_p.color:
pos_moves.append((p_pos[0] + i, p_pos[1] + j))
except IndexError:
pass
i = 1
j = -1
try:
while b[p_pos[1] + j][p_pos[0] + i] == "'" and p_pos[0] + i < 8 and p_pos[1] + j >= 0:
pos_moves.append((p_pos[0] + i, p_pos[1] + j))
i += 1
j -= 1
if b[p_pos[1] + j][p_pos[0] + i] != "'" and p_pos[0] + i < 8 and p_pos[1] + j >= 0:
if b[p_pos[1] + j][p_pos[0] + i].color != selected_p.color:
pos_moves.append((p_pos[0] + i, p_pos[1] + j))
except IndexError:
pass
i = -1
j = -1
try:
while b[p_pos[1] + j][p_pos[0] + i] == "'" and p_pos[0] + i >= 0 and p_pos[1] + j >= 0:
pos_moves.append((p_pos[0] + i, p_pos[1] + j))
i -= 1
j -= 1
if b[p_pos[1] + j][p_pos[0] + i] != "'" and p_pos[0] + i >= 0 and p_pos[1] + j >= 0:
if b[p_pos[1] + j][p_pos[0] + i].color != selected_p.color:
pos_moves.append((p_pos[0] + i, p_pos[1] + j))
except IndexError:
pass
elif selected_p.type == "queen":
i = 1
try:
while b[p_pos[1]][p_pos[0] + i] == "'" and p_pos[0] + i < 8:
pos_moves.append((p_pos[0] + i, p_pos[1]))
i += 1
if b[p_pos[1]][p_pos[0] + i] != "'" and p_pos[0] + i < 8:
if b[p_pos[1]][p_pos[0] + i].color != selected_p.color:
pos_moves.append((p_pos[0] + i, p_pos[1]))
except IndexError:
pass
i = 1
try:
while b[p_pos[1] + i][p_pos[0]] == "'" and p_pos[1] + i < 8:
pos_moves.append((p_pos[0], p_pos[1] + i))
i += 1
if b[p_pos[1] + i][p_pos[0]] != "'" and p_pos[1] + i < 8:
if b[p_pos[1] + i][p_pos[0]].color != selected_p.color:
pos_moves.append((p_pos[0], p_pos[1] + i))
except IndexError:
pass
i = -1
try:
while b[p_pos[1]][p_pos[0] + i] == "'" and p_pos[0] + i >= 0:
pos_moves.append((p_pos[0] + i, p_pos[1]))
i -= 1
if b[p_pos[1]][p_pos[0] + i] != "'" and p_pos[0] + i >= 0:
if b[p_pos[1]][p_pos[0] + i].color != selected_p.color:
pos_moves.append((p_pos[0] + i, p_pos[1]))
except IndexError:
pass
i = -1
try:
while b[p_pos[1] + i][p_pos[0]] == "'" and p_pos[1] + i >= 0:
pos_moves.append((p_pos[0], p_pos[1] + i))
i -= 1
if b[p_pos[1] + i][p_pos[0]] != "'" and p_pos[1] + i >= 0:
if b[p_pos[1] + i][p_pos[0]].color != selected_p.color:
pos_moves.append((p_pos[0], p_pos[1] + i))
except IndexError:
pass
i = 1
j = 1
try:
while b[p_pos[1] + j][p_pos[0] + i] == "'" and p_pos[0] + i < 8 and p_pos[1] + j < 8:
pos_moves.append((p_pos[0] + i, p_pos[1] + j))
i += 1
j += 1
if b[p_pos[1] + j][p_pos[0] + i] != "'" and p_pos[0] + i < 8 and p_pos[1] + j < 8:
if b[p_pos[1] + j][p_pos[0] + i].color != selected_p.color:
pos_moves.append((p_pos[0] + i, p_pos[1] + j))
except IndexError:
pass
i = -1
j = 1
try:
while b[p_pos[1] + j][p_pos[0] + i] == "'" and p_pos[0] + i >= 0 and p_pos[1] + j < 8:
pos_moves.append((p_pos[0] + i, p_pos[1] + j))
i -= 1
j += 1
if b[p_pos[1] + j][p_pos[0] + i] != "'" and p_pos[0] + i >= 0 and p_pos[1] + j < 8:
if b[p_pos[1] + j][p_pos[0] + i].color != selected_p.color:
pos_moves.append((p_pos[0] + i, p_pos[1] + j))
except IndexError:
pass
i = 1
j = -1
try:
while b[p_pos[1] + j][p_pos[0] + i] == "'" and p_pos[0] + i < 8 and p_pos[1] + j >= 0:
pos_moves.append((p_pos[0] + i, p_pos[1] + j))
i += 1
j -= 1
if b[p_pos[1] + j][p_pos[0] + i] != "'" and p_pos[0] + i < 8 and p_pos[1] + j >= 0:
if b[p_pos[1] + j][p_pos[0] + i].color != selected_p.color:
pos_moves.append((p_pos[0] + i, p_pos[1] + j))
except IndexError:
pass
i = -1
j = -1
try:
while b[p_pos[1] + j][p_pos[0] + i] == "'" and p_pos[0] + i >= 0 and p_pos[1] + j >= 0:
pos_moves.append((p_pos[0] + i, p_pos[1] + j))
i -= 1
j -= 1
if b[p_pos[1] + j][p_pos[0] + i] != "'" and p_pos[0] + i >= 0 and p_pos[1] + j >= 0:
if b[p_pos[1] + j][p_pos[0] + i].color != selected_p.color:
pos_moves.append((p_pos[0] + i, p_pos[1] + j))
except IndexError:
pass
elif selected_p.type == "pawn":
if selected_p.color == "black":
try:
if b[p_pos[1]][p_pos[0] - 1].en_passant:
pos_moves.append((p_pos[0] - 1, p_pos[1] + 1))
except (AttributeError, IndexError):
pass
try:
if b[p_pos[1]][p_pos[0] + 1].en_passant:
pos_moves.append((p_pos[0] + 1, p_pos[1] + 1))
except (AttributeError, IndexError):
pass
try:
if b[p_pos[1] + 1][p_pos[0]] == "'":
pos_moves.append((p_pos[0], p_pos[1] + 1))
except IndexError:
pass
try:
if p_pos[1] == 1 and b[p_pos[1] + 2][p_pos[0]] == "'" and b[p_pos[1] + 1][p_pos[0]] == "'":
pos_moves.append((p_pos[0], p_pos[1] + 2))
except IndexError:
pass
try:
if b[p_pos[1] + 1][p_pos[0] + 1] != "'":
pos_moves.append((p_pos[0] + 1, p_pos[1] + 1))
except IndexError:
pass
try:
if b[p_pos[1] + 1][p_pos[0] - 1] != "'":
pos_moves.append((p_pos[0] - 1, p_pos[1] + 1))
except IndexError:
pass
elif selected_p.color == "white":
try:
if b[p_pos[1]][p_pos[0] - 1].en_passant:
pos_moves.append((p_pos[0] - 1, p_pos[1] - 1))
except (IndexError, AttributeError):
pass
try:
if b[p_pos[1]][p_pos[0] + 1].en_passant:
pos_moves.append((p_pos[0] + 1, p_pos[1] - 1))
except (IndexError, AttributeError):
pass
try:
if b[p_pos[1] - 1][p_pos[0]] == "'":
pos_moves.append((p_pos[0], p_pos[1] - 1))
except IndexError:
pass
try:
if p_pos[1] == 6 and b[p_pos[1] - 2][p_pos[0]] == "'" and b[p_pos[1] - 1][p_pos[0]] == "'":
pos_moves.append((p_pos[0], p_pos[1] - 2))
except IndexError:
pass
try:
if b[p_pos[1] - 1][p_pos[0] + 1] != "'":
pos_moves.append((p_pos[0] + 1, p_pos[1] - 1))
except IndexError:
pass
try:
if b[p_pos[1] - 1][p_pos[0] - 1] != "'":
pos_moves.append((p_pos[0] - 1, p_pos[1] - 1))
except IndexError:
pass
pos_moves = check_for_possibility(b, pos_moves, selected_p, p_pos, n)
return pos_moves
# b -- board, l_of_moves -- list_of_moves, p_pos -- piece_pos, n -- number of repeats
def check_for_possibility(b, l_of_moves, selected_p, p_pos, n=0):
new_l_of_moves = []
king_pos = None
if selected_p != "king":
king_pos = return_king_pos(b, selected_p.color)
for move in l_of_moves:
try:
if move[0] >= 0 <= move[1]:
if b[move[1]][move[0]] == "'" or b[move[1]][move[0]].color != selected_p.color:
if n == 0:
new_b = simulate_move(b, move, p_pos, selected_p)
if selected_p.type == "king":
king_pos = move
king_in_check = check_around_for_pieces(new_b, selected_p.color, king_pos)
if not king_in_check:
new_l_of_moves.append(move)
else:
new_l_of_moves.append(move)
except (IndexError, AttributeError):
pass
return new_l_of_moves
def simulate_move(b, move, p_pos, selected_p): # b -- board, p_pos -- piece_position
new_b = []
for row in b:
new_row = []
for cell in row:
if cell != "'":
new_row.append(cell.copy())
else:
new_row.append("'")
new_b.append(new_row)
new_b[p_pos[1]][p_pos[0]] = "'"
new_b[move[1]][move[0]] = selected_p
# if the move is en-passant
if selected_p.type == "pawn":
if p_pos[0] != move[0] and b[move[1]][move[0]] == "'":
new_b[p_pos[1]][move[0]] = "'"
# if the move is castling
if selected_p.type == "king":
if p_pos[0] - move[0] == 2:
new_b[p_pos[1]][move[0] + 1] = new_b[p_pos[1]][0]
new_b[p_pos[1]][0] = "'"
elif p_pos[0] - move[0] == -2:
new_b[p_pos[1]][move[0] - 1] = new_b[p_pos[1]][7]
new_b[p_pos[1]][7] = "'"
return new_b
def return_king_pos(b, color):
king_pos = None
for row_counter, row in enumerate(b):
for cell_counter, piece in enumerate(row):
if piece != "'":
if piece.type == "king" and piece.color == color:
king_pos = (cell_counter, row_counter)
return king_pos
def return_enemies_moves(b, friend_color, n):
enemies_moves = []
for row_counter, row in enumerate(b):
for cell_counter, piece in enumerate(row):
if piece != "'":
if piece.color != friend_color:
moves = possible_moves(b, piece, (cell_counter, row_counter), n)
for move in moves:
enemies_moves.append(move)
return enemies_moves
def check_around_for_pieces(b, color, p_pos): # b -- board
piece_under_attack = False
# check for pawns
direction_to_check = -1 if color == "white" else 1
enemy_piece_pos_to_check = [(p_pos[0] - 1, p_pos[1] + direction_to_check),
(p_pos[0] + 1, p_pos[1] + direction_to_check)]
for enemy_pos in enemy_piece_pos_to_check:
if enemy_pos[0] >= 0 <= enemy_pos[1]:
try:
if b[enemy_pos[1]][enemy_pos[0]].type == "pawn" and b[enemy_pos[1]][enemy_pos[0]].color != color:
piece_under_attack = True
except (IndexError, AttributeError):
pass
if not piece_under_attack:
enemy_piece_pos_to_check = [(p_pos[0] - 1, p_pos[1] - 2), (p_pos[0] + 1, p_pos[1] - 2),
(p_pos[0] + 2, p_pos[1] - 1), (p_pos[0] + 2, p_pos[1] + 1),
(p_pos[0] + 1, p_pos[1] + 2), (p_pos[0] - 1, p_pos[1] + 2),
(p_pos[0] - 2, p_pos[1] + 1), (p_pos[0] - 2, p_pos[1] - 1)]
for enemy_pos in enemy_piece_pos_to_check:
if enemy_pos[0] >= 0 <= enemy_pos[1]:
try:
if b[enemy_pos[1]][enemy_pos[0]].type == "knight" and b[enemy_pos[1]][enemy_pos[0]].color != color:
piece_under_attack = True
except (IndexError, AttributeError):
pass
if not piece_under_attack:
enemy_piece_pos_to_check = [(p_pos[0] + 1, p_pos[1]), (p_pos[0] + 1, p_pos[1] + 1), (p_pos[0], p_pos[1] + 1),
(p_pos[0] - 1, p_pos[1] + 1), (p_pos[0] - 1, p_pos[1]),
(p_pos[0] - 1, p_pos[1] - 1),
(p_pos[0], p_pos[1] - 1), (p_pos[0] + 1, p_pos[1] - 1)]
for enemy_pos in enemy_piece_pos_to_check:
if enemy_pos[0] >= 0 <= enemy_pos[1]:
try:
if b[enemy_pos[1]][enemy_pos[0]].type == "king" and b[enemy_pos[1]][enemy_pos[0]].color != color:
piece_under_attack = True
except (IndexError, AttributeError):
pass
if not piece_under_attack:
append_stages = [(1, 0), (-1, 0), (0, 1), (0, -1)]
for i, j in append_stages:
if not piece_under_attack:
while 8 > p_pos[0] + i >= 0 and 8 > p_pos[1] + j >= 0:
if b[p_pos[1] + j][p_pos[0] + i] != "'":
if (b[p_pos[1] + j][p_pos[0] + i].type == "rook" or
b[p_pos[1] + j][p_pos[0] + i].type == "queen") and \
b[p_pos[1] + j][p_pos[0] + i].color != color:
piece_under_attack = True
else:
break
i += int(i / abs(i)) if i != 0 else 0
j += int(j / abs(j)) if j != 0 else 0
else:
break
if not piece_under_attack:
append_stages = [(1, 1), (1, -1), (-1, 1), (-1, -1)]
for i, j in append_stages:
if not piece_under_attack:
while 8 > p_pos[0] + i >= 0 and 8 > p_pos[1] + j >= 0:
if b[p_pos[1] + j][p_pos[0] + i] != "'":
if (b[p_pos[1] + j][p_pos[0] + i].type == "bishop" or
b[p_pos[1] + j][p_pos[0] + i].type == "queen") and \
b[p_pos[1] + j][p_pos[0] + i].color != color:
piece_under_attack = True
else:
break
i += int(i / abs(i))
j += int(j / abs(j))
else:
break
return piece_under_attack
def print_check_mate(b, winner, p_to_move, king_pos):
if winner is None:
piece_under_attack = check_around_for_pieces(b, p_to_move, king_pos)
having_pos_moves = False
for row_counter, row in enumerate(b):
for cell_counter, piece in enumerate(row):
if piece != "'" and not having_pos_moves:
if piece.color == p_to_move:
if len(possible_moves(b, piece, (cell_counter, row_counter))) > 0:
having_pos_moves = True
if piece_under_attack and not having_pos_moves:
winner = "black" if p_to_move == "white" else "white"
elif not piece_under_attack and not having_pos_moves:
winner = "stalemate"
else:
return winner, True
return winner, piece_under_attack