-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectMoreClasses git.py
More file actions
1643 lines (1317 loc) · 60.5 KB
/
projectMoreClasses git.py
File metadata and controls
1643 lines (1317 loc) · 60.5 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
#1,1 = 0,0,
import pprint
import pygame
import time
import math #imports modules
pygame.init()
displayWidth=600
displayHeight=600
black=(0,0,0)
white=(255,255,255)
red=(255,0,0)
green=(0,255,0)
blue=(0,0,255)
movedList=open("movedList.txt","w")
movedList.write("movedlist")
movedList.close()
chessDisplay=pygame.display.set_mode((displayWidth,displayHeight))#sets up display
pygame.display.set_caption('Chess')#sets caption of window
icon=pygame.image.load("whitepawn.png")
pygame.display.set_icon(icon)
x=75
bottom=x*7
bottom2=x*6
top=0
top2=x
clock=pygame.time.Clock()
def getpiece(board,mousex,mousey):
return board[mousey][mousex]
def isemptySquare(board,x,y):
if board[y][x] == '':
return True
else:
return False
def isenemysquare(board,colour,x,y):
#pass
try:
character=board[y][x]
if colour=="black":
if character[0]=='w':
return True
else:
if character[0]=='b':
return True
except IndexError:
# print("empty")
return False
class board1():
'''keeps track of board'''
def __init__(self):
self.board=[['brook1', 'bknight1', 'bbishop1', 'bqueen', 'bking', 'bbishop2', 'bknight2', 'brook2'],
['bpawn1', 'bpawn2', 'bpawn3', 'bpawn4', 'bpawn5', 'bpawn6', 'bpawn7', 'bpawn8'],
['', '', '', '', '', '', '', ''],['', '', '', '', '', '', '', ''],
['', '', '', '', '', '', '', ''],['', '', '', '', '', '', '', ''],
['wpawn1', 'wpawn2', 'wpawn3', 'wpawn4', 'wpawn5', 'wpawn6', 'wpawn7', 'wpawn8'],
['wrook1', 'wknight1', 'wbishop1', 'wqueen', 'wking', 'wbishop2', 'wknight2', 'wrook2']]
#the array above tracks the position of every piece.
def newpiece(self,newpiecex,newpiecey,pieceName):
self.board[newpiecey][newpiecex]=pieceName
def display(self):# method which displays board
for x in range(8):
pprint.pprint(self.board[x],compact=False,width=100)
def currentBoard(self):
returnboard=[]
for x in range(8):
returnboard.append([])
count=0
for y in self.board:
for x in y:
returnboard[count].append(x)
count+=1
return returnboard
def getpiece(self,mousex,mousey):#this fetches the piece specified by paramters
return self.board[mousey][mousex]
def emptySquare(self,x,y):#this checks if the square specified is empty.
# print(x,y,"is empty square")
if self.board[y][x] == '':
return True
else:
return False
def friendlysquare(self,x,y,colour):#this checks if the square contains a piece of the current players.
try:
squareInQuestion=str(self.board[y][x])
if squareInQuestion=='':
return False
else:
team=squareInQuestion[0]
if team=='b' and colour=='black':
return True
elif team=='w' and colour=='white':
return True
else:
return False
except IndexError:
# print("empty or no enemy or off board frien")
pass
def enemysquare(self,colour,x,y):#checks if it is an enemy square
try:
character=self.board[y][x]
if colour=="black":
if character[0]=='w':
return True
else:
if character[0]=='b':
return True
except IndexError:
# print("empty or no enemy or off board enem")
return False
def move(self,currentx,currenty,newx,newy):#this updates the array after a piece has moved.
record=self.board[currenty][currentx]
self.board[currenty][currentx]=''
self.board[newy][newx]=record
self.display()
##[['brook1', 'bknight1', 'bbishop1', 'bqueen', 'bking', 'bbishop2', 'bknight2', 'broo:k2'],
##['bpawn1', 'bpawn2', 'bpawn3', 'bpawn4', 'bpawn5', 'bpawn6', 'bpawn7', 'bpawn8'],
##[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
##[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],[' ', ' ', ' ', ' ', ' ', ' ', ' ', ' '],
##['wpawn1', 'wpawn2', 'wpawn3', 'wpawn4', 'wpawn5', 'wpawn6', 'wpawn7', 'wpawn8'],
##['wrook1', 'wknight1', 'wbishop1', 'wqueen', 'wking', 'wbishop2', 'wknight2', 'wrook2']]
#self.rowA,self.rowB,self.rowC,self.rowD,self.rowE,self.rowF,self.rowG,self.rowH
class piece():
'''Basic piece class'''
def __init__(self,ptype,posy,posx,colour):
self.ptype=ptype
self.posy=posy
self.posx=posx
self.moveforw=0
self.moveLorR=0
self.movedyet=False
self.colour=colour
self.checkmateAlg=False
self.checkmateMoves=False
def update(self):
chessDisplay.blit(self.image,((self.posx)*75,self.posy*75))
def coordinates(self):
return self.posx,self.posy
def upgradePawn(self,piece,colour_of_piece):
update(colour_of_piece)
if colour_of_piece=='black':
pygame.draw.rect(chessDisplay,white,(150,262,300,75))
pygame.draw.rect(chessDisplay,black,(150,262,300,75),1)
blackqueen=pygame.image.load("blackqueen.png")
blackknight=pygame.image.load("blackknight.png")
blackrook=pygame.image.load("blackrook.png")
blackbishop=pygame.image.load("blackbishop.png")
chessDisplay.blit(blackqueen,(150,262))
chessDisplay.blit(blackknight,(225,262))
chessDisplay.blit(blackrook,(300,262))
chessDisplay.blit(blackbishop,(375,262))
else:
pygame.draw.rect(chessDisplay,white,(150,262,300,75))
pygame.draw.rect(chessDisplay,black,(150,262,300,75),1)
whitequeen=pygame.image.load("whitequeen.png")
whiteknight=pygame.image.load("whiteknight.png")
whiterook=pygame.image.load("whiterook.png")
whitebishop=pygame.image.load("whitebishop.png")
chessDisplay.blit(whitequeen,(150,262))
chessDisplay.blit(whiteknight,(225,262))
chessDisplay.blit(whiterook,(300,262))
chessDisplay.blit(whitebishop,(375,262))
pygame.display.update()
clicked1=pygame.mouse.get_pressed()
mouse1=pygame.mouse.get_pos()
while clicked1[0]==0 or (mouse1[0]>450 or mouse1[0]<150 or mouse1[1]>337 or mouse1[1]<262):
pygame.event.get()
clicked1=pygame.mouse.get_pressed()
mouse1=pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
quit()
if mouse1[1]>=262 and mouse1[1]<=337:
if mouse1[0]>=150 and mouse1[0]<=225:#queen
print("queen")
number=piece[5]
upgradePawn1=(self.colour[0]+'queen'+number)
upgradeType='queen'
globals()[upgradePawn1]=Queen(upgradeType.lower(),self.posy,self.posx,self.colour)#don't eval whole thing
if mouse1[0]>=225 and mouse1[0]<=300:#knight
print("knight")
number=piece[5]
upgradePawn1=(self.colour[0]+'knight'+number)
upgradeType='knight'
globals()[upgradePawn1]=Knight(upgradeType.lower(),self.posy,self.posx,self.colour)
if mouse1[0]>=300 and mouse1[0]<=375:#rook
print("rook")
number=piece[5]
upgradePawn1=(self.colour[0]+'rook'+number)
upgradeType='rook'
globals()[upgradePawn1]=Rook(upgradeType.lower(),self.posy,self.posx,self.colour)
if mouse1[0]>=375 and mouse1[0]<=450:#bishop
print("bishop")
number=piece[5]
upgradePawn1=(self.colour[0]+'bishop'+number)
upgradeType='bishop'
globals()[upgradePawn1]=Bishop(upgradeType.lower(),self.posy,self.posx,self.colour)
allPieces.remove(piece)
theboard.newpiece(self.posx,self.posy,upgradePawn1)
if self.colour=='black':
allPieces.insert(0,upgradePawn1)
else:
allPieces.insert(-1,upgradePawn1)
def showSquares(self,availableSquarey,availableSquarex,xPerFrame,yPerFrame):
counter=0
for x in availableSquarey:
pygame.draw.rect(chessDisplay,blue,((availableSquarex[(counter)])*75,(x*75),75,75),1)
counter+=1
pygame.display.update()
clicked1=pygame.mouse.get_pressed()
while clicked1[0]==0:
pygame.event.get()
clicked1=pygame.mouse.get_pressed()
mouse1=pygame.mouse.get_pos()
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
quit()
while clicked1[0]==1:
pygame.event.get()
clicked1=pygame.mouse.get_pressed()
for event in pygame.event.get():
if event.type==pygame.QUIT:
pygame.quit()
quit()
counter=0
SquareToMoveTo=None,None
xPerFrame,yPerFrame=None,None
for x in availableSquarey:
if (mouse1[0]>(availableSquarex[counter]*75) and mouse1[0]<((availableSquarex[counter]*75)+75))and(mouse1[1]>x*75 and mouse1[1]<(x*75)+75):
SquareToMoveTo=availableSquarex[counter],x
print("moving")
xPerFrame,yPerFrame=self.distancePerFrame(SquareToMoveTo[0],SquareToMoveTo[1])
counter+=1
print("yippee")
return SquareToMoveTo,xPerFrame,yPerFrame
def moveit(self,thesquare,xPerFrame,yPerFrame,turn,current_piece):
lastposx=self.posx
lastposy=self.posy
#maybe add a a attribute called last square or even attributes for the moving created in the algorithm above
squarex=thesquare[0]
squarey=thesquare[1]
print("moveit","x",squarex,"y",squarey,xPerFrame)
while round(self.posx,1)!=squarex or round(self.posy,1)!=squarey:
clicked1=pygame.mouse.get_pressed()
self.posx=self.posx+(xPerFrame/75)
self.posy=self.posy+(yPerFrame/75)
update(turn)
self.update()
pygame.display.update()
clock.tick(30)
self.posx=squarex #sets posx and posy to move-to-coordinates.
self.posy=squarey
takenPiece=theboard.getpiece(self.posx,self.posy) #this records the piece that will be taken, if any.
print(takenPiece)
theboard.move(lastposx,lastposy,squarex,squarey)#this moves the piece in the board array(an attribute of the board class)
self.movedyet=True
if takenPiece!='':
allPieces.remove(takenPiece) #if a piece was taken it is removed from the update list.
if self.ptype=='pawn': #if pawn has reached backrow it is upgrade.
if self.colour=='black' and self.posy==7:
print("upgrade")
self.upgradePawn(current_piece,self.colour)
elif self.colour=='white' and self.posy==0:
print("upgrade")
self.upgradePawn(current_piece,self.colour)
movedList=open("movedlist.txt","a+")
movedList.write("\n"+current_piece)
movedList.close()
return False
def distancePerFrame(self,movetox,movetoy):
movetox=movetox*75
movetoy=movetoy*75
currentx=self.posx*75
currenty=self.posy*75
hor=abs(currentx-movetox)
vert=abs(currenty-movetoy)
print("hor: ",hor,"vert: ",vert) #horizontal and vertical distance
distance=math.sqrt((abs(hor))**2+(abs(vert))**2)
print("d:",distance)
speed=210 #unitspersecond
time=distance/speed
print("t: "+str(time)) #time
framestaken=round((time/1)*30)
print("framestaken",framestaken)
xPerFrame=(hor/framestaken)
yPerFrame=(vert/framestaken)
print("x move",xPerFrame,"y move",yPerFrame)
if movetox<currentx:
xPerFrame=-xPerFrame
if movetoy<currenty:
yPerFrame=-yPerFrame
return xPerFrame,yPerFrame
def endangersKing(self,colour,movetox,movetoy):
print("KEY",movetox,movetoy)
board_temp= theboard.currentBoard()
record=board_temp[self.posy][self.posx]
board_temp[self.posy][self.posx]=''
board_temp[movetoy][movetox]=record
# pp=pprint.PrettyPrinter(indent=1,width=100)
# pp.pprint(board_temp)
tempY=0
print(colour)
for y in board_temp:
try:
if colour=='white':
thex=y.index("wking")
they=tempY
print(thex)
else:
thex=y.index("bking")
they=tempY
print(thex)
tempY+=1
except ValueError:
tempY+=1
print(theboard.getpiece(thex,they))
if ( self.minicheck(board_temp,colour,thex,they) ==False) :#mini check finds if
# new move results in king in danger.
# # availableSquares.append(square)
# availableSquarex.append(self.posx)
print("no minicheck")
return False
def minicheck(self,board,colour,x,y):
#diagonal
print(x,y)
diagonal=self.diagonalsclear(board,colour,x,y)
if (diagonal==False):
print("diagonals are danger")
return True
##straights
straights=self.straightsclear(board,colour,x,y)
if straights==False:
print("straights are danger")
return True
##adjacent for king and pawns
adjacent=self.adjacentclear(board,colour,x,y)
if (adjacent==False):
print("adjacents aren't clear")
return True
##knight
knight=self.knightclear(board,colour,x,y)
if (knight==False):
print("knight are danger")
return True
return False
def recursive2(self,board,colour,x,y,currentx,currenty,tempDiagonalDangerPieces):#the other recursive algorithm was being called
if ((currentx+x>7)or(currentx+x<0))or((currenty+y<0)or(currenty+y>7)):#wrong position at first. this error must be checked for first
return True
elif isenemysquare(board,colour,currentx+x,currenty+y)==True:
thepiece=getpiece(board,currentx+x,currenty+y)
try:
if thepiece[1]=='q' or thepiece[1]=='b':
if self.checkmateAlg==False:
for o in tempDiagonalDangerPieces:
dangerPieces.append(o)
dangerPieces.append(thepiece)
return False
else:
return True
except:
return True
elif isemptySquare(board,currentx+x,currenty+y)==False:
return True
else:
print("currentx+x,currenty+y",currentx+x,currenty+y)
tempDiagonalDangerPieces.append([currentx+x,currenty+y])
value=self.recursive2(board,colour,x,y,currentx+x,currenty+y,tempDiagonalDangerPieces)
if value==True:
return True
else:#it wasn't true so it returned false, this was a big problem.
return False
def diagonalsclear(self,board,colour,posx,posy) :
originalx=posx
originaly=posy
for y in range(-1,2):
posx=originalx
posy=originaly
if (y==0):
pass
else:
for x in range(-1,2):
if (x==0):
pass
else:
tempDiagonalDangerPieces=[]
print("x and y",x,y)
returnvalue=self.recursive2(board,colour,x,y,posx,posy,tempDiagonalDangerPieces)
if self.checkmateAlg==False:
if returnvalue==False:
return False
return True
def straightsclear( self,board,colour,posx,posy):
horizontalclear=True
verticalclear= True
originalx=posx
originaly=posy
for x in range(-1,2):
posx=originalx
posy=originaly
if x==0:
pass
else:
if horizontalclear==True:
tempDangerPieces=[]
while posx+x<=7 and posx+x>=0 and posy<=7 and posy>=0 and isemptySquare(board,posx+x,posy)==True and horizontalclear==True:
posx+=x
tempDangerPieces.append([posx,posy])
if isenemysquare(board,colour,posx+x,posy)==True and posx+x>-1 and posx+x<8:
thepiece=getpiece(board,posx+x,posy)
if thepiece[1]=='q' or thepiece[1]=='r':
if self.checkmateAlg ==False:
for o in tempDangerPieces:
dangerPieces.append(o)
dangerPieces.append(thepiece)
if self.checkmateAlg ==False:
horizontalclear=False
else:
horizontalclear=True
for y in range(-1,2):
posx=originalx
posy=originaly
if y==0:#was x ==0
pass
else:
if verticalclear==True:
tempDangerPieces=[]
while posy+y<=7 and posy+y>=0 and posx<=7 and posx>=0 and isemptySquare(board,posx,posy+y)==True and verticalclear==True:
posy+=y
tempDangerPieces.append([posx,posy])
if isenemysquare(board,colour,posx,posy+y)==True and posy+y>-1 and posy+y<8 :
thepiece=getpiece(board,posx,posy+y)
if thepiece[1]=='q' or thepiece[1]=='r':
if self.checkmateAlg==False:
for o in tempDangerPieces:
dangerPieces.append(o)
dangerPieces.append(thepiece)
if self.checkmateAlg ==False:
verticalclear=False
else:
verticalclear=True
if horizontalclear==True and verticalclear==True:
return True
else:
return False
def knightclear(self,board,colour,posx,posy):
long1=[2,-2]
short=[1,-1]
for x in long1:
for y in short:
if posx+x <8 and posx+x >-1 and posy+y<8 and posy+y>-1:
if isenemysquare(board,colour,posx+x,posy+y )==True:
thepiece=getpiece(board,posx+x,posy+y)
if thepiece[1]=='k' and thepiece[2]=='n':
dangerPieces.append(thepiece)
if self.checkmateAlg==False:
return False
for y in long1:
for x in short:
if posx+x <8 and posx+x >-1 and posy+y<8 and posy+y>-1:
if isenemysquare(board,colour,posx+x,posy+y)==True:
thepiece=getpiece(board,posx+x,posy+y)
if thepiece[1]=='k' and thepiece[2]=='n':
dangerPieces.append(thepiece)
if self.checkmateAlg==False:
return False
return True
def adjacentclear(self,board,colour,posx,posy):
for x in range(-1,2):
if posx+x <8 and posx+x >-1 and posy<8 and posy>-1:
if isenemysquare(board,colour,posx+x,posy )==True:
thepiece=getpiece(board,posx+x,posy)
if thepiece[1]=='k' and thepiece[2]=='i':
dangerPieces.append(thepiece)
if self.checkmateAlg==False:
return False
for y in range(-1,2):
if posx <8 and posx >-1 and posy+y<8 and posy+y>-1:
if isenemysquare(board,colour,posx,posy+y)==True:
thepiece=getpiece(board,posx,posy+y)
if thepiece[1]=='k' and thepiece[2]=='i':
dangerPieces.append(thepiece)
if self.checkmateAlg==False:
return False
for y in range(-1,2):
if (y==0):
pass
else:
for x in range(-1,2):
if (x==0):
pass
else:
if colour=='black':
if posx+x<8 and posx+x>-1 and posy+y<8 and posy+y>-1 and isenemysquare(board,colour,posx+x,posy+y)==True:
thepiece=getpiece(board,posx+x,posy+y)
if (thepiece[1]=='p'and posy+y>posy) or (thepiece[1]=='k' and thepiece[2]=='i'):
#may need to solve pawn problem
dangerPieces.append(thepiece)
if self.checkmateAlg==False:
return False
elif colour=='white':
if posx+x<8 and posx+x>-1 and posy+y<8 and posy+y>-1 and isenemysquare(board,colour,posx+x,posy+y)==True:
thepiece=getpiece(board,posx+x,posy+y)
if (thepiece[1]=='p'and posy+y<posy) or (thepiece[1]=='k' and thepiece[2]=='i'):
dangerPieces.append(thepiece)
if self.checkmateAlg==False:
return False
return True
class King(piece):
def __init__(self,ptype,posy,posx,colour):
self.ptype=ptype
self.posy=posy
self.posx=posx
self.moveforw=0
self.moveLorR=0
self.movedyet=False
self.colour=colour
self.checkmateAlg=False
self.checkmateMoves=False
if self.colour=='black':
self.image=pygame.image.load("blackking.png")
chessDisplay.blit(self.image,((self.posx)*75,top))
else:
self.image=pygame.image.load('whiteking.png')
chessDisplay.blit(self.image,((self.posx)*75,bottom))
def get_moves(self,xPerFrame,yPerFrame,turn):
if str(self.colour)==turn:
availableSquarex=[]
availableSquares=[]
posx=self.posx
posy=self.posy
for x in range(-1,2):
for y in range(-1,2):
if posx+x>=0 and posx+x<=7 and posy+y>=0 and posy+y<=7:
if theboard.friendlysquare(posx+x,posy+y,turn)==False:
if self.endangersKing(turn,posx+x,posy+y)==False:#checks if the move endangers the king
availableSquares.append(posy+y)
availableSquarex.append(posx+x)
if availableSquares== []:
SquareToMoveTo=None,None
return SquareToMoveTo,xPerFrame,yPerFrame
else:
if self.checkmateMoves==True:
SquareToMoveTo=availableSquares
return SquareToMoveTo,xPerFrame,yPerFrame
SquareToMoveTo,xPerFrame,yPerFrame=self.showSquares(availableSquares,availableSquarex,xPerFrame,yPerFrame)
return SquareToMoveTo,xPerFrame,yPerFrame
else:
SquareTo=None,None
return SquareTo,xPerFrame,yPerFrame
def checkmate(self,turn):
#check moves of king
dangerPiecesRecord=[]
for items in dangerPieces:
if type(items)=="""<class 'list'>""":
minilist=[]
for z in items:
minilist.append(z)
dangerPiecesRecord.append(minilist)
else:
dangerPiecesRecord.append(items) #creates a duplicate list
for u in range(len(dangerPieces)):
dangerPieces.pop(0)
#dangerPieces.remove(x)
self.checkmateMoves=True#This means that get_moves returns different things depending on whether it is empty.
temporary_board=theboard.currentBoard()
moves=self.get_moves(self.posx,self.posy,turn)
print("yo",dangerPieces)
moves=moves[0]
self.checkmateMoves=False
if moves!=(None, None):
print("King can move")
return False
theThreats=self.threats(turn,temporary_board,dangerPiecesRecord)
if theThreats==False:
return False
return True
def threats(self,turn,board,dangerPiecesRecord):
#danger piece record is a list of pieces currently threatening king and spaces that can be blocked
for item in range(len(dangerPieces)):
dangerPieces.pop(0)
for x in dangerPiecesRecord:
print("dangerPieces record threats",dangerPiecesRecord)
try:
posx,posy=eval(x).coordinates() #This line and the exception are used to get the coordinates of the pieces
#I got the name of the bishop because it made the error checking easier.
except TypeError:
posx,posy=x[0],x[1]
print(posx,posy)
if turn =='black':
tempTurn='white'
else:
tempTurn='black'
self.checkmateAlg=True#This ensures only the pieces are added that can save the king opposed to the empty squares.
print("x run",x)
self.minicheck(board,tempTurn,posx,posy)#This adds to the now empty dangerPieces any pieces that can block or take pieces threatening the king.
self.checkmateAlg=False
print(dangerPieces)
# if self.minicheck(board,tempTurn,posx,posy)==True:
# #Only one piece can be endangering King. The others
# #just block the escape of the King, this is because if the King is ever in check it would have to move out of it.
# print(dangerPieces)
# for x in dangerPieces:
# posx,posy=eval(x).coordinates()
# return False
# print(dangerPieces)
print("basic",dangerPieces)
print("record",dangerPiecesRecord)
dangerPiecesRecord=[]
for items in dangerPieces:
if type(items)=="""<class 'list'>""":
minilist=[]
for z in items:
minilist.append(z)
dangerPiecesRecord.append(minilist)
else:
dangerPiecesRecord.append(items) #tis for loop creates a duplicate list
for item in dangerPiecesRecord: #The duplicate list is used so when getting moves it doesn't change the list
print(item)
eval(item).checkmateMoves=True
moves=eval(item).get_moves(self.posx,self.posy,turn)
print(moves)
if moves[0] !=(None,None): #If none of the saviour pieces(pieces that might be able to save the king) can move
#it means that there is another piece threatening king so it is checkmate if no pieces can save the king.
print("good")
eval(item).checkmateMoves=False
return False
eval(item).checkmateMoves=False
return True
class Queen(piece):
def __init__(self,ptype,posy,posx,colour):
self.ptype=ptype
self.posy=posy
self.posx=posx
self.moveforw=0
self.moveLorR=0
self.movedyet=False
self.colour=colour
self.checkmateAlg=False
self.checkmateMoves=False
if self.colour=='black':
self.image=pygame.image.load("blackqueen.png")
chessDisplay.blit(self.image,((self.posx)*75,top))
else:
self.image=pygame.image.load('whitequeen.png')
chessDisplay.blit(self.image,((self.posx)*75,bottom))
def get_moves(self,xPerFrame,yPerFrame,turn):
if str(self.colour)==turn:
availableSquarex=[]
availableSquares=[]
#diagonals
for y in range(-1,2):
if (y==0):
pass
else:
for x in range(-1,2):
if (x==0):
pass
else:
returnvaluex=[]
returnvaluey=[]
returnvaluex,returnvaluey=self.recursive1(turn,x,y,self.posx,self.posy,returnvaluex,returnvaluey)
print("x",returnvaluex,"y",returnvaluey)
for j in returnvaluex:
availableSquarex.append(j)
for k in returnvaluey:
availableSquares.append(k)
print(availableSquarex,availableSquares)
#straights
for x in range(-1,2):
posx=self.posx
posy=self.posy
if x==0:
pass
else:
while posx+x<=7 and posx+x>=0 and posy<=7 and posy>=0 and theboard.emptySquare(posx+x,posy)==True:
posx+=x
if self.endangersKing(turn,posx,posy)==False:#checks if the move endangers the king
availableSquarex.append(posx)
availableSquares.append(posy)
if theboard.enemysquare(turn,posx+x,posy)==True and posy<=7 and posy>=0 and posx+x<=7 and posx+x>=0:
posx+=x
if self.endangersKing(turn,posx,posy)==False:#checks if the move endangers the king
availableSquarex.append(posx)
availableSquares.append(posy)
print("horizontal done")
print(availableSquarex,availableSquares)
for y in range(-1,2):
posx=self.posx
posy=self.posy
if y==0:
pass
else:
while posy+y<=7 and posy+y>=0 and posx<=7 and posx>=0 and theboard.emptySquare(posx,posy+y)==True :
posy+=y
if self.endangersKing(turn,posx,posy)==False:#checks if the move endangers the king
availableSquarex.append(posx)
availableSquares.append(posy)
if theboard.enemysquare(turn,posx,posy+y)==True and posy+y<=7 and posy+y>=0 and posx<=7 and posx>=0 :
posy+=y
if self.endangersKing(turn,posx,posy)==False:#checks if the move endangers the king
availableSquarex.append(posx)
availableSquares.append(posy)
if availableSquares== []:
SquareToMoveTo=None,None
return SquareToMoveTo,xPerFrame,yPerFrame
else:
if self.checkmateMoves==True:
SquareToMoveTo=availableSquares
return SquareToMoveTo,xPerFrame,yPerFrame
SquareToMoveTo,xPerFrame,yPerFrame=self.showSquares(availableSquares,availableSquarex,xPerFrame,yPerFrame)
return SquareToMoveTo,xPerFrame,yPerFrame
else:
SquareTo=None,None
return SquareTo,xPerFrame,yPerFrame
def recursive1(self,colour,x,y,currentx,currenty,xvalues,yvalues):
if ((currentx+x>7)or(currentx+x<0))or((currenty+y<0)or(currenty+y>7)):#wrong position at first. this error must be checked for first
print("too wide")
return xvalues,yvalues
elif theboard.friendlysquare(currentx+x,currenty+y,colour)==True:
print("friendly")
return xvalues,yvalues
elif theboard.enemysquare(colour,currentx+x,currenty+y)==True:
if self.endangersKing(colour,currentx+x,currenty+y)==False:#checks if the move endangers the king
xvalues.append(currentx+x)
yvalues.append(currenty+y)
return xvalues,yvalues
else:
if self.endangersKing(colour,currentx+x,currenty+y)==False:#checks if the move endangers the king
xvalues.append(currentx+x)
yvalues.append(currenty+y)
xvalues,yvalues=self.recursive1(colour,x,y,currentx+x,currenty+y,xvalues,yvalues)
return xvalues,yvalues
class Knight(piece):
def __init__(self,ptype,posy,posx,colour):
self.ptype=ptype
self.posy=posy
self.posx=posx
self.moveforw=0
self.moveLorR=0
self.movedyet=False
self.colour=colour
self.checkmateAlg=False
self.checkmateMoves=False
if self.colour=='black':
self.image=pygame.image.load("blackknight.png")
chessDisplay.blit(self.image,((self.posx)*75,top))
else:
self.image=pygame.image.load('whiteknight.png')
chessDisplay.blit(self.image,((self.posx)*75,bottom))
def get_moves(self,xPerFrame,yPerFrame,turn):
if str(self.colour)==turn:
availableSquarex=[]
availableSquares=[]
long1=[2,-2]
short=[1,-1]
print("horizontal")
for x in long1:
for y in short:
squarex=self.posx+x
square=self.posy+y
if (theboard.friendlysquare(squarex,square,self.colour))==False and square>-1 :
if self.endangersKing(turn,squarex,square)==False:#checks if the move endangers the king
availableSquares.append(square)
availableSquarex.append(squarex)
print("vertical")
for y in long1:
for x in short:
squarex=self.posx+x
square=self.posy+y
if (theboard.friendlysquare(squarex,square,self.colour))==False and square>-1:
if self.endangersKing(turn,squarex,square)==False:#checks if the move endangers the king
availableSquares.append(square)
availableSquarex.append(squarex)
if availableSquares== []:
SquareToMoveTo=None,None
return SquareToMoveTo,xPerFrame,yPerFrame
else:
if self.checkmateMoves==True:
SquareToMoveTo=availableSquares
return SquareToMoveTo,xPerFrame,yPerFrame
SquareToMoveTo,xPerFrame,yPerFrame=self.showSquares(availableSquares,availableSquarex,xPerFrame,yPerFrame)
return SquareToMoveTo,xPerFrame,yPerFrame
else:
SquareTo=None,None
return SquareTo,xPerFrame,yPerFrame
class Rook(piece):
def __init__(self,ptype,posy,posx,colour):
self.ptype=ptype