-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.py
1009 lines (797 loc) · 34.5 KB
/
main.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
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
import os
from typing import List
import pygame
pygame.init()
fps = 30
clock = pygame.time.Clock()
tabletop_size = 800
gameDisplay = pygame.display.set_mode((tabletop_size, tabletop_size))
pygame.display.set_caption("ai-chess")
block_size = tabletop_size / 8
movements_history = []
class PiecePosition(object):
def __init__(self, color: str, piece: str, position: str):
self.color = color
self.piece = piece
self.position = position
self.have_been_moved = False
def __repr__(self):
return f"<PiecePosition - color: {self.color} - piece: {self.piece} - position: {self.position} >"
piece_image = {
"w-P": pygame.image.load(os.path.join("icons", "w-pawn.png")),
"b-P": pygame.image.load(os.path.join("icons", "b-pawn.png")),
"w-R": pygame.image.load(os.path.join("icons", "w-rook.png")),
"b-R": pygame.image.load(os.path.join("icons", "b-rook.png")),
"w-B": pygame.image.load(os.path.join("icons", "w-bishop.png")),
"b-B": pygame.image.load(os.path.join("icons", "b-bishop.png")),
"w-N": pygame.image.load(os.path.join("icons", "w-knight.png")),
"b-N": pygame.image.load(os.path.join("icons", "b-knight.png")),
"w-Q": pygame.image.load(os.path.join("icons", "w-queen.png")),
"b-Q": pygame.image.load(os.path.join("icons", "b-queen.png")),
"w-K": pygame.image.load(os.path.join("icons", "w-king.png")),
"b-K": pygame.image.load(os.path.join("icons", "b-king.png")),
}
pygame.display.set_icon(piece_image["w-N"])
image_size = 96
center_piece_img = (block_size - 96) / 2
piece_positions: List[PiecePosition] = []
piece_positions.append(PiecePosition(color="w", piece="P", position="a2"))
piece_positions.append(PiecePosition(color="w", piece="P", position="b2"))
piece_positions.append(PiecePosition(color="w", piece="P", position="c2"))
piece_positions.append(PiecePosition(color="w", piece="P", position="d2"))
piece_positions.append(PiecePosition(color="w", piece="P", position="e2"))
piece_positions.append(PiecePosition(color="w", piece="P", position="f2"))
piece_positions.append(PiecePosition(color="w", piece="P", position="g2"))
piece_positions.append(PiecePosition(color="w", piece="P", position="h2"))
piece_positions.append(PiecePosition(color="w", piece="R", position="a1"))
piece_positions.append(PiecePosition(color="w", piece="R", position="h1"))
piece_positions.append(PiecePosition(color="w", piece="N", position="b1"))
piece_positions.append(PiecePosition(color="w", piece="N", position="g1"))
piece_positions.append(PiecePosition(color="w", piece="B", position="c1"))
piece_positions.append(PiecePosition(color="w", piece="B", position="f1"))
piece_positions.append(PiecePosition(color="w", piece="Q", position="d1"))
piece_positions.append(PiecePosition(color="w", piece="K", position="e1"))
piece_positions.append(PiecePosition(color="b", piece="P", position="a7"))
piece_positions.append(PiecePosition(color="b", piece="P", position="b7"))
piece_positions.append(PiecePosition(color="b", piece="P", position="c7"))
piece_positions.append(PiecePosition(color="b", piece="P", position="d7"))
piece_positions.append(PiecePosition(color="b", piece="P", position="e7"))
piece_positions.append(PiecePosition(color="b", piece="P", position="f7"))
piece_positions.append(PiecePosition(color="b", piece="P", position="g7"))
piece_positions.append(PiecePosition(color="b", piece="P", position="h7"))
piece_positions.append(PiecePosition(color="b", piece="R", position="a8"))
piece_positions.append(PiecePosition(color="b", piece="R", position="h8"))
piece_positions.append(PiecePosition(color="b", piece="N", position="b8"))
piece_positions.append(PiecePosition(color="b", piece="N", position="g8"))
piece_positions.append(PiecePosition(color="b", piece="B", position="c8"))
piece_positions.append(PiecePosition(color="b", piece="B", position="f8"))
piece_positions.append(PiecePosition(color="b", piece="Q", position="d8"))
piece_positions.append(PiecePosition(color="b", piece="K", position="e8"))
def letter_to_number(letter):
if letter.upper() == "A":
return 0
if letter.upper() == "B":
return 1
if letter.upper() == "C":
return 2
if letter.upper() == "D":
return 3
if letter.upper() == "E":
return 4
if letter.upper() == "F":
return 5
if letter.upper() == "G":
return 6
if letter.upper() == "H":
return 7
def number_to_letter(number):
if number == 0:
return "a"
if number == 1:
return "b"
if number == 2:
return "c"
if number == 3:
return "d"
if number == 4:
return "e"
if number == 5:
return "f"
if number == 6:
return "g"
if number == 7:
return "h"
def get_piece_on_position(position, conditional_piece_positions=None):
if conditional_piece_positions is None:
conditional_piece_positions = piece_positions
for piece_position in conditional_piece_positions:
if piece_position.position == position:
return piece_position
return None
def print_tabletop(conditional_piece_positions=None):
if conditional_piece_positions is None:
conditional_piece_positions = piece_positions
cols = ["a", "b", "c", "d", "e", "f", "g", "h"]
lines = ["8", "7", "6", "5", "4", "3", "2", "1"]
print()
for x, line in enumerate(lines):
for y, col in enumerate(cols):
piece = get_piece_on_position(col + line, conditional_piece_positions)
if (x + y) % 2 == 0:
print(
"\033[0;"
+ ("37" if piece is not None and piece.color == "w" else "30")
+ ";43m"
+ " "
+ (piece.piece if piece is not None else " ")
+ " "
+ "\033[0m",
end="",
)
else:
print(
"\033[0;"
+ ("37" if piece is not None and piece.color == "w" else "30")
+ ";42m"
+ " "
+ (piece.piece if piece is not None else " ")
+ " "
+ "\033[0m",
end="",
)
print()
def am_i_in_check(my_color, conditional_piece_positions: List[PiecePosition]):
my_king_position = ""
if conditional_piece_positions is None:
conditional_piece_positions = piece_positions
for piece_position in conditional_piece_positions:
if piece_position.color == my_color and piece_position.piece == "K":
my_king_position = piece_position.position
for piece_position in conditional_piece_positions:
if piece_position.color != my_color:
if piece_position.piece != "P":
movements = get_valid_movements(
piece_position, True, conditional_piece_positions
)
else:
movements = []
direction = 1
if piece_position.color == "b":
direction = -1
left_piece = None
right_piece = None
number = letter_to_number(piece_position.position[0]) - 1
if number >= 0:
movements.append(
number_to_letter(number)
+ str(int(piece_position.position[1]) + (1 * direction))
)
number = letter_to_number(piece_position.position[0]) + 1
if number <= 7:
movements.append(
number_to_letter(number)
+ str(int(piece_position.position[1]) + (1 * direction))
)
if my_king_position in movements:
return True
return False
def get_valid_movements(
piece, looking_enemy_movements=False, conditional_piece_positions=None
):
valid_movements = []
# pawn
if piece.piece.upper() == "P":
# direction
direction = 1
if piece.color == "b":
direction = -1
# default movement ahead
## if you are in the initial position, you can move 2 houses/squares
position = piece.position[0] + str(int(piece.position[1]) + (1 * direction))
if (
(piece.color == "w" and piece.position[1] == "2")
or (piece.color == "b" and piece.position[1] == "7")
) and get_piece_on_position(
position,
conditional_piece_positions,
) is None:
conditional_piece_positions2 = change_a_piece_position_for_valid_movements(
piece, position
)
if not am_i_in_check(piece.color, conditional_piece_positions2):
valid_movements.append(position)
position = piece.position[0] + str(int(piece.position[1]) + (2 * direction))
if (
get_piece_on_position(
position,
conditional_piece_positions,
)
is None
):
conditional_piece_positions2 = (
change_a_piece_position_for_valid_movements(piece, position)
)
if not am_i_in_check(piece.color, conditional_piece_positions2):
valid_movements.append(position)
## if you aren't on the initial position, you can move only 1 house/square
elif (
get_piece_on_position(
position,
conditional_piece_positions,
)
is None
):
conditional_piece_positions2 = change_a_piece_position_for_valid_movements(
piece, position
)
if not am_i_in_check(piece.color, conditional_piece_positions2):
valid_movements.append(position)
# the 'capture' movement
## default capture
left_piece = None
right_piece = None
number = letter_to_number(piece.position[0]) - 1
if number >= 0:
left_piece = get_piece_on_position(
number_to_letter(number)
+ str(int(piece.position[1]) + (1 * direction)),
conditional_piece_positions,
)
number = letter_to_number(piece.position[0]) + 1
if number <= 7:
right_piece = get_piece_on_position(
number_to_letter(number)
+ str(int(piece.position[1]) + (1 * direction)),
conditional_piece_positions,
)
if left_piece is not None and left_piece.color != piece.color:
conditional_piece_positions2 = change_a_piece_position_for_valid_movements(
piece, left_piece.position
)
if not am_i_in_check(piece.color, conditional_piece_positions2):
valid_movements.append(left_piece.position)
if right_piece is not None and right_piece.color != piece.color:
conditional_piece_positions2 = change_a_piece_position_for_valid_movements(
piece, right_piece.position
)
if not am_i_in_check(piece.color, conditional_piece_positions2):
valid_movements.append(right_piece.position)
## en passant capture
if (piece.position[1] == "5" and piece.color == "w") or (
piece.position[1] == "4" and piece.color == "b"
):
last_movement = movements_history[-1]
if (
last_movement["piece"].piece == "P"
and abs(int(last_movement["from"][1]) - int(last_movement["to"][1]))
== 2
):
number = letter_to_number(piece.position[0])
possible_en_passant = letter_to_number(last_movement["to"][0])
if abs(number - possible_en_passant) == 1:
position = last_movement["to"][0] + str(
int(piece.position[1]) + (1 * direction)
)
conditional_piece_positions2 = (
change_a_piece_position_for_valid_movements(piece, position)
)
if not am_i_in_check(piece.color, conditional_piece_positions2):
valid_movements.append(position)
# knight
if piece.piece.upper() == "N":
x = letter_to_number(piece.position[0])
y = piece.position[1]
possibilities = [
(int(x) + 2, int(y) + 1),
(int(x) + 1, int(y) + 2),
(int(x) - 2, int(y) + 1),
(int(x) - 1, int(y) + 2),
(int(x) - 2, int(y) - 1),
(int(x) - 1, int(y) - 2),
(int(x) + 2, int(y) - 1),
(int(x) + 1, int(y) - 2),
]
for possibility in possibilities:
if possibility[1] < 1 or possibility[1] > 8:
continue
if possibility[0] < 0 or possibility[0] > 7:
continue
position = number_to_letter(possibility[0]) + str(possibility[1])
target_piece = get_piece_on_position(position, conditional_piece_positions)
if (
target_piece
and target_piece.color == piece.color
and not looking_enemy_movements
):
continue
if looking_enemy_movements:
valid_movements.append(position)
continue
conditional_piece_positions2 = change_a_piece_position_for_valid_movements(
piece, position
)
if not am_i_in_check(piece.color, conditional_piece_positions2):
valid_movements.append(position)
# rook or queen
if piece.piece.upper() == "R" or piece.piece.upper() == "Q":
x = letter_to_number(piece.position[0])
y = int(piece.position[1])
target_x = x - 1
already_first_break = False
while target_x >= 0:
target_letter = number_to_letter(target_x)
position = target_letter + str(y)
target_piece = get_piece_on_position(position, conditional_piece_positions)
if target_piece is not None:
if target_piece.color != piece.color:
valid_movements.append(position)
break
conditional_piece_positions2 = change_a_piece_position_for_valid_movements(
piece, position
)
if looking_enemy_movements or not am_i_in_check(
piece.color, conditional_piece_positions2
):
valid_movements.append(position)
target_x -= 1
target_x = x + 1
while target_x <= 7:
target_letter = number_to_letter(target_x)
position = target_letter + str(y)
target_piece = get_piece_on_position(position, conditional_piece_positions)
if target_piece is not None:
if target_piece.color != piece.color:
valid_movements.append(position)
break
conditional_piece_positions2 = change_a_piece_position_for_valid_movements(
piece, position
)
if looking_enemy_movements or not am_i_in_check(
piece.color, conditional_piece_positions2
):
valid_movements.append(position)
target_x += 1
target_y = y - 1
while target_y >= 1:
target_letter = number_to_letter(x)
position = target_letter + str(target_y)
target_piece = get_piece_on_position(position, conditional_piece_positions)
if target_piece is not None:
if target_piece.color != piece.color:
valid_movements.append(position)
break
conditional_piece_positions2 = change_a_piece_position_for_valid_movements(
piece, position
)
if looking_enemy_movements or not am_i_in_check(
piece.color, conditional_piece_positions2
):
valid_movements.append(position)
target_y -= 1
target_y = y + 1
while target_y <= 8:
target_letter = number_to_letter(x)
position = target_letter + str(target_y)
target_piece = get_piece_on_position(position, conditional_piece_positions)
if target_piece is not None:
if target_piece.color != piece.color:
valid_movements.append(position)
break
conditional_piece_positions2 = change_a_piece_position_for_valid_movements(
piece, position
)
if looking_enemy_movements or not am_i_in_check(
piece.color, conditional_piece_positions2
):
valid_movements.append(position)
target_y += 1
# bishop or queen
if piece.piece.upper() == "B" or piece.piece.upper() == "Q":
x = letter_to_number(piece.position[0])
y = int(piece.position[1])
target_x = x - 1
target_y = y - 1
while target_x >= 0 and target_y >= 1:
target_letter = number_to_letter(target_x)
position = target_letter + str(target_y)
target_piece = get_piece_on_position(position, conditional_piece_positions)
if target_piece is not None:
if target_piece.color != piece.color:
valid_movements.append(position)
break
conditional_piece_positions2 = change_a_piece_position_for_valid_movements(
piece, position
)
if looking_enemy_movements or not am_i_in_check(
piece.color, conditional_piece_positions2
):
valid_movements.append(position)
target_x -= 1
target_y -= 1
target_x = x + 1
target_y = y - 1
while target_x <= 7 and target_y >= 1:
target_letter = number_to_letter(target_x)
position = target_letter + str(target_y)
target_piece = get_piece_on_position(position, conditional_piece_positions)
if target_piece is not None:
if target_piece.color != piece.color:
valid_movements.append(position)
break
conditional_piece_positions2 = change_a_piece_position_for_valid_movements(
piece, position
)
if looking_enemy_movements or not am_i_in_check(
piece.color, conditional_piece_positions2
):
valid_movements.append(position)
target_x += 1
target_y -= 1
target_x = x - 1
target_y = y + 1
while target_x >= 0 and target_y <= 8:
target_letter = number_to_letter(target_x)
position = target_letter + str(target_y)
target_piece = get_piece_on_position(position, conditional_piece_positions)
if target_piece is not None:
if target_piece.color != piece.color:
valid_movements.append(position)
break
conditional_piece_positions2 = change_a_piece_position_for_valid_movements(
piece, position
)
if looking_enemy_movements or not am_i_in_check(
piece.color, conditional_piece_positions2
):
valid_movements.append(position)
target_x -= 1
target_y += 1
target_x = x + 1
target_y = y + 1
while target_x <= 7 and target_y <= 8:
target_letter = number_to_letter(target_x)
position = target_letter + str(target_y)
target_piece = get_piece_on_position(position, conditional_piece_positions)
if target_piece is not None:
if target_piece.color != piece.color:
valid_movements.append(position)
break
conditional_piece_positions2 = change_a_piece_position_for_valid_movements(
piece, position
)
if looking_enemy_movements or not am_i_in_check(
piece.color, conditional_piece_positions2
):
valid_movements.append(position)
target_x += 1
target_y += 1
# king
if piece.piece.upper() == "K":
x = letter_to_number(piece.position[0])
y = int(piece.position[1])
targets = [(1, -1), (1, 0), (1, 1), (0, -1), (0, 1), (-1, -1), (-1, 0), (-1, 1)]
for target in targets:
target_x = x + target[0]
target_y = y + target[1]
if target_x > 7 or target_x < 0:
continue
if target_y > 8 or target_y < 1:
continue
target_letter = number_to_letter(target_x)
position = target_letter + str(target_y)
target_piece = get_piece_on_position(position, conditional_piece_positions)
if target_piece is None or target_piece.color != piece.color:
conditional_piece_positions2 = (
change_a_piece_position_for_valid_movements(piece, position)
)
if looking_enemy_movements or not am_i_in_check(
piece.color, conditional_piece_positions2
):
valid_movements.append(position)
# Castling
if not piece.have_been_moved and (
looking_enemy_movements
or not am_i_in_check(piece.color, conditional_piece_positions)
):
my_rooks = get_rooks(piece.color, conditional_piece_positions)
for rook in my_rooks:
if not rook.have_been_moved:
line = rook.position[1]
# Queen Side
if rook.position[0] == "a":
if (
get_piece_on_position("b" + line) is None
and get_piece_on_position("c" + line) is None
and get_piece_on_position("d" + line) is None
):
conditional_piece_positions2 = (
change_a_piece_position_for_valid_movements(
piece, "d" + line
)
)
if looking_enemy_movements or not am_i_in_check(
piece.color, conditional_piece_positions2
):
conditional_piece_positions2 = (
change_a_piece_position_for_valid_movements(
piece, "c" + line
)
)
conditional_piece_positions3 = (
change_a_piece_position_for_valid_movements(
rook, "d" + line, conditional_piece_positions2
)
)
if looking_enemy_movements or not am_i_in_check(
piece.color, conditional_piece_positions3
):
valid_movements.append("c" + line)
# King Side
else:
if (
get_piece_on_position("g" + line) is None
and get_piece_on_position("f" + line) is None
):
conditional_piece_positions2 = (
change_a_piece_position_for_valid_movements(
piece, "f" + line
)
)
if looking_enemy_movements or not am_i_in_check(
piece.color, conditional_piece_positions2
):
conditional_piece_positions2 = (
change_a_piece_position_for_valid_movements(
piece, "g" + line
)
)
conditional_piece_positions3 = (
change_a_piece_position_for_valid_movements(
rook, "f" + line, conditional_piece_positions2
)
)
if looking_enemy_movements or not am_i_in_check(
piece.color, conditional_piece_positions3
):
valid_movements.append("g" + line)
return valid_movements
def get_rooks(color, conditional_piece_positions=None) -> List[PiecePosition]:
if conditional_piece_positions is None:
conditional_piece_positions = piece_positions
rooks = []
for piece_position in conditional_piece_positions:
if piece_position.piece == "R" and piece_position.color == color:
rooks.append(piece_position)
return rooks
def change_a_piece_position_for_valid_movements(
piece: PiecePosition,
to_conditional_position: str,
conditional_piece_position_param=None,
) -> List[PiecePosition]:
conditional_piece_positions = []
if conditional_piece_position_param is None:
conditional_piece_position_param = piece_positions
for piece_position in conditional_piece_position_param:
if (
piece_position.piece == piece.piece
and piece_position.color == piece.color
and piece_position.position == piece.position
):
conditional_piece_positions.append(
PiecePosition(piece.color, piece.piece, to_conditional_position)
)
elif piece_position.position != to_conditional_position:
conditional_piece_positions.append(piece_position)
return conditional_piece_positions
selected_piece = (-1, -1)
valid_movements = []
turn = "w"
promote_dialog = False
while True:
for event in pygame.event.get():
if event.type == pygame.MOUSEBUTTONDOWN:
if promote_dialog:
x = event.pos[0] / 100
y = event.pos[1] / 100
if 4.5 > y > 3.5:
if 3 > x > 2:
for piece_position in piece_positions:
if piece_position.position == movements_history[-1]["to"]:
piece_position.piece = "Q"
promote_dialog = False
if 4 > x > 3:
for piece_position in piece_positions:
if piece_position.position == movements_history[-1]["to"]:
piece_position.piece = "R"
promote_dialog = False
if 5 > x > 4:
for piece_position in piece_positions:
if piece_position.position == movements_history[-1]["to"]:
piece_position.piece = "B"
promote_dialog = False
if 6 > x > 5:
for piece_position in piece_positions:
if piece_position.position == movements_history[-1]["to"]:
piece_position.piece = "N"
promote_dialog = False
else:
letter = number_to_letter(event.pos[0] // 100)
number = 8 - (event.pos[1] // 100)
piece = get_piece_on_position(letter + str(number))
# selected a piece to move
if piece and piece.color == turn:
selected_piece = (
event.pos[0] // 100,
event.pos[1] // 100,
)
valid_movements = get_valid_movements(piece)
continue
# selected a place to move the piece
if selected_piece[0] != -1:
if letter + str(number) not in valid_movements:
continue
piece_letter = number_to_letter(selected_piece[0])
piece_number = 8 - (selected_piece[1])
old_piece = get_piece_on_position(piece_letter + str(piece_number))
from_position = old_piece.position
old_piece.position = letter + str(number)
# Castling
if old_piece.piece == "K" and from_position[0] == "e":
# king side
if old_piece.position[0] == "g":
rook_castling = get_piece_on_position(
"h" + from_position[1]
)
rook_castling.position = "f" + from_position[1]
rook_castling.have_been_moved = True
# queen side
elif old_piece.position[0] == "c":
rook_castling = get_piece_on_position(
"a" + from_position[1]
)
rook_castling.position = "d" + from_position[1]
rook_castling.have_been_moved = True
# default capture
if piece and piece.color != turn:
piece_positions.remove(piece)
# capture by en passant
if (
old_piece.piece == "P"
and from_position[0] != old_piece.position[0]
and piece is None
):
piece_positions.remove(movements_history[-1]["piece"])
selected_piece = (-1, -1)
valid_movements = []
movements_history.append(
{
"piece": old_piece,
"from": from_position,
"to": old_piece.position,
}
)
old_piece.have_been_moved = True
if old_piece.piece == "P" and (
old_piece.position[1] == "1" or old_piece.position[1] == "8"
):
promote_dialog = True
if turn == "w":
turn = "b"
else:
turn = "w"
print_tabletop()
in_check = False
if am_i_in_check(turn, piece_positions):
in_check = True
the_game_continues = False
for piece_position in piece_positions:
if piece_position.color == turn:
if len(get_valid_movements(piece_position)) > 0:
the_game_continues = True
break
if not the_game_continues:
if not in_check:
print("It's a draw")
else:
if turn == "w":
print("Black won the game.")
else:
print("White won the game.")
continue
if event.type == pygame.QUIT:
pygame.quit()
exit()
gameDisplay.fill((128, 128, 128))
# draw tabletop
for x in range(0, 8):
for y in range(0, 8):
if selected_piece[0] == x and selected_piece[1] == y:
pygame.draw.rect(
gameDisplay,
(155, 255, 155),
(x * block_size, y * block_size, block_size, block_size),
0,
)
elif (x + y) % 2 == 0:
pygame.draw.rect(
gameDisplay,
(255, 255, 255),
(x * block_size, y * block_size, block_size, block_size),
0,
)
# draw pieces on tabletop
for piece_position in piece_positions:
img = piece_image[piece_position.color + "-" + piece_position.piece]
gameDisplay.blit(
img,
(
(letter_to_number(piece_position.position[0]) * block_size)
+ center_piece_img,
((8 - int(piece_position.position[1])) * block_size) + center_piece_img,
),
)
for valid_movement in valid_movements:
x = letter_to_number(valid_movement[0])
y = 8 - int(valid_movement[1])
position = (
int((x * block_size) + (block_size / 2)),
int((y * block_size) + (block_size / 2)),
)
pygame.draw.circle(
gameDisplay,
(100, 150, 100),
position,
14,
)
pygame.draw.circle(
gameDisplay,
(0, 0, 0),
position,
15,
1,
)
if promote_dialog:
pygame.draw.rect(
gameDisplay,
(255, 255, 255),
(block_size * 2, block_size * 3, block_size * 4, block_size * 2),
0,
)
pygame.draw.rect(
gameDisplay,
(0, 0, 0),
(block_size * 2, block_size * 3, block_size * 4, block_size * 2),
1,
)
promote_color = "b"
if turn == "b":
promote_color = "w"
gameDisplay.blit(
piece_image[promote_color + "-Q"],
(
(2 * block_size) + center_piece_img,
(3.5 * block_size) + center_piece_img,
),
)
gameDisplay.blit(
piece_image[promote_color + "-R"],
(
(3 * block_size) + center_piece_img,
(3.5 * block_size) + center_piece_img,
),
)
gameDisplay.blit(
piece_image[promote_color + "-B"],
(
(4 * block_size) + center_piece_img,
(3.5 * block_size) + center_piece_img,
),
)