-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
1735 lines (1589 loc) · 79.1 KB
/
main.py
File metadata and controls
1735 lines (1589 loc) · 79.1 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
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
from tkinter import *
from PIL import ImageTk, Image
from ChessImages import images
import socket
import pickle
from client import Network
import threading
import sys
#INTERFACE
class Pieces(Frame):
#PIECES PLACEMENT
def reset(self):
#BASE POSITIONS OF PIECES
self.base_pieces_positions = {"WPawn1": [0, 6], "WPawn2" : [1, 6], "WPawn3" : [2, 6], "WPawn4" : [3, 6], "WPawn5" : [4, 6], "WPawn6" : [5, 6], "WPawn7" : [6, 6], "WPawn8" : [7, 6], "WRook1" : [0, 7], "WRook2" : [7, 7], "WKnight1" : [1, 7], "WKnight2" : [6, 7], "WBishop1" : [2, 7], "WBishop2" : [5, 7], "WQueen1" : [3, 7], "WKing1" : [4, 7], "BPawn1": [0, 1], "BPawn2" : [1, 1], "BPawn3" : [2, 1], "BPawn4" : [3, 1], "BPawn5" : [4, 1], "BPawn6" : [5, 1], "BPawn7" : [6, 1], "BPawn8" : [7, 1], "BRook1" : [0, 0], "BRook2" : [7, 0], "BKnight1" : [1, 0], "BKnight2" : [6, 0], "BBishop1" : [2, 0], "BBishop2" : [5, 0], "BQueen1" : [3, 0], "BKing1" : [4, 0]}
#RETURNING THE PIECES POSITIONS TO THE ORIGINAL POSITIONS
self.pieces_positions = self.base_pieces_positions.copy()
#RETURNING THE TURN TO WHITE
self.turn = "W"
#REMOVING ANY MARKERS OR HIGHLIGHTS
self.del_markers()
#DELETING ANY PROMOTED PAWNS
for item in self.canvas.find_withtag("fake"):
self.canvas.delete(item)
#MOVING CHESSPIECES BACK TO ORIGINAL POSITIONS
for item in self.canvas.find_withtag("ChessPiece"):
self.canvas.move(item, self.base_pieces_positions[self.canvas.gettags(item)[1]][0] * 80 + 40 - self.canvas.coords(item)[0], self.base_pieces_positions[self.canvas.gettags(item)[1]][1] * 80 + 40 - self.canvas.coords(item)[1])
#REMOVING THE CHECKMATE BANNER
self.del_checkmate()
#RESETTING THE COLOR CHANGES TO THE CHESSBOARD CAUSED BY CHECK
self.reset_chessboard_colors()
#RESETTING THE RECORD
self.moves = [[["WPawn1", 0, 0]]]
self.history = [[["WPawn1", 0, 0]]]
self.stalemate = 0
#SENDING RESET MESSAGE TO SERVER SO THAT THE OTHER PLAYER RESETS THEIR BOARD AS WELL
self.history.append([["RESET", self.player]])
self.moved = True
def server_reset(self):
#BASE POSITIONS OF PIECES
self.base_pieces_positions = {"WPawn1": [0, 6], "WPawn2" : [1, 6], "WPawn3" : [2, 6], "WPawn4" : [3, 6], "WPawn5" : [4, 6], "WPawn6" : [5, 6], "WPawn7" : [6, 6], "WPawn8" : [7, 6], "WRook1" : [0, 7], "WRook2" : [7, 7], "WKnight1" : [1, 7], "WKnight2" : [6, 7], "WBishop1" : [2, 7], "WBishop2" : [5, 7], "WQueen1" : [3, 7], "WKing1" : [4, 7], "BPawn1": [0, 1], "BPawn2" : [1, 1], "BPawn3" : [2, 1], "BPawn4" : [3, 1], "BPawn5" : [4, 1], "BPawn6" : [5, 1], "BPawn7" : [6, 1], "BPawn8" : [7, 1], "BRook1" : [0, 0], "BRook2" : [7, 0], "BKnight1" : [1, 0], "BKnight2" : [6, 0], "BBishop1" : [2, 0], "BBishop2" : [5, 0], "BQueen1" : [3, 0], "BKing1" : [4, 0]}
#RETURNING HTE PIECES POSITIONS TO THE ORIGINAL POSITIONS
self.pieces_positions = self.base_pieces_positions.copy()
#RETURNING THE TURN TO WHITE
self.turn = "W"
self.stalemate = 0
#DELETING ANY MARKERS OR HIGHLIGHTS
self.del_markers()
for item in self.canvas.find_withtag("fake"):
self.canvas.delete(item)
#MOVING CHESSPIECES BACK TO ORIGINAL POSITIONS
for item in self.canvas.find_withtag("ChessPiece"):
self.canvas.move(item, self.base_pieces_positions[self.canvas.gettags(item)[1]][0] * 80 + 40 - self.canvas.coords(item)[0], self.base_pieces_positions[self.canvas.gettags(item)[1]][1] * 80 + 40 - self.canvas.coords(item)[1])
#REMOVING THE CHECKMATE/STALEMATE BANNER
self.del_checkmate()
#RESETTING THE COLOR CHANGES TO THE CHESSBOARD CAUSED BY CHECK
self.reset_chessboard_colors()
#RESETTING THE RECORD
self.moves = [[["WPawn1", 0, 0]]]
self.history = [[["WPawn1", 0, 0]]]
def place(self):
#ESSENTIALLY CREATING THE IMAGES ON THE BOARD
self.BPawn1 = self.canvas.create_image(self.pieces_positions["BPawn1"][0] * 80 + 40, self.pieces_positions["BPawn1"][1] * 80 + 40, image = self.BPawn, anchor = CENTER, tag = ("ChessPiece", "BPawn1", "B", "BPawn"))
self.BPawn2 = self.canvas.create_image(self.pieces_positions["BPawn2"][0] * 80 + 40, self.pieces_positions["BPawn2"][1] * 80 + 40, image = self.BPawn, anchor = CENTER, tag = ("ChessPiece", "BPawn2", "B", "BPawn"))
self.BPawn3 = self.canvas.create_image(self.pieces_positions["BPawn3"][0] * 80 + 40, self.pieces_positions["BPawn3"][1] * 80 + 40, image = self.BPawn, anchor = CENTER, tag = ("ChessPiece", "BPawn3", "B", "BPawn"))
self.BPawn4 = self.canvas.create_image(self.pieces_positions["BPawn4"][0] * 80 + 40, self.pieces_positions["BPawn4"][1] * 80 + 40, image = self.BPawn, anchor = CENTER, tag = ("ChessPiece", "BPawn4", "B", "BPawn"))
self.BPawn5 = self.canvas.create_image(self.pieces_positions["BPawn5"][0] * 80 + 40, self.pieces_positions["BPawn5"][1] * 80 + 40, image = self.BPawn, anchor = CENTER, tag = ("ChessPiece", "BPawn5", "B", "BPawn"))
self.BPawn6 = self.canvas.create_image(self.pieces_positions["BPawn6"][0] * 80 + 40, self.pieces_positions["BPawn6"][1] * 80 + 40, image = self.BPawn, anchor = CENTER, tag = ("ChessPiece", "BPawn6", "B", "BPawn"))
self.BPawn7 = self.canvas.create_image(self.pieces_positions["BPawn7"][0] * 80 + 40, self.pieces_positions["BPawn7"][1] * 80 + 40, image = self.BPawn, anchor = CENTER, tag = ("ChessPiece", "BPawn7", "B", "BPawn"))
self.BPawn8 = self.canvas.create_image(self.pieces_positions["BPawn8"][0] * 80 + 40, self.pieces_positions["BPawn8"][1] * 80 + 40, image = self.BPawn, anchor = CENTER, tag = ("ChessPiece", "BPawn8", "B", "BPawn"))
self.BKnight1 = self.canvas.create_image(self.pieces_positions["BKnight1"][0] * 80 + 40, self.pieces_positions["BKnight1"][1] * 80 + 40, image = self.BKnight, anchor = CENTER, tag = ("ChessPiece", "BKnight1", "B", "BKnight"))
self.BKnight2 = self.canvas.create_image(self.pieces_positions["BKnight2"][0] * 80 + 40, self.pieces_positions["BKnight2"][1] * 80 + 40, image = self.BKnight, anchor = CENTER, tag = ("ChessPiece", "BKnight2", "B", "BKnight"))
self.BRook1 = self.canvas.create_image(self.pieces_positions["BRook1"][0] * 80 + 40, self.pieces_positions["BRook1"][1] * 80 + 40, image = self.BRook, anchor = CENTER, tag = ("ChessPiece", "BRook1", "B", "BRook"))
self.BRook2 = self.canvas.create_image(self.pieces_positions["BRook2"][0] * 80 + 40, self.pieces_positions["BRook2"][1] * 80 + 40, image = self.BRook, anchor = CENTER, tag = ("ChessPiece", "BRook2", "B", "BRook"))
self.BBishop1 = self.canvas.create_image(self.pieces_positions["BBishop1"][0] * 80 + 40, self.pieces_positions["BBishop1"][1] * 80 + 40, image = self.BBishop, anchor = CENTER, tag = ("ChessPiece", "BBishop1", "B", "BBishop"))
self.BBishop2 = self.canvas.create_image(self.pieces_positions["BBishop2"][0] * 80 + 40, self.pieces_positions["BBishop2"][1] * 80 + 40, image = self.BBishop, anchor = CENTER, tag = ("ChessPiece", "BBishop2", "B", "BBishop"))
self.BQueen1 = self.canvas.create_image(self.pieces_positions["BQueen1"][0] * 80 + 40, self.pieces_positions["BQueen1"][1] * 80 + 40, image = self.BQueen, anchor = CENTER, tag = ("ChessPiece", "BQueen1", "B", "BQueen"))
self.BKing1 = self.canvas.create_image(self.pieces_positions["BKing1"][0] * 80 + 40, self.pieces_positions["BKing1"][1] * 80 + 40, image = self.BKing, anchor = CENTER, tag = ("ChessPiece", "BKing1", "B", "BKing"))
self.WPawn1 = self.canvas.create_image(self.pieces_positions["WPawn1"][0] * 80 + 40, self.pieces_positions["WPawn1"][1] * 80 + 40, image = self.WPawn, anchor = CENTER, tag = ("ChessPiece", "WPawn1", "W", "WPawn"))
self.WPawn2 = self.canvas.create_image(self.pieces_positions["WPawn2"][0] * 80 + 40, self.pieces_positions["WPawn2"][1] * 80 + 40, image = self.WPawn, anchor = CENTER, tag = ("ChessPiece", "WPawn2", "W", "WPawn"))
self.WPawn3 = self.canvas.create_image(self.pieces_positions["WPawn3"][0] * 80 + 40, self.pieces_positions["WPawn3"][1] * 80 + 40, image = self.WPawn, anchor = CENTER, tag = ("ChessPiece", "WPawn3", "W", "WPawn"))
self.WPawn4 = self.canvas.create_image(self.pieces_positions["WPawn4"][0] * 80 + 40, self.pieces_positions["WPawn4"][1] * 80 + 40, image = self.WPawn, anchor = CENTER, tag = ("ChessPiece", "WPawn4", "W", "WPawn"))
self.WPawn5 = self.canvas.create_image(self.pieces_positions["WPawn5"][0] * 80 + 40, self.pieces_positions["WPawn5"][1] * 80 + 40, image = self.WPawn, anchor = CENTER, tag = ("ChessPiece", "WPawn5", "W", "WPawn"))
self.WPawn6 = self.canvas.create_image(self.pieces_positions["WPawn6"][0] * 80 + 40, self.pieces_positions["WPawn6"][1] * 80 + 40, image = self.WPawn, anchor = CENTER, tag = ("ChessPiece", "WPawn6", "W", "WPawn"))
self.WPawn7 = self.canvas.create_image(self.pieces_positions["WPawn7"][0] * 80 + 40, self.pieces_positions["WPawn7"][1] * 80 + 40, image = self.WPawn, anchor = CENTER, tag = ("ChessPiece", "WPawn7", "W", "WPawn"))
self.WPawn8 = self.canvas.create_image(self.pieces_positions["WPawn8"][0] * 80 + 40, self.pieces_positions["WPawn8"][1] * 80 + 40, image = self.WPawn, anchor = CENTER, tag = ("ChessPiece", "WPawn8", "W", "WPawn"))
self.WKnight1 = self.canvas.create_image(self.pieces_positions["WKnight1"][0] * 80 + 40, self.pieces_positions["WKnight1"][1] * 80 + 40, image = self.WKnight, anchor = CENTER, tag = ("ChessPiece", "WKnight1", "W", "WKnight"))
self.WKnight2 = self.canvas.create_image(self.pieces_positions["WKnight2"][0] * 80 + 40, self.pieces_positions["WKnight2"][1] * 80 + 40, image = self.WKnight, anchor = CENTER, tag = ("ChessPiece", "WKnight2", "W", "WKnight"))
self.WRook1 = self.canvas.create_image(self.pieces_positions["WRook1"][0] * 80 + 40, self.pieces_positions["WRook1"][1] * 80 + 40, image = self.WRook, anchor = CENTER, tag = ("ChessPiece", "WRook1", "W", "WRook"))
self.WRook2 = self.canvas.create_image(self.pieces_positions["WRook2"][0] * 80 + 40, self.pieces_positions["WRook2"][1] * 80 + 40, image = self.WRook, anchor = CENTER, tag = ("ChessPiece", "WRook2", "W", "WRook"))
self.WBishop1 = self.canvas.create_image(self.pieces_positions["WBishop1"][0] * 80 + 40, self.pieces_positions["WBishop1"][1] * 80 + 40, image = self.WBishop, anchor = CENTER, tag = ("ChessPiece", "WBishop1", "W", "WBishop"))
self.WBishop2 = self.canvas.create_image(self.pieces_positions["WBishop2"][0] * 80 + 40, self.pieces_positions["WBishop2"][1] * 80 + 40, image = self.WBishop, anchor = CENTER, tag = ("ChessPiece", "WBishop2", "W", "WBishop"))
self.WQueen1 = self.canvas.create_image(self.pieces_positions["WQueen1"][0] * 80 + 40, self.pieces_positions["WQueen1"][1] * 80 + 40, image = self.WQueen, anchor = CENTER, tag = ("ChessPiece", "WQueen1", "W", "WQueen"))
self.WKing1 = self.canvas.create_image(self.pieces_positions["WKing1"][0] * 80 + 40, self.pieces_positions["WKing1"][1] * 80 + 40, image = self.WKing, anchor = CENTER, tag = ("ChessPiece", "WKing1", "W", "WKing"))
#LIFTING THE IMAGES OF THE CHESSPIECES SO THAT THEY DONT END UP BELOW THE BOARD OR MARKERS
for piece in self.canvas.find_withtag("ChessPiece"):
self.canvas.lift(piece)
def reset_chessboard_colors(self):
#RESET CHESSBOARD COLORS
for x in range(8):
for y in range(8):
if (x+y) % 2 == 1: #ODD VALUES ARE BLACK
self.canvas.itemconfig(self.canvas.find_withtag("" + str(x) + ", " + str(y))[0], fill = "#b58863")
if (x+y) % 2 == 0: #EVEN VALUES ARE WHITE
self.canvas.itemconfig(self.canvas.find_withtag("" + str(x) + ", " + str(y))[0], fill = "#f0d9b5")
def undo(self):
if len(self.moves) > 1:
#CHECK EACH MOVEMENT IN THE LAST ENTRY TO THE RECORD OF MOVES (AKA SELF.MOVES)
for piece in self.moves[-1]:
#IF THE PIECE IS A PROMOTED PAWN AND THUS HAS NO MOVEMENT VALUES, DELETE IT
if "fake" in self.canvas.gettags(piece[0]):
self.canvas.delete(piece[0])
del self.pieces_positions[piece[0]]
#MOVE THE PIECES TO THEIR ORIGINAL POSITIONS
else:
self.canvas.move(piece[0], -piece[1] * 80, -piece[2] * 80)
self.pieces_positions[piece[0]] = [int(self.canvas.coords(self.canvas.find_withtag(piece[0])[0])[0]/80), int(self.canvas.coords(self.canvas.find_withtag(piece[0])[0])[1]/80)]
#DELETE ANY MARKERS
self.del_markers()
#TELL THE SERVER ABOUT THE UNDO SO THAT THE OPPONENT'S BOARD IS CHANGED
self.history.append([["UNDO", self.player]])
self.moved = True
#REMOVE THAT MOVEMENT FROM THE RECORD
self.moves.remove(self.moves[-1])
self.stalemate -= 1
#CHANGING THE TURN
if self.turn == "W":
self.turn = "B"
elif self.turn == "B":
self.turn = "W"
#REMOVING ANY CHECKMATE BANNER
self.del_checkmate()
#CHECKING FOR A CHECK, RESETTING THE CHESSBOARD COLORS, THEN HIGHLIGHTING THE KING IF CHECKED
self.check_attack_positions(thepositions = self.pieces_positions)
self.reset_chessboard_colors()
self.actually_check_for_check()
def del_markers(self): #DELETING MARKERS
for item in self.canvas.find_withtag("marker"):
self.canvas.delete(item)
def del_checkmate(self): #DELETING CHECKMATE BANNER
if len(self.canvas.find_withtag("Checkmate")) > 0:
self.canvas.delete(self.canvas.find_withtag("Checkmate")[0])
if len(self.canvas.find_withtag("Stalemate")) > 0:
self.canvas.delete(self.canvas.find_withtag("Stalemate")[0])
def server_undo(self):#CHECK EACH MOVEMENT IN THE LAST ENTRY TO THE RECORD OF MOVES (AKA SELF.MOVES), USED BY THE OTHER END OF THE UNDO, SO THAT IT DOESNT SEND BACK ANOTHER UNDO MESSAGE
if len(self.moves) > 1:
for piece in self.moves[-1]:
#IF THE PIECE IS A PROMOTED PAWN AND THUS HAS NO MOVEMENT VALUES, DELETE IT
if "fake" in self.canvas.gettags(piece[0]):
self.canvas.delete(piece[0])
del self.pieces_positions[piece[0]]
#MOVE THE PIECES TO THEIR ORIGINAL POSITIONS
else:
self.canvas.move(piece[0], -piece[1] * 80, -piece[2] * 80)
self.pieces_positions[piece[0]] = [int(self.canvas.coords(self.canvas.find_withtag(piece[0])[0])[0]/80), int(self.canvas.coords(self.canvas.find_withtag(piece[0])[0])[1]/80)]
#DELETE ANY MARKERS
self.del_markers()
#RECORD UNDO CHANGE
self.history.append([["UNDO", self.player]])
#REMOVE THAT MOVEMENT FROM THE RECORD
self.moves.remove(self.moves[-1])
#CHANGING THE TURN
if self.turn == "W":
self.turn = "B"
elif self.turn == "B":
self.turn = "W"
#REMOVING ANY CHECKMATE BANNER
self.del_checkmate()
self.stalemate -= 1
#CHECKING FOR A CHECK, RESETTING THE CHESSBOARD COLORS, THEN HIGHLIGHTING THE KING IF CHECKED
self.check_attack_positions(thepositions = self.pieces_positions)
self.reset_chessboard_colors()
self.actually_check_for_check()
def movement_marker(self, pieces): #HIGHLIGHT MOVEMENT
#DELETE PREVIOUS MOVEMENT
for item in self.canvas.find_withtag("marker"):
self.canvas.delete(item)
#CREATE HIGHLIGHTS
for piece in pieces:
if piece[1] < 8 and piece[1] > -8 and piece[2] < 8 and piece[2] > -8:
x, y = self.pieces_positions[piece[0]]
w, z = self.moves[-1][0][1], self.moves[-1][0][2]
if x >= 0 and y < 8 and x < 8 and y >= 0:
self.canvas.itemconfig(self.canvas.find_withtag("" + str(x) + ", " + str(y))[0], fill = "#e8e18e")
if len(pieces) == 2:
if pieces[0][0][0] == pieces[1][0][0]:
pass
elif x - w >= 0 and y - z < 8 and x - w < 8 and y - z >= 0:
self.canvas.itemconfig(self.canvas.find_withtag("" + str(x-w) + ", " + str(y-z))[0], fill = "#b8af4e")
# MOUSE MOVEMENT AND MOVEMENT OF PIECES
def mouse_down(self, event):
self.moving = False
if self.player != self.turn and self.player != "S":
self.can_Click2 = False
elif self.player == self.turn or self.player == "S":
self.can_Click2 = True
if self.canvas.gettags(CURRENT)[1][0] == self.turn and self.can_Click == True and self.can_Click2 == True:
self.lastx = event.x
self.lasty = event.y
self.basex = self.canvas.coords(CURRENT)[0]
self.basey = self.canvas.coords(CURRENT)[1]
obstacle = []
for x in self.pieces_positions.values():
r, y = x
obstacle.append([r, y])
black_obstacles = []
white_obstacles = []
for key in self.pieces_positions:
if key[0] == "B":
black_obstacles.append(self.pieces_positions[key])
if key[0] == "W":
white_obstacles.append(self.pieces_positions[key])
if self.canvas.gettags(CURRENT)[1][1:-1] == "Queen":
x = self.queensAttack(n = 7, obstacles = obstacle, bobstacles = black_obstacles, wobstacles = white_obstacles, current = CURRENT)
for position in x:
if self.check_for_check(xposition = position, current = CURRENT) == False:
self.canvas.create_oval(position[0] * 80 + 40 - 10, position[1] * 80 + 40 - 10, position[0] * 80 + 40 + 10, position[1] * 80 + 40 + 10, fill = "#138209", tag = "marker")
if self.canvas.gettags(CURRENT)[1][1:-1] == "Bishop":
x = self.bishopsAttack(n = 7, obstacles = obstacle, bobstacles = black_obstacles, wobstacles = white_obstacles, current = CURRENT)
for position in x:
if self.check_for_check(xposition = position, current = CURRENT) == False:
self.canvas.create_oval(position[0] * 80 + 40 - 10, position[1] * 80 + 40 - 10, position[0] * 80 + 40 + 10, position[1] * 80 + 40 + 10, fill = "#138209", tag = "marker")
if self.canvas.gettags(CURRENT)[1][1:-1] == "Rook":
x = self.rooksAttack(n = 7, obstacles = obstacle, bobstacles = black_obstacles, wobstacles = white_obstacles, current = CURRENT)
for position in x:
if self.check_for_check(xposition = position, current = CURRENT) == False:
self.canvas.create_oval(position[0] * 80 + 40 - 10, position[1] * 80 + 40 - 10, position[0] * 80 + 40 + 10, position[1] * 80 + 40 + 10, fill = "#138209", tag = "marker")
if self.canvas.gettags(CURRENT)[1][1:-1] == "Knight":
x = self.knightsAttack(n = 7, obstacles = obstacle, bobstacles = black_obstacles, wobstacles = white_obstacles, current = CURRENT)
for position in x:
if self.check_for_check(xposition = position, current = CURRENT) == False:
self.canvas.create_oval(position[0] * 80 + 40 - 10, position[1] * 80 + 40 - 10, position[0] * 80 + 40 + 10, position[1] * 80 + 40 + 10, fill = "#138209", tag = "marker")
if self.canvas.gettags(CURRENT)[1][1:-1] == "King":
x = self.kingsAttack(n = 7, obstacles = obstacle, bobstacles = black_obstacles, wobstacles = white_obstacles, current = CURRENT)
for position in x:
if self.check_for_check(xposition = position, current = CURRENT) == False:
self.canvas.create_oval(position[0] * 80 + 40 - 10, position[1] * 80 + 40 - 10, position[0] * 80 + 40 + 10, position[1] * 80 + 40 + 10, fill = "#138209", tag = "marker")
if self.canvas.gettags(CURRENT)[1][1:-1] == "Pawn":
x = self.pawnsAttack(n = 7, obstacles = obstacle, bobstacles = black_obstacles, wobstacles = white_obstacles, current = CURRENT)
for position in x:
if self.check_for_check(xposition = position, current = CURRENT) == False:
self.canvas.create_oval(position[0] * 80 + 40 - 10, position[1] * 80 + 40 - 10, position[0] * 80 + 40 + 10, position[1] * 80 + 40 + 10, fill = "#138209", tag = "marker")
def mouse_Move(self, event):
if self.player != self.turn and self.player != "S":
self.can_Click2 = False
elif self.player == self.turn or self.player == "S":
self.can_Click2 = True
if "immovable" in self.canvas.gettags(CURRENT):
pass
elif self.can_Click == True and self.can_Click2 == True:
if self.canvas.gettags(CURRENT)[1][0] == self.turn:
event.widget.move(CURRENT, event.x - self.lastx, event.y - self.lasty)
self.lastx = event.x
self.lasty = event.y
self.moving = True
self.canvas.lift(CURRENT)
def mouse_Up(self, event):
if self.player != self.turn and self.player != "S":
self.can_Click2 = False
elif self.player == self.turn or self.player == "S":
self.can_Click2 = True
if "immovable" in self.canvas.gettags(CURRENT):
pass
elif self.canvas.gettags(CURRENT)[1][0] == self.turn and self.can_Click == True and self.can_Click2 == True and self.moving == True:
YUM = True
self.lastx = int(event.x/80)
self.lasty = int(event.y/80)
self.basex = int(self.basex/80)
self.basey = int(self.basey/80)
items = []
for item in self.canvas.find_withtag("marker"):
items.append([int(self.canvas.coords(item)[0]/80), int(self.canvas.coords(item)[1]/80)])
# KILLING OTHER PIECES
if [self.lastx, self.lasty] in items:
self.movement = []
Castled = False
for piece in self.canvas.find_withtag("ChessPiece"):
if self.canvas.gettags(piece)[1] != self.canvas.gettags(CURRENT)[1]:
if self.canvas.gettags(piece)[1][1:-1] == "Rook" and self.canvas.gettags(CURRENT)[1][1:-1] == "King" and [int(self.canvas.coords(piece)[0]/80), int(self.canvas.coords(piece)[1]/80)] == [int(self.lastx), int(self.lasty)]:
if self.canvas.gettags(piece)[1][-1] == "1":
self.movement.append([self.canvas.gettags(piece)[1], 3, 0])
self.movement.append([self.canvas.gettags(CURRENT)[1], 2, 0])
self.canvas.move(self.canvas.find_withtag(self.canvas.gettags(CURRENT)[1])[0], 160 + 40 - self.canvas.coords(CURRENT)[0], self.lasty * 80 + 40 - self.canvas.coords(CURRENT)[1])
self.canvas.move(piece, 3 * 80, 0)
self.pieces_positions[self.canvas.gettags(piece)[1]] = [int(self.canvas.coords(piece)[0]/80), int(self.canvas.coords(piece)[1]/80)]
self.pieces_positions[self.canvas.gettags(CURRENT)[1]] = [int(self.canvas.coords(CURRENT)[0]/80), int(self.canvas.coords(CURRENT)[1]/80)]
Castled = True
if self.canvas.gettags(piece)[1][-1] == "2":
self.movement.append([self.canvas.gettags(piece)[1], -2, 0])
self.movement.append([self.canvas.gettags(CURRENT)[1], -1, 0])
self.canvas.move(self.canvas.find_withtag(self.canvas.gettags(CURRENT)[1])[0], 480 + 40 - self.canvas.coords(CURRENT)[0], self.lasty * 80 + 40 - self.canvas.coords(CURRENT)[1])
self.canvas.move(piece, -2 * 80, 0)
self.pieces_positions[self.canvas.gettags(piece)[1]] = [int(self.canvas.coords(piece)[0]/80), int(self.canvas.coords(piece)[1]/80)]
self.pieces_positions[self.canvas.gettags(CURRENT)[1]] = [int(self.canvas.coords(CURRENT)[0]/80), int(self.canvas.coords(CURRENT)[1]/80)]
Castled = True
#CHANGING POSITION IN DATABASE
if Castled == False:
self.pieces_positions[self.canvas.gettags(CURRENT)[1]] = [self.lastx, self.lasty]
#MOVING THE PIECE
if Castled == False:
self.movement.append([self.canvas.gettags(CURRENT)[1], self.lastx - self.basex, self.lasty - self.basey])
for piece in self.canvas.find_withtag("ChessPiece"):
if self.canvas.gettags(piece)[1] != self.canvas.gettags(CURRENT)[1]:
if [int(self.canvas.coords(piece)[0]/80), int(self.canvas.coords(piece)[1]/80)] == [int(self.lastx), int(self.lasty)]:
self.stalemate = 0
self.movement.append([self.canvas.gettags(piece)[1], 10000/80, 10000/80])
self.canvas.move(piece, 10000, 10000)
self.pieces_positions[self.canvas.gettags(piece)[1]] = [int(self.canvas.coords(piece)[0]/80), int(self.canvas.coords(piece)[1]/80)]
break
#IF PAWN AT OTHER END, REPLACE And En possant
if self.canvas.gettags(CURRENT)[1][1: -1] == "Pawn":
self.stalemate = 0
if self.canvas.gettags(CURRENT)[1][0] == "W":
if len(self.moves) <= 2:
pass
elif self.moves[-1][0][0][1:-1] == "Pawn" and abs(self.moves[-1][0][2]) == 2:
c, s = self.pieces_positions[self.canvas.gettags(CURRENT)[1]]
r, y = self.pieces_positions[self.moves[-1][0][0]]
if c == r and s == y - 1:
self.movement.append([self.canvas.gettags(self.moves[-1][0][0])[1], 10000/80, 10000/80])
self.canvas.move(self.canvas.find_withtag(self.moves[-1][0][0])[0], 10000, 10000)
if self.pieces_positions[self.canvas.gettags(CURRENT)[1]][1] == 0:
self.pop_up(current = self.canvas.gettags(CURRENT)[1])
self.YUM = False
if self.canvas.gettags(CURRENT)[1][0] == "B":
if len(self.moves) <= 2:
pass
elif self.moves[-1][0][0][1:-1] == "Pawn" and abs(self.moves[-1][0][2]) == 2:
c, s = self.pieces_positions[self.canvas.gettags(CURRENT)[1]]
r, y = self.pieces_positions[self.moves[-1][0][0]]
if c == r and s == y + 1:
self.movement.append([self.canvas.gettags(self.moves[-1][0][0])[1], 10000/80, 10000/80])
self.canvas.move(self.canvas.find_withtag(self.moves[-2][0]), 10000, 10000)
if self.pieces_positions[self.canvas.gettags(CURRENT)[1]][1] == 7:
self.pop_up(current = self.canvas.gettags(CURRENT)[1])
self.YUM = False
self.moves.append(self.movement)
self.history.append(self.movement)
if Castled == False:
self.canvas.move(self.canvas.gettags(CURRENT)[1], self.lastx * 80 + 40 - self.canvas.coords(CURRENT)[0], self.lasty * 80 + 40 - self.canvas.coords(CURRENT)[1])
self.moving = False
if self.YUM == True:
self.moved = True
# CHANGING TURNS
self.movement_marker(self.movement)
if self.turn == "W":
self.turn = "B"
elif self.turn == "B":
self.turn = "W"
#CHECKING
self.check_attack_positions(thepositions = self.pieces_positions)
self.reset_chessboard_colors()
self.movement_marker(self.movement)
self.actually_check_for_check()
self.check_for_checkmate()
self.check_stalemate()
else:
self.canvas.move(self.canvas.gettags(CURRENT)[1], self.basex * 80 + 40 - self.canvas.coords(CURRENT)[0], self.basey * 80 + 40 - self.canvas.coords(CURRENT)[1])
for item in self.canvas.find_withtag("marker"):
self.canvas.delete(item)
# CHECKING IF THE KING IS CHECKED
def actually_check_for_check(self):
if self.wking_check == True:
self.canvas.itemconfig(self.canvas.find_withtag("" + str(self.pieces_positions["WKing1"][0]) + ", " + str(self.pieces_positions["WKing1"][1]))[0], fill = "brown")
return True
if self.bking_check == True:
self.canvas.itemconfig(self.canvas.find_withtag("" + str(self.pieces_positions["BKing1"][0]) + ", " + str(self.pieces_positions["BKing1"][1]))[0], fill = "brown")
return True
def check_stalemate(self):
if self.actually_check_for_check() == True:
self.stalemate = 0
else:
self.stalemate += 1
if self.stalemate == 50:
self.canvas.lift(self.canvas.create_image(320, 320, image = self.stalemate_image, anchor = CENTER, tag = "Stalemate"))
def check_for_checkmate(self):
total_positions = 0
obstacle = []
for x in self.pieces_positions.values():
r, y = x
obstacle.append([r, y])
black_obstacles = []
white_obstacles = []
for key in self.pieces_positions:
if key[0] == "B":
black_obstacles.append(self.pieces_positions[key])
if key[0] == "W":
white_obstacles.append(self.pieces_positions[key])
if self.turn == "W":
for piece in self.canvas.find_withtag("W"):
if self.pieces_positions[self.canvas.gettags(piece)[1]][0] < 8 and self.pieces_positions[self.canvas.gettags(piece)[1]][0] >= 0 and self.pieces_positions[self.canvas.gettags(piece)[1]][1] < 8 and self.pieces_positions[self.canvas.gettags(piece)[1]][1] >= 0:
if self.canvas.gettags(piece)[1][1:-1] == "Queen":
x = self.queensAttack(n = 7, obstacles = obstacle, bobstacles = black_obstacles, wobstacles = white_obstacles, current = piece)
for position in x:
if self.check_for_check(xposition = position, current = piece) == False:
total_positions += 1
if self.canvas.gettags(piece)[1][1:-1] == "Bishop":
x = self.bishopsAttack(n = 7, obstacles = obstacle, bobstacles = black_obstacles, wobstacles = white_obstacles, current = piece)
for position in x:
if self.check_for_check(xposition = position, current = piece) == False:
total_positions += 1
if self.canvas.gettags(piece)[1][1:-1] == "Rook":
x = self.rooksAttack(n = 7, obstacles = obstacle, bobstacles = black_obstacles, wobstacles = white_obstacles, current = piece)
for position in x:
if self.check_for_check(xposition = position, current = piece) == False:
total_positions += 1
if self.canvas.gettags(piece)[1][1:-1] == "Knight":
x = self.knightsAttack(n = 7, obstacles = obstacle, bobstacles = black_obstacles, wobstacles = white_obstacles, current = piece)
for position in x:
if self.check_for_check(xposition = position, current = piece) == False:
total_positions += 1
if self.canvas.gettags(piece)[1][1:-1] == "King":
x = self.kingsAttack(n = 7, obstacles = obstacle, bobstacles = black_obstacles, wobstacles = white_obstacles, current = piece)
for position in x:
if self.check_for_check(xposition = position, current = piece) == False:
total_positions += 1
if self.canvas.gettags(piece)[1][1:-1] == "Pawn":
x = self.pawnsAttack(n = 7, obstacles = obstacle, bobstacles = black_obstacles, wobstacles = white_obstacles, current = piece)
for position in x:
if self.check_for_check(xposition = position, current = piece) == False:
total_positions += 1
if self.turn == "B":
for piece in self.canvas.find_withtag("B"):
if self.pieces_positions[self.canvas.gettags(piece)[1]][0] < 8 and self.pieces_positions[self.canvas.gettags(piece)[1]][0] >= 0 and self.pieces_positions[self.canvas.gettags(piece)[1]][1] < 8 and self.pieces_positions[self.canvas.gettags(piece)[1]][1] >= 0:
if self.canvas.gettags(piece)[1][1:-1] == "Queen":
x = self.queensAttack(n = 7, obstacles = obstacle, bobstacles = black_obstacles, wobstacles = white_obstacles, current = piece)
for position in x:
if self.check_for_check(xposition = position, current = piece) == False:
total_positions += 1
if self.canvas.gettags(piece)[1][1:-1] == "Bishop":
x = self.bishopsAttack(n = 7, obstacles = obstacle, bobstacles = black_obstacles, wobstacles = white_obstacles, current = piece)
for position in x:
if self.check_for_check(xposition = position, current = piece) == False:
total_positions += 1
if self.canvas.gettags(piece)[1][1:-1] == "Rook":
x = self.rooksAttack(n = 7, obstacles = obstacle, bobstacles = black_obstacles, wobstacles = white_obstacles, current = piece)
for position in x:
if self.check_for_check(xposition = position, current = piece) == False:
total_positions += 1
if self.canvas.gettags(piece)[1][1:-1] == "Knight":
x = self.knightsAttack(n = 7, obstacles = obstacle, bobstacles = black_obstacles, wobstacles = white_obstacles, current = piece)
for position in x:
if self.check_for_check(xposition = position, current = piece) == False:
total_positions += 1
if self.canvas.gettags(piece)[1][1:-1] == "King":
x = self.kingsAttack(n = 7, obstacles = obstacle, bobstacles = black_obstacles, wobstacles = white_obstacles, current = piece)
for position in x:
if self.check_for_check(xposition = position, current = piece) == False:
total_positions += 1
if self.canvas.gettags(piece)[1][1:-1] == "Pawn":
x = self.pawnsAttack(n = 7, obstacles = obstacle, bobstacles = black_obstacles, wobstacles = white_obstacles, current = piece)
for position in x:
if self.check_for_check(xposition = position, current = piece) == False:
total_positions += 1
if total_positions == 0:
if self.actually_check_for_check() == True:
self.canvas.lift(self.canvas.create_image(320, 320, image = self.checkmate, anchor = CENTER, tag = "Checkmate"))
else:
self.canvas.lift(self.canvas.create_image(320, 320, image = self.stalemate_image, anchor = CENTER, tag = "Stalemate"))
def check_attack_positions(self, thepositions):
obstacle = []
for x in thepositions.values():
r, y = x
obstacle.append([r, y])
black_obstacles = []
white_obstacles = []
for key in thepositions:
if key[0] == "B":
black_obstacles.append(thepositions[key])
if key[0] == "W":
white_obstacles.append(thepositions[key])
BATTACKPOSITIONS = []
WATTACKPOSITIONS = []
self.wking_check = False
self.bking_check = False
r_q = thepositions["WKing1"][0]
c_q = thepositions["WKing1"][1]
Wking_vertical_positions = self.rooksAttack(n = 7, obstacles = obstacle, bobstacles = black_obstacles, wobstacles = white_obstacles, current = self.canvas.gettags("WKing1")[1])
Wking_diagonal_positions = self.bishopsAttack(n = 7, obstacles = obstacle, bobstacles = black_obstacles, wobstacles = white_obstacles, current = self.canvas.gettags("WKing1")[1])
Wking_knight_positions = self.knightsAttack(n = 7, obstacles = obstacle, bobstacles = black_obstacles, wobstacles = white_obstacles, current = self.canvas.gettags("WKing1")[1])
Wking_pawn_positions = [[r_q - 1, c_q - 1], [r_q + 1, c_q - 1]]
for piece in (self.canvas.find_withtag("BQueen") + self.canvas.find_withtag("BBishop")):
x = self.canvas.gettags(piece)[1]
if self.pieces_positions[x] in Wking_diagonal_positions:
self.wking_check = True
for piece in (self.canvas.find_withtag("BKnight")):
x = self.canvas.gettags(piece)[1]
if self.pieces_positions[x] in Wking_knight_positions:
self.wking_check = True
for piece in (self.canvas.find_withtag("BRook") + self.canvas.find_withtag("BQueen")):
x = self.canvas.gettags(piece)[1]
if self.pieces_positions[x] in Wking_vertical_positions:
self.wking_check = True
for position in self.pieces_positions:
if position[:-1] == "BPawn":
if self.pieces_positions[position]in Wking_pawn_positions:
self.wking_check = True
r_q = thepositions["BKing1"][0]
c_q = thepositions["BKing1"][1]
Bking_vertical_positions = self.rooksAttack(n = 7, obstacles = obstacle, bobstacles = black_obstacles, wobstacles = white_obstacles, current = self.canvas.gettags("BKing1")[1])
Bking_diagonal_positions = self.bishopsAttack(n = 7, obstacles = obstacle, bobstacles = black_obstacles, wobstacles = white_obstacles, current = self.canvas.gettags("BKing1")[1])
Bking_knight_positions = self.knightsAttack(n = 7, obstacles = obstacle, bobstacles = black_obstacles, wobstacles = white_obstacles, current = self.canvas.gettags("BKing1")[1])
Bking_pawn_positions = [[r_q - 1, c_q + 1], [r_q + 1, c_q + 1]]
for piece in (self.canvas.find_withtag("WQueen") + self.canvas.find_withtag("WBishop")):
x = self.canvas.gettags(piece)[1]
if self.pieces_positions[x] in Bking_diagonal_positions:
self.bking_check = True
for piece in (self.canvas.find_withtag("WKnight")):
x = self.canvas.gettags(piece)[1]
if self.pieces_positions[x] in Bking_knight_positions:
self.bking_check = True
for piece in (self.canvas.find_withtag("WRook") + self.canvas.find_withtag("WQueen")):
x = self.canvas.gettags(piece)[1]
if self.pieces_positions[x] in Bking_vertical_positions:
self.bking_check = True
for position in self.pieces_positions:
if position[:-1] == "WPawn":
if self.pieces_positions[position] in Bking_pawn_positions:
self.bking_check = True
#HYPOTHETICALLY CHECKING
def hypothetical_check_attack_positions(self, thepositions, current):
hypothetical_wking_check = False
hypothetical_bking_check = False
for y in thepositions:
if thepositions[self.canvas.gettags(current)[1]] == thepositions[y] and self.canvas.gettags(current)[1] != y:
thepositions[y] = [2000, 2000]
obstacle = []
for x in thepositions.values():
r, y = x
obstacle.append([r, y])
black_obstacles = []
white_obstacles = []
for key in thepositions:
if key[0] == "B":
black_obstacles.append(thepositions[key])
if key[0] == "W":
white_obstacles.append(thepositions[key])
BATTACKPOSITIONS = []
WATTACKPOSITIONS = []
c_q = thepositions["WKing1"][0]
r_q = thepositions["WKing1"][1]
Wking_vertical_positions = self.hypotheticalrooksAttack(n = 7, obstacles = obstacle, bobstacles = black_obstacles, wobstacles = white_obstacles, current = [c_q, r_q, self.canvas.gettags("WKing1")[2]])
Wking_diagonal_positions = self.hypotheticalbishopsAttack(n = 7, obstacles = obstacle, bobstacles = black_obstacles, wobstacles = white_obstacles, current = [c_q, r_q, self.canvas.gettags("WKing1")[2]])
Wking_knight_positions = self.hypotheticalknightsAttack(n = 7, obstacles = obstacle, bobstacles = black_obstacles, wobstacles = white_obstacles, current = [c_q, r_q, self.canvas.gettags("WKing1")[2]])
Wking_pawn_positions = [[c_q - 1, r_q - 1], [c_q + 1, r_q - 1]]
Wking_king_positions = [[c_q - 1, r_q - 1], [c_q + 1, r_q + 1], [c_q - 1, r_q + 1], [c_q + 1, r_q - 1], [c_q, r_q + 1], [c_q, r_q - 1], [c_q + 1, r_q], [c_q - 1, r_q]]
for piece in (self.canvas.find_withtag("BQueen") + self.canvas.find_withtag("BBishop")):
x = self.canvas.gettags(piece)[1]
if thepositions[x] in Wking_diagonal_positions:
hypothetical_wking_check = True
for piece in (self.canvas.find_withtag("BKnight")):
x = self.canvas.gettags(piece)[1]
if thepositions[x] in Wking_knight_positions:
hypothetical_wking_check = True
for piece in (self.canvas.find_withtag("BRook") + self.canvas.find_withtag("BQueen")):
x = self.canvas.gettags(piece)[1]
if thepositions[x] in Wking_vertical_positions:
hypothetical_wking_check = True
for position in thepositions:
if position[:-1] == "BPawn":
if thepositions[position]in Wking_pawn_positions:
hypothetical_wking_check = True
for position in thepositions:
if position[:-1] == "BKing":
if thepositions[position] in Wking_king_positions:
hypothetical_wking_check = True
c_q = thepositions["BKing1"][0]
r_q = thepositions["BKing1"][1]
Bking_vertical_positions = self.hypotheticalrooksAttack(n = 7, obstacles = obstacle, bobstacles = black_obstacles, wobstacles = white_obstacles, current = [c_q, r_q, self.canvas.gettags("BKing1")[2]])
Bking_diagonal_positions = self.hypotheticalbishopsAttack(n = 7, obstacles = obstacle, bobstacles = black_obstacles, wobstacles = white_obstacles, current = [c_q, r_q, self.canvas.gettags("BKing1")[2]])
Bking_knight_positions = self.hypotheticalknightsAttack(n = 7, obstacles = obstacle, bobstacles = black_obstacles, wobstacles = white_obstacles, current = [c_q, r_q, self.canvas.gettags("BKing1")[2]])
Bking_pawn_positions = [[c_q - 1, r_q + 1], [c_q + 1, r_q + 1]]
Bking_king_positions = [[c_q - 1, r_q - 1], [c_q + 1, r_q + 1], [c_q - 1, r_q + 1], [c_q + 1, r_q - 1], [c_q, r_q + 1], [c_q, r_q - 1], [c_q + 1, r_q], [c_q - 1, r_q]]
for piece in (self.canvas.find_withtag("WQueen") + self.canvas.find_withtag("WBishop")):
x = self.canvas.gettags(piece)[1]
if thepositions[x] in Bking_diagonal_positions:
hypothetical_bking_check = True
for piece in (self.canvas.find_withtag("WKnight")):
x = self.canvas.gettags(piece)[1]
if thepositions[x] in Bking_knight_positions:
hypothetical_bking_check = True
for piece in (self.canvas.find_withtag("WRook") + self.canvas.find_withtag("WQueen")):
x = self.canvas.gettags(piece)[1]
if thepositions[x] in Bking_vertical_positions:
hypothetical_bking_check = True
for position in thepositions:
if position[:-1] == "WPawn":
if thepositions[position]in Bking_pawn_positions:
hypothetical_bking_check = True
for position in thepositions:
if position[:-1] == "WKing":
if thepositions[position] in Bking_king_positions:
hypothetical_bking_check = True
return [hypothetical_wking_check, hypothetical_bking_check]
def check_for_check(self, xposition, current):
Is_it_Check = False
hypothetical_positions = self.pieces_positions.copy()
hypothetical_positions[self.canvas.gettags(current)[1]] = xposition
if self.hypothetical_check_attack_positions(thepositions = hypothetical_positions, current = current)[0] == True and self.canvas.gettags(current)[2] == "W":
Is_it_Check = True
if self.hypothetical_check_attack_positions(thepositions = hypothetical_positions, current = current)[1] == True and self.canvas.gettags(current)[2] == "B":
Is_it_Check = True
return Is_it_Check
def hypotheticalbishopsAttack(self, n, obstacles, bobstacles, wobstacles, current):
c_q = current[0]
r_q = current[1]
W = False
B = False
if current[2] == "W":
W = True
if current[2] == "B":
B = True
diagonal_positions = []
#OBSTACLES
bottom_right = [None, None]
top_right = [None, None]
bottom_left = [None, None]
top_left = [None, None]
#OBSTACLES
#GETTING CLOSEST OBSTACLES
for obstacle in obstacles:
c, r = obstacle
if r - r_q > 0 and c - c_q < 0 and abs(r - r_q) == abs(c - c_q):
z, m = bottom_left
if z == None and m == None:
bottom_left = [c, r]
elif r - r_q < m - r_q and c - c_q > z - c_q:
bottom_left = [c, r]
if r - r_q < 0 and c - c_q < 0 and abs(r - r_q) == abs(c - c_q):
z, m = top_left
if z == None and m == None:
top_left = [c, r]
elif r - r_q > m - r_q and c - c_q > z - c_q:
top_left = [c, r]
if r - r_q > 0 and c - c_q > 0 and abs(r - r_q) == abs(c - c_q):
z, m = bottom_right
if z == None and m == None:
bottom_right = [c, r]
elif r - r_q < m - r_q and c - c_q < z - c_q:
bottom_right = [c, r]
if r - r_q < 0 and c - c_q > 0 and abs(r - r_q) == abs(c - c_q):
z, m = top_right
if z == None and m == None:
top_right = [c, r]
elif r - r_q < m - r_q and c - c_q < z - c_q:
top_right = [c, r]
if W == True:
if top_left in bobstacles:
top_left[1] -= 1
top_left[0] -= 1
if top_right in bobstacles:
top_right[1] -= 1
top_right[0] += 1
if bottom_left in bobstacles:
bottom_left[1] += 1
bottom_left[0] -= 1
if bottom_right in bobstacles:
bottom_right[1] += 1
bottom_right[0] += 1
if B == True:
if top_left in wobstacles:
top_left[1] -= 1
top_left[0] -= 1
if top_right in wobstacles:
top_right[1] -= 1
top_right[0] += 1
if bottom_left in wobstacles:
bottom_left[1] += 1
bottom_left[0] -= 1
if bottom_right in wobstacles:
bottom_right[1] += 1
bottom_right[0] += 1
#GETTING ALL THE POSITIONS
for i in range(1, n):
if r_q - i >= 0 and c_q + i <= n:
if top_right[0] == None:
diagonal_positions.append([c_q + i, r_q - i])
elif r_q - i > top_right[1] and c_q + i < top_right[0]:
diagonal_positions.append([c_q + i, r_q - i])
if r_q + i <= n and c_q + i <= n:
if bottom_right[1] == None:
diagonal_positions.append([c_q + i, r_q + i])
elif r_q + i < bottom_right[1] and c_q + i < bottom_right[0]:
diagonal_positions.append([c_q + i, r_q + i])
if r_q - i >= 0 and c_q - i >= 0:
if top_left[1] == None:
diagonal_positions.append([c_q - i, r_q - i])
elif r_q - i > top_left[1] and c_q - i > top_left[0]:
diagonal_positions.append([c_q - i, r_q - i])
if r_q + i <= n and c_q - i >= 0:
if bottom_left[1] == None:
diagonal_positions.append([c_q - i, r_q + i])
elif r_q + i < bottom_left[1] and c_q - i > bottom_left[0]:
diagonal_positions.append([c_q - i, r_q + i])
return diagonal_positions
def hypotheticalknightsAttack(self, n, obstacles, bobstacles, wobstacles, current):
c_q = current[0]
r_q = current[1]
W = False
B = False
if current[2] == "W":
W = True
if current[2] == "B":
B = True
positions = [[c_q - 2, r_q - 1], [c_q - 2, r_q + 1], [c_q - 1, r_q + 2], [c_q + 1, r_q + 2], [c_q + 2, r_q + 1], [c_q + 2, r_q - 1], [c_q - 1, r_q - 2], [c_q + 1, r_q - 2]]
remove_from_positions = []
for position in positions:
if position[0] < 0 or position[0] > 7 or position[1] < 0 or position[1] > 7:
remove_from_positions.append(position)
if W == True:
for obstacle in wobstacles:
for position in positions:
if position == obstacle:
remove_from_positions.append(position)
if B == True:
for obstacle in bobstacles:
for position in positions:
if position == obstacle:
remove_from_positions.append(position)
for position in remove_from_positions:
if position in positions:
positions.remove(position)
return positions
def hypotheticalrooksAttack(self, n, obstacles, bobstacles, wobstacles, current):
c_q = current[0]
r_q = current[1]
W = False
B = False
if current[2] == "W":
W = True
if current[2] == "B":
B = True
vertical_positions = []
horizontal_positions = []
#OBSTACLES
right = [None, None]
left = [None, None]
top = [None, None]
bottom = [None, None]
#OBSTACLES
#GETTING CLOSEST OBSTACLES
for obstacle in obstacles:
c, r = obstacle
if c == c_q and r < r_q:
z, m = top
if z == None and m == None:
top = [c, r]
elif r - r_q > m - r_q:
top = [c, r]
if c == c_q and r > r_q:
z, m = bottom
if z == None and m == None:
bottom = [c, r]
elif r - r_q < m - r_q:
bottom = [c, r]
if r == r_q and c > c_q:
z, m = right
if z == None and m == None:
right = [c, r]
elif c - c_q < z - c_q:
right = [c, r]
if r == r_q and c < c_q:
z, m = left
if z == None and m == None:
left = [c, r]
elif c - c_q > z - c_q:
left = [c, r]
if W == True:
if top in bobstacles:
top[1] -= 1
if bottom in bobstacles:
bottom[1] += 1
if right in bobstacles:
right[0] += 1
if left in bobstacles:
left[0] -= 1
if B == True:
if top in wobstacles:
top[1] -= 1
if bottom in wobstacles:
bottom[1] += 1
if right in wobstacles:
right[0] += 1
if left in wobstacles:
left[0] -= 1
#GETTING ALL THE POSITIONS
for i in range(1, n):
if c_q + i <= n:
if right[0] == None:
horizontal_positions.append([c_q + i, r_q])
elif c_q + i < right[0]:
horizontal_positions.append([c_q + i, r_q])
if c_q - i >= 0:
if left[0] == None:
horizontal_positions.append([c_q - i, r_q])
elif c_q - i > left[0]:
horizontal_positions.append([c_q - i, r_q])
if r_q - i >= 0:
if top[1] == None:
vertical_positions.append([c_q, r_q - i])
elif r_q - i > top[1]:
vertical_positions.append([c_q, r_q - i])
if r_q + i <= n:
if bottom[1] == None:
vertical_positions.append([c_q, r_q + i])
elif r_q + i < bottom[1]:
vertical_positions.append([c_q, r_q + i])
return vertical_positions + horizontal_positions
#INDIVIDUAL PIECE MOVEMENT RESTRICTIONS
def pawnsAttack(self, n, obstacles, bobstacles, wobstacles, current):
c_q = int(self.canvas.coords(current)[0]/80)
r_q = int(self.canvas.coords(current)[1]/80)
W = False
B = False
if self.canvas.gettags(current)[1][0] == "W":
W = True
if self.canvas.gettags(current)[1][0] == "B":
B = True
positions = []
if (c_q * 80 + 40, r_q * 80 + 40) == (self.base_pieces_positions[self.canvas.gettags(current)[1]][0] * 80 + 40, self.base_pieces_positions[self.canvas.gettags(current)[1]][1] * 80 + 40):
if self.canvas.gettags(current)[1][0] == "B":
if [c_q, r_q + 2] in obstacles or [c_q, r_q + 1] in obstacles:
pass
else:
positions.append([c_q, r_q + 2])
if self.canvas.gettags(current)[1][0] == "W":
if [c_q, r_q - 2] in obstacles or [c_q, r_q - 1] in obstacles:
pass
else:
positions.append([c_q, r_q - 2])
if self.canvas.gettags(current)[1][0] == "B":
if [c_q, r_q + 1] in obstacles:
pass
else:
positions.append([c_q, r_q + 1])
if self.canvas.gettags(current)[1][0] == "W":
if [c_q, r_q - 1] in obstacles:
pass
else:
positions.append([c_q, r_q - 1])
for obstacle in obstacles:
r, y = obstacle
if self.canvas.gettags(current)[1][0] == "B" and ([r, y] == [c_q - 1, r_q + 1] or [r, y] == [c_q + 1, r_q + 1]):
positions.append([r, y])
if self.canvas.gettags(current)[1][0] == "W" and ([r, y] == [c_q - 1, r_q - 1] or [r, y] == [c_q + 1, r_q - 1]):
positions.append([r, y])
remove_from_positions = []
for position in positions:
if position[0] < 0 or position[0] > 7 or position[1] < 0 or position[1] > 7:
remove_from_positions.append(position)
if len(self.moves) == 0:
pass
elif self.moves[-1][0][0][1:-1] == "Pawn" and abs(self.moves[-1][0][2]) == 2:
c, s = self.pieces_positions[self.canvas.gettags(current)[1]]
r, y = self.pieces_positions[self.moves[-1][0][0]]
if s == y and abs(c - r) == 1:
if W == True:
if abs(c) - abs(r) < 0:
positions.append([c_q + 1, r_q - 1])
if abs(c) - abs(r) > 0:
positions.append([c_q - 1, r_q - 1])
if B == True:
if abs(c) - abs(r) < 0:
positions.append([c_q + 1, r_q + 1])
if abs(c) - abs(r) > 0:
positions.append([c_q - 1, r_q + 1])
if W == True:
for obstacle in wobstacles:
for position in positions:
if position == obstacle:
remove_from_positions.append(position)
if B == True:
for obstacle in bobstacles:
for position in positions:
if position == obstacle:
remove_from_positions.append(position)
for position in remove_from_positions:
if position in positions:
positions.remove(position)
return positions
def kingsAttack(self, n, obstacles, bobstacles, wobstacles, current):
c_q = int(self.canvas.coords(current)[0]/80)
r_q = int(self.canvas.coords(current)[1]/80)
W = False
B = False
if self.canvas.gettags(current)[1][0] == "W":
W = True
if self.canvas.gettags(current)[1][0] == "B":
B = True
right = [None, None]
left = [None, None]
for obstacle in obstacles:
c, r = obstacle
if r == r_q and c > c_q:
z, m = right
if z == None and m == None:
right = [c, r]
elif c - c_q < z - c_q: