-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogic.py
1368 lines (1127 loc) · 56.2 KB
/
logic.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 logmessage
def define_basic_global_variables():
global blrook1, blhor1, blbish1, blqueen, blking, blbish2, blhor2, blrook2, p1, p2, p3, p4, p5, p6, p7, p8,emp,P1, P2, P3, P4, P5, P6, P7, P8, whrook1, whhor1, whbish1, whqueen, whking, whbish2, whhor2, whrook2, colour, list2d, List_of_Moves, chance, enpassant_possible, pawnnum
#Creating White pieces
whrook1=["rook", "*][", 7, 0, False] #Identity(no shortforms. Full name), Symbol(It will be modified based on the "piece" choice given by the user), Rownumber, Columnnumber, Moved?
whrook2=["rook", "*][", 7, 7, False] #The numbers for rownumber and columnumber are based on 2D list indexing and not Usual Chess Address
whhor1=["horse", "*/>", 7, 1]
whhor2=["horse", "*/>", 7, 6]
whbish1=["bishop", "*A", 7, 2]
whbish2=["bishop", "*A", 7, 5]
whqueen=["queen", "*Q", 7, 3]
whking=["king", "*$", 7, 4, False]
P1=["pawn", "*^", 6, 0, False] #False indicating that the pawn cannot be a victim of en passant capture. When it is susceptible, it will be changed to True.
P2=["pawn", "*^", 6, 1, False]
P3=["pawn", "*^", 6, 2, False]
P4=["pawn", "*^", 6, 3, False]
P5=["pawn", "*^", 6, 4, False]
P6=["pawn", "*^", 6, 5, False]
P7=["pawn", "*^", 6, 6, False]
P8=["pawn", "*^", 6, 7, False]
#Creating Black pieces
blrook1=["rook", "][", 0, 0, False] #Identity(no shortforms. Full name), Symbol(It will be modified based on the "piece" choice given by the user), Rownumber, Columnnumber
blrook2=["rook", "][", 0, 7, False] #The numbers for rownumber and columnumber are based on 2D list indexing and not Usual Chess Address
blhor1=["horse", "/>", 0, 1]
blhor2=["horse", "/>", 0, 6]
blbish1=["bishop", "A", 0, 2]
blbish2=["bishop", "A", 0, 5]
blqueen=["queen", "Q", 0, 3]
blking=["king", "$", 0, 4, False]
p1=["pawn", "^", 1, 0, False]
p2=["pawn", "^", 1, 1, False]
p3=["pawn", "^", 1, 2, False]
p4=["pawn", "^", 1, 3, False]
p5=["pawn", "^", 1, 4, False]
p6=["pawn", "^", 1, 5, False]
p7=["pawn", "^", 1, 6, False]
p8=["pawn", "^", 1, 7, False]
emp=["", "", 0, 0] #Empty
list2d=[[blrook1, blhor1, blbish1, blqueen, blking, blbish2, blhor2, blrook2],[p1, p2, p3, p4, p5, p6, p7, p8],[emp]*8,[emp]*8,[emp]*8,[emp]*8,[P1, P2, P3, P4, P5, P6, P7, P8],[whrook1, whhor1, whbish1, whqueen, whking, whbish2, whhor2, whrook2]]
List_of_Moves=[]
chance="white"
enpassant_possible=False #Is an en passant capture possible in the next move?
pawnnum=1 #A number used to identify the pieces which are formed after a pawnpromotion
def copy_of_init_list2d():
#None of the below variables are global variables. ALL of them are LOCAL variables.
#Creating White pieces
whrook1=["rook", "*][", 7, 0, False] #Identity(no shortforms. Full name), Symbol(It will be modified based on the "piece" choice given by the user), Rownumber, Columnnumber, Moved?
whrook2=["rook", "*][", 7, 7, False] #The numbers for rownumber and columnumber are based on 2D list indexing and not Usual Chess Address
whhor1=["horse", "*/>", 7, 1]
whhor2=["horse", "*/>", 7, 6]
whbish1=["bishop", "*A", 7, 2]
whbish2=["bishop", "*A", 7, 5]
whqueen=["queen", "*Q", 7, 3]
whking=["king", "*$", 7, 4, False]
P1=["pawn", "*^", 6, 0, False] #False indicating that the pawn cannot be a victim of en passant capture. When it is susceptible, it will be changed to True.
P2=["pawn", "*^", 6, 1, False]
P3=["pawn", "*^", 6, 2, False]
P4=["pawn", "*^", 6, 3, False]
P5=["pawn", "*^", 6, 4, False]
P6=["pawn", "*^", 6, 5, False]
P7=["pawn", "*^", 6, 6, False]
P8=["pawn", "*^", 6, 7, False]
#Creating Black pieces
blrook1=["rook", "][", 0, 0, False] #Identity(no shortforms. Full name), Symbol(It will be modified based on the "piece" choice given by the user), Rownumber, Columnnumber
blrook2=["rook", "][", 0, 7, False] #The numbers for rownumber and columnumber are based on 2D list indexing and not Usual Chess Address
blhor1=["horse", "/>", 0, 1]
blhor2=["horse", "/>", 0, 6]
blbish1=["bishop", "A", 0, 2]
blbish2=["bishop", "A", 0, 5]
blqueen=["queen", "Q", 0, 3]
blking=["king", "$", 0, 4, False]
p1=["pawn", "^", 1, 0, False]
p2=["pawn", "^", 1, 1, False]
p3=["pawn", "^", 1, 2, False]
p4=["pawn", "^", 1, 3, False]
p5=["pawn", "^", 1, 4, False]
p6=["pawn", "^", 1, 5, False]
p7=["pawn", "^", 1, 6, False]
p8=["pawn", "^", 1, 7, False]
emp=["", "", 0, 0] #Empty
return [[blrook1, blhor1, blbish1, blqueen, blking, blbish2, blhor2, blrook2],[p1, p2, p3, p4, p5, p6, p7, p8],[emp]*8,[emp]*8,[emp]*8,[emp]*8,[P1, P2, P3, P4, P5, P6, P7, P8],[whrook1, whhor1, whbish1, whqueen, whking, whbish2, whhor2, whrook2]]
#generation of a requests link
def generate_code():
return 1234
def encrypt(x):
return x
#Aim: To display any list for debugging purposes
def displaylist(lst):
colwd=25
print("-"*(int(5*colwd)))
for i in lst:
for j in i:
if colour(j)!="empty":
clr=colour(j)
else:
clr=""
print(clr+" "+j[0], end=" "*((colwd//2)-len(j[0]+" "+clr))+"|" + " "*(colwd//7))
print()
print("-"*(int(5*colwd)))
#Parameters: Starting address and the ending address as tuples
#Function: In list2d, the piece present in the starting address is moved to the ending address making 3 important changes
def move(add1, add2):
global list2d
list2d[add2[0]][add2[1]]=list2d[add1[0]][add1[1]]
list2d[add1[0]][add1[1]]=emp
list2d[add2[0]][add2[1]][2]=add2[0]
list2d[add2[0]][add2[1]][3]=add2[1]
#Parameters: Starting address and the ending address as tuples, the list in which the move is to be made
#Function: In the list given as a parameter, the piece present in the starting address is moved to the ending address making 3 important changes
def validatemove(add1,add2,testf2d):
testf2d[add2[0]][add2[1]]=testf2d[add1[0]][add1[1]]
testf2d[add1[0]][add1[1]]=["", "", 0, 0]
testf2d[add2[0]][add2[1]]=[testf2d[add2[0]][add2[1]][0], testf2d[add2[0]][add2[1]][1], add2[0], add2[1]]
#Aim: To get the colour of the piece
#Parameter: Piece
#Result: "black", "white" or "empty"
def colour(piece):
if piece[1]=="":
return "empty"
elif piece[1][0]=="*":
return "white"
else:
return "black"
#Given a color, the opposite colour is returned
def oppcolour(clr): #Opposite Colour
if clr=="black":
return "white"
elif clr=="white":
return "black"
#Aim: To check if two pieces belong to the same team
#Parameters: Example: whrook1, whbish2
#Return: True if they belong to the same team and False if they belong to different teams or if one of the pieces is ["", "", 0, 0]-->empty space
#Explanation: whrook1=["rook", "*...", rownumber, columnnumber], whbish2=["bishop", "*...", rownumber, columnnumber]. If the zeroth index of the 1st index of each of the two pieces is equal to *, both are white. If both do not contain *, they are both black. If only one of them contain, they are opposite teams. whrook1 and whbish2 belong to the same team.
def sameteam(pc1, pc2):
if pc1[1]!="" and pc2[1]!="":
if pc1[1][0]=="*" and pc2[1][0]=="*":
return True
elif pc1[1][0]!="*" and pc2[1][0]!="*":
return True
else:
return False
else:
return False
#Aim: (To detect a CHECK, To check if a move will be intercepted by a piece) Given a starting address(based on 2D list indexing) and an ending address(same indexing), finding out all the pieces in between the two addresses on the CHESSBOARD STARTING from the starting address and moving to the ending address.
#Parameters: starting address[given as a tuple], ending address[given as a tuple], a 2 dimensional list
#Note: If the starting address and the ending address lie in the same row, same column or same diagonal, the operation can be done. Otherwise, it cannot be done.
#Return: if pieces present in between return the Pieces in a tuple; if no pieces present, return empty tuple; if operation cannot be carried out, return "Invalid"
#Example: parameters --> (0,1), (7,1), list2d ; output --> (value of p2, value of P2, value of whhor1) ==> Output is a tuple not a list AND the element/piece present in the starting address is NOT given in the output
#Explanation: [Refer to the chessboard on google docs] Between the two addresses given, p2, P2, whhor1 are present. blhor1 is NOT included.
def piecesbetween(add1, add2, f2d):
if add1==add2:
return ()
if add1[0]==add2[0] or add1[1]==add2[1] or abs(add1[0] - add2[0])==abs(add1[1]- add2[1]):
path=()
if add1[0]==add2[0]:
stepr=0
else:
stepr=(add2[0]-add1[0])//abs(add2[0]-add1[0])
if add1[1]==add2[1]:
stepc=0
else:
stepc=(add2[1]-add1[1])//abs(add2[1]-add1[1])
curadd=(add1[0]+stepr, add1[1]+stepc) #Current address at which we are present and checking for pieces
complete=False
while complete==False:
if curadd==add2:
complete=True
if f2d[curadd[0]][curadd[1]] ==["", "", 0, 0]:
pass
else:
path+=(f2d[curadd[0]][curadd[1]],)
curadd=(curadd[0]+stepr, curadd[1]+stepc) #Changing the value of the current address to the next address which we should check
return path
else:
return "Invalid"
#Returns the rowstepcount and columnstepcount between the starting address and the ending address (NOT in a tuple)
#Possible return values are: 0,1 ; 0,-1 ; 1,0 ; -1,0 ; 1,1 ; 1,-1 ; -1,1 ; -1,-1
def stepcount(add1, add2):
if add1[0]==add2[0]:
stepr=0
else:
stepr=(add2[0]-add1[0])//abs(add2[0]-add1[0])
if add1[1]==add2[1]:
stepc=0
else:
stepc=(add2[1]-add1[1])//abs(add2[1]-add1[1])
return stepr, stepc
#Aim: to check if a move given by the user is valid
#Parameters: Starting address(2D Indexing)(tuple), Ending Address(2D Indexing)(tuple), 2 dimensional list
#Return: True if the move is valid and False if the move is invalid.
def rookmove(add1, add2, f2d):
level=0
#1st Level
if add1[0]==add2[0]: #row numbers
level+=1
elif add1[1]==add2[1]: #Column Numbers
level+=1
else:
return False
#2nd Level
pathpieces=piecesbetween(add1, add2, f2d)
if pathpieces ==():
level+=1
elif len(pathpieces)==1 and (pathpieces[0][2], pathpieces[0][3])==add2 and sameteam(pathpieces[0], f2d[add1[0]][add1[1]])==False:
level+=1
if level==2:
return True
else:
return False
#For a horse, we need not worry about whether there is a piece in between or not.
def hormove(add1, add2, f2d):
level=0
#1st Level
if (add1[0]==add2[0]+1 or add1[0]==add2[0]-1) and (add1[1]==add2[1]+2 or add1[1]==add2[1]-2):
level+=1
elif (add1[0]==add2[0]+2 or add1[0]==add2[0]-2) and (add1[1]==add2[1]+1 or add1[1]==add2[1]-1):
level+=1
else:
return False
#2nd Level
if sameteam(f2d[add2[0]][add2[1]], f2d[add1[0]][add1[1]])==False:
level+=1
if level==2:
return True
else:
return False
def bishmove(add1, add2, f2d):
level=0
#1st Level
if abs(add1[0]-add2[0])==abs(add1[1]-add2[1]):
level+=1
else:
return False
#2nd Level
pathpieces=piecesbetween(add1, add2, f2d)
if pathpieces ==():
level+=1
elif len(pathpieces)==1 and (pathpieces[0][2], pathpieces[0][3])==add2 and sameteam(pathpieces[0], f2d[add1[0]][add1[1]])==False:
level+=1
if level==2:
return True
else:
return False
def queenmove(add1, add2, f2d):
level=0
#1st Level
if add1[0]==add2[0] or add1[1]==add2[1]:
level+=1
elif abs(add1[0]-add2[0])==abs(add1[1]-add2[1]):
level+=1
else:
return False
#2nd Level
pathpieces=piecesbetween(add1, add2, f2d)
if pathpieces ==():
level+=1
elif len(pathpieces)==1 and (pathpieces[0][2], pathpieces[0][3])==add2 and sameteam(pathpieces[0], f2d[add1[0]][add1[1]])==False:
level+=1
if level==2:
return True
else:
return False
def kingmove(add1, add2, f2d):
level=0
#1st Level
#(add1[0]==add2[0]+1 or add1[0]==add2[0]-1 or add1[0]==add2[0]) and (add1[1]==add2[1] or add1[1]==add2[1]-1 or add1[1]==add2[1]+1)
if (add1[0]-add2[0])**2 + (add1[1]-add2[1])**2<=2:
level+=1
else :
return False
#2nd Level
pathpieces=piecesbetween(add1, add2, f2d)
if pathpieces ==():
level+=1
elif len(pathpieces)==1 and (pathpieces[0][2], pathpieces[0][3])==add2 and sameteam(pathpieces[0], f2d[add1[0]][add1[1]])==False:
level+=1
if level==2:
return True
else:
return False
#Parameters: Two addresses are given as tuples, 2 dimensional list
#Will return True if castling is possible, False if not possible
def castle(add1, add2, f2d):
if not(0<=add1[0]<=7 and 0<=add1[1]<=7 and 0<=add2[0]<=7 and 0<=add2[1]<=7):
return (False, ((0,0), (0,0)), ((0,0), (0,0)))
#Converting add2 to possible add of rook
if add2[1] > add1[1]:
add2 = (add2[0], add2[1] + 1)
elif add2[1] < add1[1]:
add2 = (add2[0], add2[1] - 2)
#Getting the relevant pieces
pc1=f2d[add1[0]][add1[1]]
pc2=f2d[add2[0]][add2[1]]
caslevel1=False
if pc1[0]=="king" and pc2[0]=="rook":
caslevel1=True
logmessage.log(" Caslevel1: (King has been moved correctly for a castle) ", caslevel1)
caslevel1_1=False
if caslevel1==True:
if abs(add1[1]-add2[1])==3:
if f2d[add1[0]][pc2[3]-1]==["", "", 0, 0] and f2d[add1[0]][pc2[3]-2]==["", "", 0, 0]:
caslevel1_1=True
elif abs(add1[1]-add2[1])==4:
if f2d[add1[0]][pc2[3]+1]==["", "", 0, 0] and f2d[add1[0]][pc2[3]+2]==["", "", 0, 0] and f2d[add1[0]][pc2[3]+3]==["", "", 0, 0] :
caslevel1_1=True
if caslevel1==True:
logmessage.log(" Caslevel1_1: (Squares in between are empty) ", caslevel1_1)
caslevel2=False
if caslevel1_1==True and check((pc1[2], pc1[3]), f2d)[0]==False:
caslevel2=True
if caslevel1_1==True:
logmessage.log(" Caslevel2: (Castling king is not facing a check) ", caslevel2)
caslevel3=False
if caslevel2==True and sameteam(pc1, pc2)==True and pc1[4]==False and pc2[4]==False and len(piecesbetween(add1, add2, f2d))==1:
caslevel3=True
if caslevel2==True:
logmessage.log(" Caslevel3: (Both the pieces belong to the same team) ", caslevel3)
caslevel4=False
if caslevel3==True:
clist2d=[]
for i in f2d:
temp=[]
for j in i:
temp.append(j.copy())
clist2d.append(temp)
if abs(add1[1]-add2[1])==3:
validatemove((pc1[2], pc1[3]), (pc1[2], pc1[3]+1), clist2d)
if check((pc1[2], pc1[3]+1), clist2d)[0]==False:
caslevel4=True
newkingadd=(pc1[2], pc1[3]+2)
newrookadd=(pc2[2], pc2[3]-2)
elif abs(add1[1]-add2[1])==4:
validatemove((pc1[2], pc1[3]), (pc1[2], pc1[3]-1), clist2d)
if check((pc1[2], pc1[3]-1), clist2d)[0]==False:
caslevel4=True
newkingadd=(pc1[2], pc1[3]-2)
newrookadd=(pc2[2], pc2[3]+3)
if caslevel4==True:
castleinfo=(True, ((pc1[2], pc1[3]), newkingadd), ((pc2[2], pc2[3]), newrookadd))
return (True, ((pc1[2], pc1[3]), newkingadd), ((pc2[2], pc2[3]), newrookadd)) #Castle is possible or not?, King's starting and ending add, rook's S and E add
elif caslevel4==False:
return (False, ((0,0), (0,0)), ((0,0), (0,0)))
#Movement of the pawn is slightly complicated. Depending on whether it is black or white, it can only move in ONE direction. It can move CROSS only if something can be attacked.
#Parameters: Starting address[2D indexing], ending address[2D indexing], 2 dimensional list
#Return: if the move is straight and there is 'NO piece in the ending address' return True; if the move is cross there is a piece of the OPPOSITE team in the ending address return True
#Incorporated starting double move and En Passant also
#The returned tuple consists of 3 boolean values and 1 tuple: Is the move possible?, Is the moving pawn a possible en passant victim in the next move?, Is the moving pawn killing another pawn by en passant?, The address of the pawn which has to be killed.
def pawnmove(add1, add2, f2d):
if f2d[add1[0]][add1[1]][1]=="*^": #White Checking
if add2[0]-add1[0]==-1: #For white, row should decrease.
if add1[1]==add2[1]: #Checking if the column is the same
if f2d[add2[0]][add2[1]][1]=="": #Checking if the ending address is empty
return (True, False, False, None)
else:
return (False, False,False, None)
elif abs(add2[1]-add1[1])==1: #Checking if the pawn has moved cross
if sameteam(f2d[add1[0]][add1[1]], f2d[add2[0]][add2[1]])==False and f2d[add2[0]][add2[1]]!=["", "", 0, 0]: #Checking if the ending address is occupied by a black piece
return (True, False,False, None)
elif f2d[add2[0]][add2[1]]==["", "", 0, 0] and f2d[add1[0]][add2[1]][0]=="pawn" and f2d[add1[0]][add2[1]][4]==True:
return (True, False, True, (add1[0],add2[1]))
else:
return (False, False,False, None)
elif add2[0]-add1[0]==-2 and add1[0]==6:
if add1[1]==add2[1]: #Checking if the column is the same
if f2d[add2[0]][add2[1]][1]=="": #Checking if the ending address is empty
return (True, True,False, None)
else:
return (False, False,False, None)
else:
return (False, False, False, None)
elif f2d[add1[0]][add1[1]][1]=="^": #Black Checking
if add2[0]-add1[0]==1: #For black, row should increase.
if add1[1]==add2[1]: #Checking if the column is the same
if f2d[add2[0]][add2[1]][1]=="": #Checking if the ending address is empty
return (True, False,False, None)
else:
return (False, False,False, None)
elif abs(add2[1]-add1[1])==1: #Checking if the pawn has moved cross
if sameteam(f2d[add1[0]][add1[1]], f2d[add2[0]][add2[1]])==False and f2d[add2[0]][add2[1]]!=["", "", 0, 0]: #Checking if the ending address is occupied by a white piece
return (True, False,False, None)
elif f2d[add2[0]][add2[1]]==["", "", 0, 0] and f2d[add1[0]][add2[1]][0]=="pawn" and f2d[add1[0]][add2[1]][4]==True:
return (True, False, True,(add1[0],add2[1]))
else:
return (False, False,False, None)
elif add2[0]-add1[0]==2 and add1[0]==1:
if add1[1]==add2[1]: #Checking if the column is the same
if f2d[add2[0]][add2[1]][1]=="": #Checking if the ending address is empty
return (True, True,False, None)
else:
return (False, False,False, None)
else:
return (False, False,False, None)
return (None, None,False, None)
#Parameters: First address, Second address, 2 dimensional list
#Function: Given that a piece CAN MOVE from the first address to the second address, extend return the addresses (tuples) of all the squares following the first address along the line(first add, second add) that are either free or occupied by a piece opposite to the piece present in the starting address.
#In short, it gives the reach of the piece present in the first address given that it can move from the first address to the second address.
def extend(add1, add2, f2d):
r,c=add1[0], add1[1]
if colour(f2d[r][c])=="white":
for m in f2d:
for n in m:
if n[1]=="*$":
extendking=n #King of the piece which is being moved. We need to see if the king of the piece which is moving suffers a check or not
elif colour(f2d[r][c])=="black":
for m in f2d:
for n in m:
if n[1]=="$":
extendking=n
stepr, stepc=stepcount(add1, add2)
piece=f2d[add1[0]][add1[1]]
rowcounter, colcounter=add2[0], add2[1]
tup=()
while True:
if 0<=rowcounter<=7 and 0<=colcounter<=7:
if sameteam(piece, f2d[rowcounter][colcounter])==False:
templist2d=[]
for l in f2d:
temp=[]
for k in l:
temp.append(k.copy())
templist2d.append(temp)
validatemove((r,c), (rowcounter,colcounter), templist2d)
if check((extendking[2], extendking[3]), templist2d)[0]==False:
tup+=((rowcounter, colcounter),)
if colour(f2d[rowcounter][colcounter])=="white" or colour(f2d[rowcounter][colcounter])=="black":
break
else:
break
rowcounter+=stepr
colcounter+=stepc
else:
break
return tup
#Given an address and a 2 dimensional list, it returns the addresses of all the squares that the piece present in the starting address can go to as a TUPLE.
def legal(add,f2d):
if colour(f2d[add[0]][add[1]])=="empty":
return (False, ())
global legaladdresses
legaladdresses=()
r,c=add[0], add[1]
if colour(f2d[r][c])=="white":
for m in f2d:
for n in m:
if n[1]=="*$":
king=n #King of the piece which is being moved. We need to see if the king of the piece which is moving suffers a check or not
elif colour(f2d[r][c])=="black":
for m in f2d:
for n in m:
if n[1]=="$":
king=n
def aroundcheck(add, f2d, a1, a2, a3, a4, a5, a6, a7, a8):
global legaladdresses
r,c=add[0], add[1]
pc=f2d[r][c][0]
ru=cl=8
if r>=1:
ru=r-1
if c>=1:
cl=c-1
around=[a1,a2,a3,a4,a5,a6,a7,a8]
addresses=[(r, c+1), (ru, c+1), (ru, c), (ru, cl), (r, cl), (r+1, cl), (r+1, c), (r+1, c+1)]
for i in range(8):
if around[i]==False:
addresses[i]=(None, None)
for i,j in addresses:
try:
if i!=None:
templist2d=[]
for l in f2d:
temp=[]
for k in l:
temp.append(k.copy())
templist2d.append(temp)
if pc=="pawn":
level1=pawnmove((r,c), (i,j), templist2d)[0] #Pawn attacking and moving has different rules.
else:
level1=not(sameteam(f2d[r][c], f2d[i][j])) #All other pieces have the same rules of attacking and moving
if level1!=True:
continue
validatemove((r,c), (i,j), templist2d)
if colour(f2d[r][c])=="white":
for m in templist2d:
for n in m:
if n[1]=="*$":
teamking=n #King of the piece which is being moved. We need to see if the king of the piece which is moving suffers a check or not
elif colour(f2d[r][c])=="black":
for m in templist2d:
for n in m:
if n[1]=="$":
teamking=n
if check((teamking[2], teamking[3]), templist2d)[0]==False:
if pc=="king" or pc=="horse" or pc=="pawn":
legaladdresses+=((i,j),)
elif pc=="queen" or pc=="bishop" or pc=="rook":
if colour(f2d[i][j])=="empty":
for k in extend(add, (i,j), f2d):
legaladdresses+=(k,)
else:
legaladdresses+=((i,j),)
if pc=="pawn" and colour(f2d[i][j])=="empty":
stepr=i-r
nextr=i+stepr
templist2d=[]
for l in f2d:
temp=[]
for k in l:
temp.append(k.copy())
templist2d.append(temp)
validatemove((r,c), (nextr, j), templist2d)
if pawnmove((r,c), (nextr, j), f2d)[0]==True:
if check((teamking[2], teamking[3]), templist2d)[0]==False:
legaladdresses+=((nextr, j),)
except IndexError:
pass
if legaladdresses==():
return (False, legaladdresses)
else:
return (True, legaladdresses)
def aroundhorsecheck(add, f2d):
global legaladdresses
r=add[0]
c=add[1]
rowu=rowuu=coll=colll=8 #rowup, rowupup, columnleft, columnleftleft
if r-1>=0:
rowu=r-1
if r-2>=0:
rowuu=r-2
if c-1>=0:
coll=c-1
if c-2>=0:
colll=c-2
addresses=((rowu, c+2),(r+1, c+2),(rowuu, c+1),(r+2, c+1),(rowu, colll),(r+1, colll),(rowuu, coll),(r+2, coll))
for i, j in addresses:
try:
templist2d=[]
for l in f2d:
temp=[]
for k in l:
temp.append(k.copy())
templist2d.append(temp)
level1=not(sameteam(f2d[r][c], f2d[i][j]))
if level1!=True:
continue
validatemove((r,c), (i,j), templist2d)
if colour(f2d[r][c])=="white":
for m in templist2d:
for n in m:
if n[1]=="*$":
teamking=n #King of the piece which is being moved. We need to see if the king of the piece which is moving suffers a check or not
elif colour(f2d[r][c])=="black":
for m in templist2d:
for n in m:
if n[1]=="$":
teamking=n
if check((teamking[2], teamking[3]), templist2d)[0]==False:
legaladdresses+=((i,j),)
except IndexError:
pass
if legaladdresses==():
return (False, legaladdresses)
else:
return (True, legaladdresses)
if check((king[2], king[3]), f2d)[0]==True and (f2d[r][c][0]=="rook" or f2d[r][c][0]=="bishop" or f2d[r][c][0]=="queen"):
attpcs=check((king[2], king[3]), f2d)[1] #Attacking Pieces
logmessage.log("Legal: Check=True: attpcs ", attpcs)
for indattpc in attpcs: #Individual Attacking Piece
rowf=indattpc[2] #Row number of the attacking piece
colf=indattpc[3] #Column number of the attacking piece
rowstep, colstep=stepcount((king[2], king[3]), (rowf, colf)) #Gives us the rowstep value and the colstep value to gradually move from the king's address to the attackers's address
rowcounter=king[2]+rowstep
colcounter=king[3]+colstep
locations=()
if indattpc[0]=="horse":
locations+=(indattpc,)
else:
while True:
if rowcounter==rowf and colcounter==colf:
locations+=(indattpc,)
break
bwsquare=["", "", rowcounter, colcounter] #Between square
locations+=(bwsquare,)
rowcounter+=rowstep
colcounter+=colstep
for indsquares in locations: #Individual squares in between the king and the attacking species
templist2d=[]
for l in f2d:
temp=[]
for k in l:
temp.append(k.copy())
templist2d.append(temp)
checklevel1=False
if f2d[r][c][0]=="rook":
if rookmove((r,c), (indsquares[2], indsquares[3]), templist2d)==True:
validatemove((r,c), (indsquares[2], indsquares[3]), templist2d)
checklevel1=True
elif f2d[r][c][0]=="bishop":
if bishmove((r,c), (indsquares[2], indsquares[3]), templist2d)==True:
validatemove((r,c), (indsquares[2], indsquares[3]), templist2d)
checklevel1=True
elif f2d[r][c][0]=="queen":
if queenmove((r,c), (indsquares[2], indsquares[3]), templist2d)==True:
validatemove((r,c), (indsquares[2], indsquares[3]), templist2d)
checklevel1=True
if checklevel1==True:
if colour(f2d[r][c])=="white":
for m in templist2d:
for n in m:
if n[1]=="*$":
teamking=n #King of the piece which is being moved. We need to see if the king of the piece which is moving suffers a check or not
elif colour(f2d[r][c])=="black":
for m in templist2d:
for n in m:
if n[1]=="$":
teamking=n
if check((teamking[2], teamking[3]), templist2d)[0]==False:
legaladdresses+=((indsquares[2], indsquares[3]),)
else:
if f2d[r][c][0]=="king":
return aroundcheck((r,c), f2d, True, True, True, True, True, True, True, True)
elif f2d[r][c][0]=="queen":
return aroundcheck((r,c), f2d, True, True, True, True, True, True, True, True)
elif f2d[r][c][0]=="rook":
return aroundcheck((r,c), f2d, True, False, True, False, True, False, True, False)
elif f2d[r][c][0]=="bishop":
return aroundcheck((r,c), f2d, False, True, False, True, False, True, False, True)
elif f2d[r][c][0]=="pawn":
return aroundcheck((r,c), f2d, False, True, True, True,False,True,True,True)
elif f2d[r][c][0]=="horse":
return aroundhorsecheck((r,c), f2d)
if legaladdresses==():
return (False, legaladdresses)
else:
return (True, legaladdresses)
#Aim: Detection of a Check
#Parameters: king's position as a tuple, 2 dimensional list
#Result: if a check is possible, return (True, all the attacking pieces in a tuple). If not return (False, ())
def check(add,f2d):
attack=()
#Checking all pieces except Horse
leftadd=(add[0], 0)
left=piecesbetween(add, leftadd, f2d)
rightadd=(add[0], 7)
right=piecesbetween(add, rightadd, f2d)
topadd=(0, add[1])
top=piecesbetween(add, topadd, f2d)
bottomadd=(7, add[1])
bottom=piecesbetween(add, bottomadd, f2d)
neadd=(add[0]-min(abs(add[0]-0), abs(7-add[1])),add[1]+min(abs(add[0]-0), abs(7-add[1]))) #North east - ne
ne=piecesbetween(add, neadd, f2d)
nwadd=(add[0]-min(abs(add[0]-0), abs(0-add[1])),add[1]-min(abs(add[0]-0), abs(0-add[1]))) #North west - nw
nw=piecesbetween(add, nwadd, f2d)
swadd=(add[0]+min(abs(add[0]-7), abs(0-add[1])),add[1]-min(abs(add[0]-7), abs(0-add[1]))) #South west - sw
sw=piecesbetween(add, swadd, f2d)
seadd=(add[0]+min(abs(add[0]-7), abs(7-add[1])),add[1]+min(abs(add[0]-7), abs(7-add[1]))) #South east - se
se=piecesbetween(add, seadd, f2d)
alldir=[left, right, top, bottom, ne, nw, se, sw]
count=0
for i in alldir:
if i==():
pass
else:
if i[0][0]=="horse":
pass
else:
if sameteam(i[0], f2d[add[0]][add[1]])==False:
if (i[0][0]=="rook" or i[0][0]=="queen") and count in range(4):
attack+=(i[0],)
elif (i[0][0]=="bishop" or i[0][0]=="queen") and count in (4,5,6,7):
attack+=(i[0],)
elif i[0][0]=="pawn" and ((add[0]-i[0][2])**2 + (add[1]-i[0][3])**2)==2:
if i[0][1][0]=="*" and count in (6,7):
attack+=(i[0],)
elif i[0][1][0]!="*" and count in (4,5):
attack+=(i[0],)
elif i[0][0]=="king" and (i[0][2]-add[0])**2 + (i[0][3]-add[1])**2<=2:
return (True, (i[0],))
count+=1
#Checking Horse
r=add[0]
c=add[1]
rowu=rowuu=coll=colll=8 #rowup, rowupup, columnleft, columnleftleft
if r-1>=0:
rowu=r-1
if r-2>=0:
rowuu=r-2
if c-1>=0:
coll=c-1
if c-2>=0:
colll=c-2
addresses=((rowu, c+2),(r+1, c+2),(rowuu, c+1),(r+2, c+1),(rowu, colll),(r+1, colll),(rowuu, coll),(r+2, coll))
for tup in addresses:
try:
if f2d[tup[0]][tup[1]][0]=="horse" and sameteam(f2d[tup[0]][tup[1]], f2d[r][c])==False:
attack+=(f2d[tup[0]][tup[1]],)
except IndexError:
pass
if attack==():
return (False, attack)
else:
return (True, attack)
#Aim: [Underlying aim is to detect a checkmate] Given an address and its status(empty or non-empty), being able to detect whether any piece of the SPECIFIED colour can enter the address either by just occupying or attacking
#Parameters: address(tuple), empty=True or False, clr=what is the colour of the piece you are looking for to occupy the given address, 2 dimensional list
#Note: Either of the kings cannot be considered for moving into the required square.
#Return: if some piece satisfying the GIVEN conditions is present, return (True, pieces in a tuple), If ot, return (False, ())
def capture(add, empty, clr, f2d):
attack=()
leftadd=(add[0], 0)
left=piecesbetween(add, leftadd, f2d)
rightadd=(add[0], 7)
right=piecesbetween(add, rightadd, f2d)
topadd=(0, add[1])
top=piecesbetween(add, topadd, f2d)
bottomadd=(7, add[1])
bottom=piecesbetween(add, bottomadd, f2d)
neadd=(add[0]-min(abs(add[0]-0), abs(7-add[1])),add[1]+min(abs(add[0]-0), abs(7-add[1]))) #North east - ne
ne=piecesbetween(add, neadd, f2d)
nwadd=(add[0]-min(abs(add[0]-0), abs(0-add[1])),add[1]-min(abs(add[0]-0), abs(0-add[1]))) #North west - nw
nw=piecesbetween(add, nwadd, f2d)
swadd=(add[0]+min(abs(add[0]-7), abs(0-add[1])),add[1]-min(abs(add[0]-7), abs(0-add[1]))) #South west - sw
sw=piecesbetween(add, swadd, f2d)
seadd=(add[0]+min(abs(add[0]-7), abs(7-add[1])),add[1]+min(abs(add[0]-7), abs(7-add[1]))) #South east - se
se=piecesbetween(add, seadd, f2d)
alldir=[left, right, top, bottom, ne, nw, se, sw]
count=0
for i in alldir:
if i==():
pass
else:
if i[0][0]=="horse":
pass
else:
if colour(i[0])==clr:
if (i[0][0]=="rook" or i[0][0]=="queen") and count in range(4):
attack+=(i[0],)
elif (i[0][0]=="bishop" or i[0][0]=="queen") and count in (4,5,6,7):
attack+=(i[0],)
elif i[0][0]=="pawn":
if empty==True and count in (2,3):
if clr=="black" and (add[0]-i[0][2]==1 or (add[0]-i[0][2]==2 and i[0][2]==1)):
attack+=(i[0],)
elif clr=="white" and (add[0]-i[0][2]==-1 or (add[0]-i[0][2]==-2 and i[0][2]==6)):
attack+=(i[0],)
elif empty==False:
if clr=="black" and count in (4,5) and ((add[0]-i[0][2])**2 + (add[1]-i[0][3])**2 ==2):
attack+=(i[0],)
elif clr=="white" and count in (6,7) and ((add[0]-i[0][2])**2 + (add[1]-i[0][3])**2 ==2):
attack+=(i[0],)
count+=1
#Checking Horse
r=add[0]
c=add[1]
rowu=rowuu=coll=colll=8 #rowup, rowupup, columnleft, columnleftleft
if r-1>=0:
rowu=r-1
if r-2>=0:
rowuu=r-2
if c-1>=0:
coll=c-1
if c-2>=0:
colll=c-2
addresses=((rowu, c+2),(r+1, c+2),(rowuu, c+1),(r+2, c+1),(rowu, colll),(r+1, colll),(rowuu, coll),(r+2, coll))
for tup in addresses:
try:
if f2d[tup[0]][tup[1]][0]=="horse" and colour(f2d[tup[0]][tup[1]])==clr:
attack+=(f2d[tup[0]][tup[1]],)
except IndexError:
pass
if attack==():
return (False, attack)
else:
return (True, attack)
#Aim: Detecting a Checkmate
#Parameters: Address of the king for whom you would like to validate for checkmate(tuple), 2 dimensional list
#Return: True if it is a checkmate and return False if it is not a checkmate.
def checkmate(add, f2d):
attack=check(add,f2d)[1]
if attack==():
return False
logmessage.log("\nCHECK!")
logmessage.log("Pieces attacking the king:", attack)
oppclr=oppcolour(colour(f2d[add[0]][add[1]])) #The colour opposite to the king's colour which the king can attack
kingclr=colour(f2d[add[0]][add[1]])
logmessage.log("Attacked King colour: ", kingclr)
logmessage.log("Opposite King Colour: ", oppclr)
#Checking whether the king can move to a SAFE square around it either by just moving or by attacking another piece
r,c=add[0], add[1]
ru=8
cl=8
if r-1>=0:
ru=r-1
if c-1>=0:
cl=c-1
addresses=((r,c+1), (ru, c+1) ,(ru, c) ,(ru, cl) ,(r, cl) ,(r+1, cl) ,(r+1, c) ,(r+1, c+1))
logmessage.log(addresses)
for tup in addresses:
testf2d=[]
for i in f2d:
temp=[]
for j in i:
temp.append(j.copy())
testf2d.append(temp)
try:
if colour(testf2d[tup[0]][tup[1]])=="empty" or colour(testf2d[tup[0]][tup[1]])==oppclr:
validatemove((r,c), tup, testf2d)
logmessage.log("Pieces which can capture the king if it moves to the given adjacent square: ", tup, capture((tup[0], tup[1]), False, oppclr, testf2d))
capturecheck=True
if kingclr=="white":
#rok and cok-row of opposite king and column of opposite king
for i in testf2d:
for j in i:
if j[1]=="$":
rok, cok=j[2], j[3]
if (rok-tup[0])**2 + (cok-tup[1])**2<=2:
capturecheck=False
elif kingclr=="black":
#rok and cok-row of opposite king and column of opposite king
for i in testf2d:
for j in i:
if j[1]=="*$":
rok, cok=j[2], j[3]
if (rok-tup[0])**2 + (cok-tup[1])**2<=2: