-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessEngine.py
More file actions
995 lines (914 loc) · 39 KB
/
ChessEngine.py
File metadata and controls
995 lines (914 loc) · 39 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
# Storing the information about the current state of a chess game and determine the valid moves at the current state.
import copy
import ZobristHash as hash
class GameState:
def __init__(self):
# The board is an 8x8 2D List, each element of the list has 2 characters.
# The first character represents the color of the piece (b or w)
# The second character represents the type of the piece (King, Queen, Rook, Bishop, Knight, Pawn)
self.board = [
["bR", "bN", "bB", "bQ", "bK", "bB", "bN", "bR"],
["bp", "bp", "bp", "bp", "bp", "bp", "bp", "bp"],
["--", "--", "--", "--", "--", "--", "--", "--"],
["--", "--", "--", "--", "--", "--", "--", "--"],
["--", "--", "--", "--", "--", "--", "--", "--"],
["--", "--", "--", "--", "--", "--", "--", "--"],
["wp", "wp", "wp", "wp", "wp", "wp", "wp", "wp"],
["wR", "wN", "wB", "wQ", "wK", "wB", "wN", "wR"],
]
self.piecesMove = {
"p": self.PawnMoves,
"R": self.RookMoves,
"N": self.KnightMoves,
"B": self.BishopMoves,
"K": self.KingMoves,
"Q": self.QueenMoves,
}
self.pieceValue = {
"bp": -1,
"bR": -5,
"bN": -3,
"bB": -3,
"bQ": -9,
"bK": 0,
"wp": 1,
"wR": 5,
"wN": 3,
"wB": 3,
"wQ": 9,
"wK": 0,
"--": 0,
}
self.zobrist = hash.initTable()
self.whiteTurn = True
self.moveLog = []
self.hashLog = [hash.initHash(self.board, self.zobrist)]
self.whiteKingLocation = (7, 4)
self.blackKingLocation = (0, 4)
self.inCheck = False
self.checkMate = False
self.staleMate = False
self.enpassant = ()
self.enpassantLog = [self.enpassant]
self.pins = []
self.checks = []
self.currentCastleRight = castleRight(True, True, True, True)
self.castleRightLog = [
castleRight(
self.currentCastleRight.wK,
self.currentCastleRight.wQ,
self.currentCastleRight.bK,
self.currentCastleRight.bQ,
)
]
def makeMove(self, move, humanTurn):
self.board[move.startRow][move.startCol] = "--"
self.board[move.endRow][move.endCol] = move.pieceMoved
self.moveLog.append(move) # store history of move
self.hashLog.append(hash.hashMove(move, self.hashLog[-1], self.zobrist))
self.whiteTurn = not self.whiteTurn
# update king's location
if move.pieceMoved == "wK":
self.whiteKingLocation = (move.endRow, move.endCol)
if move.pieceMoved == "bK":
self.blackKingLocation = (move.endRow, move.endCol)
# promotion
if move.isPawnPromotion:
if humanTurn:
promotedPiece = input(
"Enter the piece you want to promote to (Q, R, B, N): "
)
promotedPiece = promotedPiece.upper()
self.board[move.endRow][move.endCol] = (
move.pieceMoved[0] + promotedPiece
)
else:
self.board[move.endRow][move.endCol] = (
move.pieceMoved[0] + move.promotedPiece
)
# Enpassant Move
if move.isEnpassantMove:
self.board[move.startRow][move.endCol] = "--" # capturing the pawn
# enpassant update
if move.pieceMoved[1] == "p" and (
abs(move.startRow - move.endRow) == 2
): # only 2 squares pawn move
self.enpassant = ((move.startRow + move.endRow) // 2, move.endCol)
else:
self.enpassant = () # not en passant move
self.enpassantLog.append(self.enpassant)
# update castling right - whenever king move or rook move
self.updateCastleRight(move)
self.castleRightLog.append(
castleRight(
self.currentCastleRight.wK,
self.currentCastleRight.wQ,
self.currentCastleRight.bK,
self.currentCastleRight.bQ,
)
)
# castle move
if move.isCastleMove:
if move.endCol - move.startCol == 2:
self.board[move.endRow][move.endCol - 1] = self.board[move.endRow][
move.endCol + 1
]
self.board[move.endRow][move.endCol + 1] = "--"
else:
self.board[move.endRow][move.endCol + 1] = self.board[move.endRow][
move.endCol - 2
]
self.board[move.endRow][move.endCol - 2] = "--"
def undoMove(self):
if len(self.moveLog) != 0:
move = self.moveLog.pop()
self.hashLog.pop()
self.board[move.startRow][move.startCol] = move.pieceMoved
self.board[move.endRow][move.endCol] = move.pieceCaptured
self.whiteTurn = not self.whiteTurn
# update king's location
if move.pieceMoved == "wK":
self.whiteKingLocation = (move.startRow, move.startCol)
if move.pieceMoved == "bK":
self.blackKingLocation = (move.startRow, move.startCol)
# undo enpassant move
if move.isEnpassantMove:
self.board[move.endRow][
move.endCol
] = "--" # leave the landing square blank
self.board[move.startRow][move.endCol] = move.pieceCaptured
self.enpassantLog.pop()
self.enpassant = copy.deepcopy(self.enpassantLog[-1])
# undo castling right
self.castleRightLog.pop()
self.currentCastleRight = copy.deepcopy(self.castleRightLog[-1])
# undo castle move
if move.isCastleMove:
if move.endCol - move.startCol == 2:
self.board[move.endRow][move.endCol + 1] = self.board[move.endRow][
move.endCol - 1
]
self.board[move.endRow][move.endCol - 1] = "--"
else:
self.board[move.endRow][move.endCol - 2] = self.board[move.endRow][
move.endCol + 1
]
self.board[move.endRow][move.endCol + 1] = "--"
self.checkMate = False
self.staleMate = False
def updateCastleRight(self, move):
if move.pieceMoved == "wK":
self.currentCastleRight.wK = False
self.currentCastleRight.wQ = False
elif move.pieceMoved == "bK":
self.currentCastleRight.bK = False
self.currentCastleRight.bQ = False
elif move.pieceMoved == "wR":
if move.startRow == 7:
if move.startCol == 0:
self.currentCastleRight.wQ = False
elif move.startCol == 7:
self.currentCastleRight.wK = False
elif move.pieceMoved == "bR":
if move.startRow == 0:
if move.startCol == 0:
self.currentCastleRight.bQ = False
elif move.startCol == 7:
self.currentCastleRight.bK = False
if move.pieceCaptured == "wR":
if move.endRow == 7:
if move.endCol == 0:
self.currentCastleRight.wQ = False
elif move.endCol == 7:
self.currentCastleRight.wK = False
elif move.pieceCaptured == "bR":
if move.endRow == 0:
if move.endCol == 0:
self.currentCastleRight.bQ = False
elif move.endCol == 7:
self.currentCastleRight.bK = False
def getValidMoves(self):
moves = []
self.inCheck, self.pins, self.checks = self.checkForPinsAndChecks()
if self.whiteTurn:
kingRow = self.whiteKingLocation[0]
kingCol = self.whiteKingLocation[1]
else:
kingRow = self.blackKingLocation[0]
kingCol = self.blackKingLocation[1]
if self.inCheck:
if len(self.checks) == 1:
moves = self.getAllPossibleMoves()
check = self.checks[0]
checkRow = check[0]
checkCol = check[1]
pieceChecking = self.board[checkRow][checkCol]
validSqs = []
if pieceChecking[1] == "N":
validSqs = [(checkRow, checkCol)]
else:
for i in range(1, 8):
validSq = (kingRow + check[2] * i, kingCol + check[3] * i)
validSqs.append(validSq)
if validSq[0] == checkRow and validSq[1] == checkCol:
break
for i in range(len(moves) - 1, -1, -1):
if moves[i].pieceMoved[1] != "K":
if not (moves[i].endRow, moves[i].endCol) in validSqs:
moves.remove(moves[i])
else:
self.KingMoves(kingRow, kingCol, moves)
else:
moves = self.getAllPossibleMoves()
if len(moves) == 0:
if self.inCheck:
self.checkMate = True
else:
self.staleMate = True
return moves
def getAllPossibleMoves(self):
moves = []
dimension = len(self.board)
for row in range(dimension):
for col in range(dimension):
turnColor = self.board[row][col][0] # the color of piece
if (turnColor == "w" and self.whiteTurn) or (
turnColor == "b" and not self.whiteTurn
):
piece = self.board[row][col][1]
self.piecesMove[piece](row, col, moves)
return moves
def PawnMoves(self, row, col, moves):
piecePinned = False
pinDirection = ()
for i in range(len(self.pins) - 1, -1, -1):
if self.pins[i][0] == row and self.pins[i][1] == col:
piecePinned = True
pinDirection = (self.pins[i][2], self.pins[i][3])
self.pins.remove(self.pins[i])
break
if self.whiteTurn:
kingRow = self.whiteKingLocation[0]
kingCol = self.whiteKingLocation[1]
else:
kingRow = self.blackKingLocation[0]
kingCol = self.blackKingLocation[1]
if self.whiteTurn:
if self.board[row - 1][col] == "--":
if not piecePinned or pinDirection == (-1, 0):
moves.append(Move((row, col), (row - 1, col), self.board))
if row == 6:
if self.board[4][col] == "--":
moves.append(Move((row, col), (4, col), self.board))
if col - 1 >= 0:
if self.board[row - 1][col - 1][0] == "b":
if not piecePinned or pinDirection == (-1, -1):
moves.append(Move((row, col), (row - 1, col - 1), self.board))
elif (row - 1, col - 1) == self.enpassant:
attackingPiece = blockingPiece = False
if kingRow == row:
if kingCol < col:
insideRange = range(kingCol + 1, col - 1)
outsideRange = range(col + 1, 8)
else:
insideRange = range(kingCol - 1, col, -1)
outsideRange = range(col - 2, -1, -1)
for i in insideRange:
if self.board[row][i] != "--":
blockingPiece = True
for i in outsideRange:
square = self.board[row][i]
if square[0] == "b" and (
square[1] == "R" or square[1] == "Q"
):
attackingPiece = True
elif square != "--":
blockingPiece = True
if not attackingPiece or blockingPiece:
moves.append(
Move(
(row, col),
(row - 1, col - 1),
self.board,
isEnpassantMove=True,
)
)
if col + 1 <= 7:
if self.board[row - 1][col + 1][0] == "b":
if not piecePinned or pinDirection == (-1, 1):
moves.append(Move((row, col), (row - 1, col + 1), self.board))
elif (row - 1, col + 1) == self.enpassant:
attackingPiece = blockingPiece = False
if kingRow == row:
if kingCol < col:
insideRange = range(kingCol + 1, col)
outsideRange = range(col + 2, 8)
else:
insideRange = range(kingCol - 1, col + 1, -1)
outsideRange = range(col - 1, -1, -1)
for i in insideRange:
if self.board[row][i] != "--":
blockingPiece = True
for i in outsideRange:
square = self.board[row][i]
if square[0] == "b" and (
square[1] == "R" or square[1] == "Q"
):
attackingPiece = True
elif square != "--":
blockingPiece = True
if not attackingPiece or blockingPiece:
moves.append(
Move(
(row, col),
(row - 1, col + 1),
self.board,
isEnpassantMove=True,
)
)
else:
if self.board[row + 1][col] == "--":
if not piecePinned or pinDirection == (1, 0):
moves.append(Move((row, col), (row + 1, col), self.board))
if row == 1:
if self.board[3][col] == "--":
moves.append(Move((row, col), (3, col), self.board))
if col - 1 >= 0:
if self.board[row + 1][col - 1][0] == "w":
if not piecePinned or pinDirection == (1, -1):
moves.append(Move((row, col), (row + 1, col - 1), self.board))
elif (row + 1, col - 1) == self.enpassant:
attackingPiece = blockingPiece = False
if kingRow == row:
if kingCol < col:
insideRange = range(kingCol + 1, col - 1)
outsideRange = range(col + 1, 8)
else:
insideRange = range(kingCol - 1, col, -1)
outsideRange = range(col - 2, -1, -1)
for i in insideRange:
if self.board[row][i] != "--":
blockingPiece = True
for i in outsideRange:
square = self.board[row][i]
if square[0] == "w" and (
square[1] == "R" or square[1] == "Q"
):
attackingPiece = True
elif square != "--":
blockingPiece = True
if not attackingPiece or blockingPiece:
moves.append(
Move(
(row, col),
(row + 1, col - 1),
self.board,
isEnpassantMove=True,
)
)
if col + 1 <= 7:
if self.board[row + 1][col + 1][0] == "w":
if not piecePinned or pinDirection == (1, 1):
moves.append(Move((row, col), (row + 1, col + 1), self.board))
elif (row + 1, col + 1) == self.enpassant:
attackingPiece = blockingPiece = False
if kingRow == row:
if kingCol < col:
insideRange = range(kingCol + 1, col)
outsideRange = range(col + 2, 8)
else:
insideRange = range(kingCol - 1, col + 1, -1)
outsideRange = range(col - 1, -1, -1)
for i in insideRange:
if self.board[row][i] != "--":
blockingPiece = True
for i in outsideRange:
square = self.board[row][i]
if square[0] == "w" and (
square[1] == "R" or square[1] == "Q"
):
attackingPiece = True
elif square != "--":
blockingPiece = True
if not attackingPiece or blockingPiece:
moves.append(
Move(
(row, col),
(row + 1, col + 1),
self.board,
isEnpassantMove=True,
)
)
def RookMoves(self, row, col, moves):
piecePinned = False
pinDirection = ()
for i in range(len(self.pins) - 1, -1, -1):
if self.pins[i][0] == row and self.pins[i][1] == col:
piecePinned = True
pinDirection = (self.pins[i][2], self.pins[i][3])
if self.board[row][col][1] != "Q":
self.pins.remove(self.pins[i])
break
straightDirections = ((-1, 0), (1, 0), (0, -1), (0, 1))
enemyColor = "b" if self.whiteTurn else "w"
for d in straightDirections:
for i in range(1, 8):
endRow = row + d[0] * i
endCol = col + d[1] * i
if (0 <= endRow < 8) and (0 <= endCol < 8):
if (
not piecePinned
or pinDirection == d
or pinDirection == (-d[0], -d[1])
):
endPiece = self.board[endRow][endCol]
if endPiece == "--":
moves.append(Move((row, col), (endRow, endCol), self.board))
elif endPiece[0] == enemyColor:
moves.append(Move((row, col), (endRow, endCol), self.board))
break
else:
break
else:
break
def KnightMoves(self, row, col, moves):
piecePinned = False
for i in range(len(self.pins) - 1, -1, -1):
if self.pins[i][0] == row and self.pins[i][1] == col:
piecePinned = True
self.pins.remove(self.pins[i])
break
knightDirections = (
(-2, -1),
(-1, -2),
(1, -2),
(2, -1),
(2, 1),
(1, 2),
(-1, 2),
(-2, 1),
)
allyColor = "w" if self.whiteTurn else "b"
for d in knightDirections:
endRow = row + d[0]
endCol = col + d[1]
if (0 <= endRow < 8) and (0 <= endCol < 8):
if not piecePinned:
endPiece = self.board[endRow][endCol]
if endPiece[0] != allyColor:
moves.append(Move((row, col), (endRow, endCol), self.board))
def BishopMoves(self, row, col, moves):
piecePinned = False
pinDirection = ()
for i in range(len(self.pins) - 1, -1, -1):
if self.pins[i][0] == row and self.pins[i][1] == col:
piecePinned = True
pinDirection = (self.pins[i][2], self.pins[i][3])
self.pins.remove(self.pins[i])
break
diagonalDirections = ((-1, -1), (1, 1), (1, -1), (-1, 1))
enemyColor = "b" if self.whiteTurn else "w"
for d in diagonalDirections:
for i in range(1, 8):
endRow = row + d[0] * i
endCol = col + d[1] * i
if (0 <= endRow < 8) and (0 <= endCol < 8):
if (
not piecePinned
or pinDirection == d
or pinDirection == (-d[0], -d[1])
):
endPiece = self.board[endRow][endCol]
if endPiece == "--":
moves.append(Move((row, col), (endRow, endCol), self.board))
elif endPiece[0] == enemyColor:
moves.append(Move((row, col), (endRow, endCol), self.board))
break
else:
break
else:
break
def QueenMoves(self, row, col, moves):
tempPieces = ["R", "B"]
for char in tempPieces:
self.piecesMove[char](row, col, moves)
def KingMoves(self, row, col, moves):
kingDirections = (
(-1, 0),
(1, 0),
(0, -1),
(0, 1),
(-1, -1),
(1, 1),
(1, -1),
(-1, 1),
)
allyColor = "w" if self.whiteTurn else "b"
for i in range(8):
endRow = row + kingDirections[i][0]
endCol = col + kingDirections[i][1]
if (0 <= endRow < 8) and (0 <= endCol < 8):
endPiece = self.board[endRow][endCol]
if endPiece[0] != allyColor:
# place king on the end of square to check
if allyColor == "w":
self.whiteKingLocation = (endRow, endCol)
else:
self.blackKingLocation = (endRow, endCol)
inCheck, pins, checks = self.checkForPinsAndChecks()
if not inCheck:
moves.append(Move((row, col), (endRow, endCol), self.board))
# place king back
if allyColor == "w":
self.whiteKingLocation = (row, col)
else:
self.blackKingLocation = (row, col)
self.getCastleMoves(row, col, moves, allyColor)
def checkForPinsAndChecks(self):
pins = []
checks = []
inCheck = False
if self.whiteTurn:
enemyColor = "b"
allyColor = "w"
startRow = self.whiteKingLocation[0]
startCol = self.whiteKingLocation[1]
else:
enemyColor = "w"
allyColor = "b"
startRow = self.blackKingLocation[0]
startCol = self.blackKingLocation[1]
directions = (
(-1, 0),
(1, 0),
(0, -1),
(0, 1),
(-1, -1),
(-1, 1),
(1, 1),
(1, -1),
)
for j in range(len(directions)):
d = directions[j]
possiblePins = ()
for i in range(1, 8):
endRow = startRow + d[0] * i
endCol = startCol + d[1] * i
if 0 <= endRow < 8 and 0 <= endCol < 8:
endPiece = self.board[endRow][endCol]
if endPiece[0] == allyColor and endPiece[1] != "K":
if possiblePins == ():
possiblePins = (endRow, endCol, d[0], d[1])
else:
break
elif endPiece[0] == enemyColor:
pieceType = endPiece[1]
if (
(0 <= j <= 3 and pieceType == "R")
or (4 <= j <= 7 and pieceType == "B")
or (
i == 1
and pieceType == "p"
and (
(enemyColor == "b" and 4 <= j <= 5)
or (enemyColor == "w" and 6 <= j <= 7)
)
)
or (pieceType == "Q")
or (i == 1 and pieceType == "K")
):
if possiblePins == ():
inCheck = True
checks.append((endRow, endCol, d[0], d[1]))
break
else:
pins.append(possiblePins)
break
else:
break
else:
break
knightDirections = (
(-2, -1),
(-1, -2),
(1, -2),
(2, -1),
(2, 1),
(1, 2),
(-1, 2),
(-2, 1),
)
for m in knightDirections:
endRow = startRow + m[0]
endCol = startCol + m[1]
if 0 <= endRow < 8 and 0 <= endCol < 8:
endPiece = self.board[endRow][endCol]
if (endPiece[0] == enemyColor) and (endPiece[1] == "N"):
inCheck = True
checks.append((endRow, endCol, m[0], m[1]))
break
return inCheck, pins, checks
def getCastleMoves(self, row, col, moves, allyColor=""):
if self.inCheck:
return # can't castle while be checked
if (self.whiteTurn and self.currentCastleRight.wK) or (
not self.whiteTurn and self.currentCastleRight.bK
):
self.getKingsideCastleMove(row, col, moves, allyColor)
if (self.whiteTurn and self.currentCastleRight.wQ) or (
not self.whiteTurn and self.currentCastleRight.bQ
):
self.getQueensideCastleMove(row, col, moves, allyColor)
def getKingsideCastleMove(self, row, col, moves, allyColor=""):
if self.board[row][col + 1] == "--" and self.board[row][col + 2] == "--":
if allyColor == "w":
self.whiteKingLocation = (row, col + 1)
else:
self.blackKingLocation = (row, col + 1)
inCheck1, pins, checks = self.checkForPinsAndChecks()
if allyColor == "w":
self.whiteKingLocation = (row, col + 2)
else:
self.blackKingLocation = (row, col + 2)
inCheck2, pins, checks = self.checkForPinsAndChecks()
if allyColor == "w":
self.whiteKingLocation = (row, col)
else:
self.blackKingLocation = (row, col)
if not inCheck1 and not inCheck2:
moves.append(
Move((row, col), (row, col + 2), self.board, isCastleMove=True)
)
def getQueensideCastleMove(self, row, col, moves, allyColor=""):
if (
self.board[row][col - 1] == "--"
and self.board[row][col - 2] == "--"
and self.board[row][col - 3] == "--"
):
if allyColor == "w":
self.whiteKingLocation = (row, col - 1)
else:
self.blackKingLocation = (row, col - 1)
inCheck1, pins, checks = self.checkForPinsAndChecks()
if allyColor == "w":
self.whiteKingLocation = (row, col - 2)
else:
self.blackKingLocation = (row, col - 2)
inCheck2, pins, checks = self.checkForPinsAndChecks()
# if allyColor == 'w':
# self.whiteKingLocation = (row, col - 3)
# else:
# self.blackKingLocation = (row, col - 3)
# inCheck3, pins, checks = self.checkForPinsAndChecks()
if allyColor == "w":
self.whiteKingLocation = (row, col)
else:
self.blackKingLocation = (row, col)
if not inCheck1 and not inCheck2:
moves.append(
Move((row, col), (row, col - 2), self.board, isCastleMove=True)
)
def evaluate(self):
# Check for repitition draws
if self.isRepitition():
return 0
score = 0
b_pawns = [0, 0, 0, 0, 0, 0, 0, 0]
w_pawns = [0, 0, 0, 0, 0, 0, 0, 0]
development = 0
space = 0
b_safety = 0
w_safety = 0
b_queen_alive = False
w_queen_alive = False
for row, row_list in enumerate(self.board):
for col, piece in enumerate(row_list):
# piece evaluation
score += self.pieceValue[piece]
# count queens alive:
if piece == "wQ":
w_queen_alive = True
if piece == "bQ":
b_queen_alive = True
if piece == "wp":
space += (7 - row) / 2
if row == 1:
space += 10
w_pawns[col] += 1
# centre
if row in range(2, 6) and col in range(3, 5):
score += 0.2
# pawn structure
if (
self.board[max(0, row - 1)][max(0, col - 1)] == "wp"
or self.board[max(0, row - 1)][min(7, col + 1)] == "wp"
):
score += 0.02
elif piece == "bp":
space -= row / 2
if row == 6:
space -= 10
b_pawns[col] += 1
# centre
if row in range(2, 6) and col in range(3, 5):
score -= 0.2
# pawn structure
if (
self.board[min(7, row + 1)][max(0, col - 1)] == "bp"
or self.board[min(7, row + 1)][min(7, col + 1)] == "bp"
):
score -= 0.02
else:
# Prevent early queen moves
if (
piece[1] == "Q"
and len(self.moveLog) < 12
and row in (2, 3, 4, 5)
):
if piece[0] == "w":
development -= 0.25
elif piece[0] == "b":
development += 0.25
# development
if piece[1] != "K" and piece[1] != "R":
if piece[0] == "w" and row < 6:
development += 0.15
elif piece[0] == "b" and row > 1:
development -= 0.15
elif piece[1] == "R":
if piece[0] == "w" and col in range(3, 7):
development += 0.15
elif piece[0] == "b" and col in range(3, 7):
development -= 0.15
elif piece[1] == "K":
if piece[0] == "w" and col in (0, 1, 2, 6, 7):
w_safety += 0.4
# King safety
castling_side_list = [(0, 1, 2), (5, 6, 7)]
# king_pawns[a,b], where a is one square away from king, b is two squares away
for castling_side in castling_side_list:
if col in castling_side:
king_pawns = [0, 0]
for i in castling_side:
if self.board[6][i] == "wp":
king_pawns[0] += 1
if self.board[5][i] == "wp":
king_pawns[1] += 1
# evaluate safety
if king_pawns[0] < 2:
if king_pawns[0] == 0:
w_safety -= 0.5
elif king_pawns[1] == 2:
w_safety -= 0.05
elif king_pawns[1] == 1:
w_safety -= 0.1
elif king_pawns[1] == 0:
w_safety -= 0.3
elif piece[0] == "b" and col in (0, 1, 2, 6, 7):
b_safety += 0.4
# King safety
castling_side_list = [(0, 1, 2), (5, 6, 7)]
# king_pawns[a,b], where a is one square away from king, b is two squares away
for castling_side in castling_side_list:
if col in castling_side:
king_pawns = [0, 0]
for i in castling_side:
if self.board[1][i] == "bp":
king_pawns[0] += 1
if self.board[2][i] == "bp":
king_pawns[1] += 1
# evaluate safety
if king_pawns[0] < 2:
if king_pawns[0] == 0:
b_safety -= 0.5
elif king_pawns[1] == 2:
b_safety -= 0.05
elif king_pawns[1] == 1:
b_safety -= 0.1
elif king_pawns[1] == 0:
b_safety -= 0.3
# Knight on edge case
if piece[1] == "N":
if piece[0] == "w" and col in (0, 7):
development -= 0.05
elif piece[0] == "b" and col in (0, 7):
development += 0.05
# Check doubled pawns
for stacked_pawns in w_pawns:
score -= max(0, stacked_pawns - 1) * 0.5
for stacked_pawns in b_pawns:
score += max(0, stacked_pawns - 1) * 0.5
# Check passed pawns:
for i in range(8):
if (
w_pawns[i] > 0
and b_pawns[max(i - 1, 0)] == 0
and b_pawns[max(i, 0)] == 0
and b_pawns[min(i + 1, 7)] == 0
):
score += 0.3
if (
b_pawns[i] > 0
and w_pawns[max(i - 1, 0)] == 0
and w_pawns[max(i, 0)] == 0
and w_pawns[min(i + 1, 7)] == 0
):
score -= 0.3
# Add developement and space
score += development + 0.05 * space
# Add king safety
if b_queen_alive:
score += w_safety
if w_queen_alive:
score -= b_safety
return score
# returns number of pieces and pawns, (pieces valued higher)
def piece_value_total(self):
total = 0
for row_list in self.board:
for piece in row_list:
total += abs(self.pieceValue[piece])
return total
def isRepitition(self):
latestState = self.hashLog[-1]
repitition = 1
for i in range(2, len(self.moveLog) + 1):
if (
self.moveLog[-i].pieceMoved == "wp"
or self.moveLog[-i].pieceMoved == "bp"
):
return False
if self.hashLog[-i] == latestState:
repitition += 1
if repitition == 3:
return True
return False
class Move:
# row start from 1->8 (bottom left to top left)
rankToRow = {"1": 7, "2": 6, "3": 5, "4": 4, "5": 3, "6": 2, "7": 1, "8": 0}
rowToRank = {v: k for k, v in rankToRow.items()}
# col start from a->h (bottom left to bottom right)
alphaToCol = {"a": 0, "b": 1, "c": 2, "d": 3, "e": 4, "f": 5, "g": 6, "h": 7}
colToAlpha = {v: k for k, v in alphaToCol.items()}
def __init__(
self,
startSq,
endSq,
board,
isEnpassantMove=False,
isCastleMove=False,
promotedPiece="",
):
self.startRow = startSq[0]
self.startCol = startSq[1]
self.endRow = endSq[0]
self.endCol = endSq[1]
self.pieceMoved = board[self.startRow][self.startCol]
self.pieceCaptured = board[self.endRow][self.endCol]
self.isPawnPromotion = (self.pieceMoved == "wp" and self.endRow == 0) or (
self.pieceMoved == "bp" and self.endRow == 7
)
self.isEnpassantMove = isEnpassantMove
if self.isEnpassantMove:
self.pieceCaptured = "wp" if self.pieceMoved == "bp" else "bp"
self.isCastleMove = isCastleMove
self.promotedPiece = promotedPiece
self.isCapture = self.pieceCaptured != "--"
self.moveId = (
self.startCol * 1000 + self.startRow * 100 + self.endCol * 10 + self.endRow
)
# override function
def __eq__(self, other):
if isinstance(other, Move):
return self.moveId == other.moveId
return False
# printing to console the move from xx to yy
def getChessNotation(self):
return self.getFileRank(self.startRow, self.startCol) + self.getFileRank(
self.endRow, self.endCol
)
def getFileRank(self, row, col):
return str(self.colToAlpha[col] + self.rowToRank[row])
# override function
def __str__(self):
# castleMove:
if self.isCastleMove:
return "O-O" if self.endCol == 6 else "O-O-O"
endSquare = self.getFileRank(self.endRow, self.endCol)
# pawn move
if self.pieceMoved[1] == "p":
if self.isCapture:
return self.colToAlpha[self.startCol] + "x" + endSquare
else:
return endSquare
# piece moves
moveString = self.pieceMoved[1]
if self.isCapture:
moveString += "x"
return moveString + endSquare
class castleRight:
def __init__(self, wK, wQ, bK, bQ):
self.wK = wK
self.bK = bK
self.wQ = wQ
self.bQ = bQ