-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathChessEngine.py
More file actions
652 lines (566 loc) · 27.7 KB
/
ChessEngine.py
File metadata and controls
652 lines (566 loc) · 27.7 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
import math
import copy
class GameState():
def __init__(self):
# board is 8x8 2D List, each element of the list has 2 characters
# initial character == colour (b,w)
# second character == piece
# R == rook, N == knight, B == bishop, Q == Queen, K == king, P == pawn
# -- == empty space
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.moveFunctions = {'P': self.getPawnMoves, 'R': self.getRookMoves, 'N': self.getKnightMoves,
'B': self.getBishopMoves, 'Q': self.getQueenMoves, 'K': self.getKingMoves}
self.whiteToMove = True
self.AIturn = False
self.moveLog = []
# used in identifying checks and invalid moves
self.wKingLoc = (7,4)
self.bKingLoc = (0,4)
self.checkMate = False
self.stalemate = False
self.enpassantPossible = ()
self.currentCastlingRight = castleRights(True, True, True, True)
self.castleRightsLog = [castleRights(self.currentCastlingRight.wks, self.currentCastlingRight.bks,
self.currentCastlingRight.wqs, self.currentCastlingRight.bqs)]
# makes the move
def makeMove(self, move):
# making the startSq empty
self.board[move.startRow][move.startCol] = '--'
# moving the piece
self.board[move.endRow][move.endCol] = move.pieceMoved
# appending move to the movelog
self.moveLog.append(move)
# changes turn
self.whiteToMove = not self.whiteToMove
# checking if the kings were moved to update there locations
if move.pieceMoved == 'wK':
self.wKingLoc = (move.endRow, move.endCol)
elif move.pieceMoved == 'bK':
self.bKingLoc = (move.endRow, move.endCol)
# promotion
if move.isPawnPromotion:
self.board[move.endRow][move.endCol] = move.pieceMoved[0] + 'Q'
# enpassant
if move.isEnpassantMove:
self.board[move.startRow][move.endCol] = '--'
if move.pieceMoved[1] == 'P' and abs(move.startRow - move.endRow) == 2:
self.enpassantPossible = ((move.startRow + move.endRow) // 2, move.startCol)
else:
self.enpassantPossible = ()
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] = '--'
# update castling rights - whenever king or rook move is played
self.updateCastleRights(move)
self.castleRightsLog.append(castleRights(self.currentCastlingRight.wks, self.currentCastlingRight.bks,
self.currentCastlingRight.wqs, self.currentCastlingRight.bqs))
def undoMove(self):
if len(self.moveLog) != 0:
# removing move form log
move = self.moveLog.pop()
# reversing make move
self.board[move.startRow][move.startCol] = move.pieceMoved
self.board[move.endRow][move.endCol] = move.pieceCaptured
# changing turn
self.whiteToMove = not self.whiteToMove
# checking if the kings were moved to update there locations
if move.pieceMoved == 'wK':
self.wKingLoc = (move.startRow, move.startCol)
elif move.pieceMoved == 'bK':
self.bKingLoc = (move.startRow, move.startCol)
# undoing enpassant move
if move.isEnpassantMove:
self.board[move.endRow][move.endCol] = '--'
self.board[move.startRow][move.endCol] = move.pieceCaptured
self.enpassantPossible = (move.endRow, move.endCol)
# undo a 2 square pawn advance
if move.pieceMoved[1] == 'P' and abs(move.startRow - move.endRow) == 2:
self.enpassantPossible = ()
# undoing 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] = '--'
# undoing castling rights
self.castleRightsLog.pop()
self.currentCastlingRight = self.castleRightsLog[-1]
def updateCastleRights(self, move):
if move.pieceMoved == 'wK':
self.currentCastlingRight.wks = False
self.currentCastlingRight.wqs = False
elif move.pieceMoved == 'bK':
self.currentCastlingRight.bks = False
self.currentCastlingRight.bqs = False
elif move.pieceMoved == 'wR':
if move.startRow == 7:
if move.startCol == 0:
self.currentCastlingRight.wqs = False
elif move.startCol == 7:
self.currentCastlingRight.wks = False
elif move.pieceMoved == 'bR':
if move.startRow == 0:
if move.startCol == 0:
self.currentCastlingRight.bqs = False
elif move.startCol == 7:
self.currentCastlingRight.bks = False
def getValidMoves(self):
tempEnpassantPossible = self.enpassantPossible
tempCastleRights = castleRights(self.currentCastlingRight.wks, self.currentCastlingRight.bks,
self.currentCastlingRight.wqs, self.currentCastlingRight.bqs)
# gets all move
moves = self.getAllMoves()
if self.whiteToMove:
self.getCastleMoves(self.wKingLoc[0], self.wKingLoc[1], moves)
else:
self.getCastleMoves(self.bKingLoc[0], self.bKingLoc[1], moves)
# goes backwords through the list
for i in range(len(moves)-1,-1,-1):
# makes the move and changes turn
self.makeMove(moves[i])
self.whiteToMove = not self.whiteToMove
if self.inCheck():
# sees if that previous move puts the player in check
moves.remove(moves[i])
# changes turn back and undoes the move
self.whiteToMove = not self.whiteToMove
self.undoMove()
# checks if there are no valid moves (either: stalemate or checkmate)
if len(moves) == 0:
# sees if in check or stalemate
if self.inCheck():
self.checkMate = True
else:
self.stalemate = True
# all the valid moves
self.enpassantPossible = tempEnpassantPossible
self.currentCastlingRight = tempCastleRights
return moves
def inCheck(self):
# checks which turn
if self.whiteToMove:
# returns a bool and checks if the whiite king is under attack
return self.squareUnderAttack(self.wKingLoc[0], self.wKingLoc[1])
else:
# then checks black king
return self.squareUnderAttack(self.bKingLoc[0], self.bKingLoc[1])
def squareUnderAttack(self, r, c):
# sees opponent moves by changing turn gets all there moves and changes back turn
self.whiteToMove = not self.whiteToMove
oppMoves = self.getAllMoves()
self.whiteToMove = not self.whiteToMove
# checks all moves and sees if the end square is the square entered in the function
for move in oppMoves:
if move.endRow == r and move.endCol == c:
return True
return False
def getAllMoves(self):
# initialising the move list
moves = []
# going through each element in the list
for r in range(len(self.board)):
for c in range(len(self.board[r])):
# checking piece colour
turn = self.board[r][c][0]
if (turn == 'w' and self.whiteToMove) or (turn == 'b' and not self.whiteToMove):
# using dictionary to reduce if statements
piece = self.board[r][c][1]
self.moveFunctions[piece](r, c, moves)
return moves
def getPawnMoves(self, r, c, moves):
# white pawn moves
if self.whiteToMove:
# checking if square above is empty
if self.board[r-1][c] == '--':
# if it is we append that as a valid move
moves.append(Move((r, c), (r-1, c), self.board))
# checks if the piece hasn't been moved so it can do a double move
if r == 6 and self.board[r-2][c] == '--':
moves.append(Move((r, c), (r-2, c), self.board))
# captures to the left
if c - 1 >= 0:
if self.board[r-1][c-1][0] == 'b':
moves.append(Move((r, c), (r-1, c-1), self.board))
elif (r-1, c-1) == self.enpassantPossible:
moves.append(Move((r, c), (r-1, c-1), self.board, isEnpassantMove = True))
# captures to the right
if c + 1 <= 7:
if self.board[r-1][c+1][0] == 'b':
moves.append(Move((r, c), (r-1, c+1), self.board))
elif (r-1, c+1) == self.enpassantPossible:
moves.append(Move((r, c), (r-1, c+1), self.board, isEnpassantMove = True))
# black pawn moves
else:
if self.board[r + 1][c] == '--':
# checking if square below is empty
moves.append(Move((r, c), (r + 1, c), self.board))
# checks if the piece hasn't been moved so it can do a double move
if r == 1 and self.board[r + 2][c] == '--':
moves.append(Move((r, c), (r+2, c), self.board))
# captures to the left
if c - 1 >= 0:
if self.board[r + 1][c - 1][0] == 'w':
moves.append(Move((r, c), (r + 1, c-1), self.board))
elif (r + 1, c - 1) == self.enpassantPossible:
moves.append(Move((r, c), (r + 1, c - 1), self.board, isEnpassantMove = True))
# captures to the right
if c + 1 <= 7:
if self.board[r + 1][c + 1][0] == 'w':
moves.append(Move((r, c), (r+1, c+1), self.board))
elif (r + 1, c + 1) == self.enpassantPossible:
moves.append(Move((r, c), (r + 1, c + 1), self.board, isEnpassantMove = True))
def getRookMoves(self, r, c, moves):
# directions up, down, left and right(not in that order)
directions = ((-1,0), (0,-1), (1,0), (0,1))
# conditional expression in order to get opponents colour
oppColour = 'b' if self.whiteToMove else 'w'
#goes through each direction
for d in directions:
# loops 8 times length/width of the board as a rook can move 8 squares
for i in range(1,8):
# adds a factor of the direction
endRow = r + d[0] * i
endCol = c + d[1] * i
#checks if the final position is off the board
if 0 <= endRow < 8 and 0 <= endCol < 8:
# checks if it can move to that end square by checking if it is empty
endPiece = self.board[endRow][endCol]
if endPiece == '--':
moves.append(Move((r, c), (endRow, endCol), self.board))
elif endPiece[0] == oppColour:
moves.append(Move((r, c), (endRow, endCol), self.board))
break
else:
break
# breaks if it of the board
else:
break
def getBishopMoves(self, r, c, moves):
# directions the bishop ccan move in (diaganols)
directions = ((-1,-1), (1,-1), (1,1), (-1,1))
# gets opponents colour
oppColour = 'b' if self.whiteToMove else 'w'
# goes through the directions
for d in directions:
# iterates 8 times
for i in range(1,8):
# multiplies the end position by i
endRow = r + d[0] * i
endCol = c + d[1] * i
# checks if the endSq is off the board
if 0 <= endRow < 8 and 0 <= endCol < 8:
endPiece = self.board[endRow][endCol]
# if the endSq is empty it is a valid move
if endPiece == '--':
moves.append(Move((r, c), (endRow, endCol), self.board))
# if there is an opponents piece we can take it then breaks out of that direction
elif endPiece[0] == oppColour:
moves.append(Move((r, c), (endRow, endCol), self.board))
break
# if it is a friendly piece then you can no longer go in that direction so we be break
else:
break
# breaks if it off the board
else:
break
def getQueenMoves(self, r, c, moves):
# can move in all directions so we use the rook and bishop valid move checks
self.getRookMoves(r, c, moves)
self.getBishopMoves(r, c, moves)
def getKnightMoves(self, r, c, moves):
# knight moves
knightMoves = ((-2,-1), (-2,1), (-1,-2), (-1,2), (1,-2), (1,2), (2,-1), (2,1))
# gets ally colour
allyColour = 'w' if self.whiteToMove else 'b'
for m in knightMoves:
endRow = r + m[0]
endCol = c + m[1]
if 0 <= endRow < 8 and 0 <= endCol < 8:
endPiece = self.board[endRow][endCol]
if endPiece[0] != allyColour:
moves.append(Move((r, c), (endRow, endCol), self.board))
def getKingMoves(self, r, c, moves):
# king can only move 1 square but any direction
directions = ((-1,-1), (1,-1), (1,1), (-1,1), (-1,0), (0,-1), (1,0), (0,1))
# easier to check if the
allyColour = 'w' if self.whiteToMove else 'b'
for d in directions:
endRow = r + d[0]
endCol = c + d[1]
# checks if off the board
if 0 <= endRow < 8 and 0 <= endCol < 8:
endPiece = self.board[endRow][endCol]
if endPiece[0] != allyColour:
moves.append(Move((r, c), (endRow, endCol), self.board))
def getCastleMoves(self, r, c, moves):
# cant castle if a aquare a is under attack
if self.squareUnderAttack(r, c):
return
# can move there
if (self.whiteToMove and self.currentCastlingRight.wks) or (not self.whiteToMove and self.currentCastlingRight.bks):
self.getKingsideCastleMoves(r, c, moves)
if (self.whiteToMove and self.currentCastlingRight.wqs) or (not self.whiteToMove and self.currentCastlingRight.bqs):
self.getQueensideCastleMoves(r, c, moves)
def getKingsideCastleMoves(self, r, c, moves):
if self.board[r][c + 1] == '--' and self.board[r][c + 2] == '--':
if not self.squareUnderAttack(r, c + 1) and not self.squareUnderAttack(r, c + 2):
moves.append(Move((r, c), (r, c + 2), self.board, isCastleMove = True))
def getQueensideCastleMoves(self, r, c, moves):
if self.board[r][c - 1] == '--' and self.board[r][c - 2] == '--' and self.board[r][c - 3] == '--':
if not self.squareUnderAttack(r, c - 1) and not self.squareUnderAttack(r, c - 2):
moves.append(Move((r, c), (r, c - 2), self.board, isCastleMove = True))
def minimax(self, depth, alpha, beta, isMaximiser):
possibleMoves = self.getValidMoves()
value = self.boardEval()
if depth == 0 or value == -math.inf or value == math.inf:
return value
if isMaximiser:
maxEval = -math.inf
for move in possibleMoves:
self.makeMove(move)
evaluation = self.minimax(depth - 1, alpha, beta, False)
self.undoMove()
if (evaluation > maxEval):
maxEval = evaluation
alpha = max(alpha, evaluation)
if beta <= alpha:
break
return maxEval
else:
minEval = math.inf
for move in possibleMoves:
self.makeMove(move)
evaluation = self.minimax(depth - 1, alpha, beta, True)
self.undoMove()
minEval = min(evaluation, minEval)
beta = min(evaluation, beta)
if beta <= alpha:
break
return minEval
def getBestMove(self, depth, isMaximiser):
# function to get and play best move for AI
tempCheckmate = copy.deepcopy(self.checkMate)
tempStalemate = copy.deepcopy(self.stalemate)
tempCastle = copy.deepcopy((self.currentCastlingRight.wks, self.currentCastlingRight.bks,
self.currentCastlingRight.wqs, self.currentCastlingRight.bqs))
self.AIturn = True
moves = self.getValidMoves()
bestValue = -math.inf if isMaximiser else math.inf
bestMove = None
for move in moves:
self.makeMove(move)
value = self.minimax(depth - 1, -math.inf, math.inf, not isMaximiser)
self.undoMove()
if value == -math.inf and not isMaximiser:
return move
elif value == math.inf and isMaximiser:
return move
else:
if isMaximiser:
try:
if value > bestValue:
bestValue = value
bestMove = copy.deepcopy(move)
except:
pass
else:
try:
if value < bestValue:
bestValue = value
bestMove = copy.deepcopy(move)
except:
pass
self.AIturn = False
self.checkMate = copy.deepcopy(tempCheckmate)
self.stalemate = copy.deepcopy(tempStalemate)
self.currentCastlingRight = castleRights(tempCastle[0],tempCastle[1],tempCastle[2],tempCastle[3])
return bestMove
def printAllMoveID(self, l):
# testing
for move in l:
print(move.moveID)
v = self.boardEval()
print(v)
def boardEval(self):
moves = self.getValidMoves()
if not self.whiteToMove and self.checkMate:
return math.inf
elif self.whiteToMove and self.checkMate:
return -math.inf
elif self.stalemate:
return 0
#Pawns
wPpieceSquare = [[ 0, 0, 0, 0, 0, 0, 0, 0],
[50, 50, 50, 50, 50, 50, 50, 50],
[10, 10, 20, 30, 30, 20, 10, 10],
[5, 5, 10, 25, 25, 10, 5, 5],
[0, 0, 0, 20, 20, 0, 0, 0],
[5, -5,-10, 0, 0,-10, -5, 5],
[5, 10, 10,-20,-20, 10, 10, 5],
[0, 0, 0, 0, 0, 0, 0, 0]]
#Knights
wNpieceSquare = [[-50,-40,-30,-30,-30,-30,-40,-50],
[-40,-20, 0, 0, 0, 0,-20,-40],
[-30, 0, 10, 15, 15, 10, 0,-30],
[-30, 5, 15, 20, 20, 15, 5,-30],
[-30, 0, 15, 20, 20, 15, 0,-30],
[-30, 5, 10, 15, 15, 10, 5,-30],
[-40,-20, 0, 5, 5, 0,-20,-40],
[-50,-40,-30,-30,-30,-30,-40,-50]]
#Bishops
wBpieceSquare = [[-20,-10,-10,-10,-10,-10,-10,-20],
[-10, 0, 0, 0, 0, 0, 0,-10],
[-10, 0, 5, 10, 10, 5, 0,-10],
[-10, 5, 5, 10, 10, 5, 5,-10],
[-10, 0, 10, 10, 10, 10, 0,-10],
[-10, 10, 10, 10, 10, 10, 10,-10],
[-10, 5, 0, 0, 0, 0, 5,-10],
[-20,-10,-10,-10,-10,-10,-10,-20]]
#Rooks
wRpieceSquare = [[0, 0, 0, 0, 0, 0, 0, 0],
[ 5, 10, 10, 10, 10, 10, 10, 5],
[-5, 0, 0, 0, 0, 0, 0, -5],
[-5, 0, 0, 0, 0, 0, 0, -5],
[-5, 0, 0, 0, 0, 0, 0, -5],
[-5, 0, 0, 0, 0, 0, 0, -5],
[-5, 0, 0, 0, 0, 0, 0, -5],
[ 0, 0, 0, 5, 5, 0, 0, 0]]
#Queens
wQpieceSquare = [[-20,-10,-10, -5, -5,-10,-10,-20],
[-10, 0, 0, 0, 0, 0, 0,-10],
[-10, 0, 5, 5, 5, 5, 0,-10],
[ -5, 0, 5, 5, 5, 5, 0, -5],
[ 0, 0, 5, 5, 5, 5, 0, -5],
[-10, 5, 5, 5, 5, 5, 0,-10],
[-10, 0, 5, 0, 0, 0, 0,-10],
[-20,-10,-10, -5, -5,-10,-10,-20]]
#King
wKpieceSquare = [[-30,-40,-40,-50,-50,-40,-40,-30],
[-30,-40,-40,-50,-50,-40,-40,-30],
[-30,-40,-40,-50,-50,-40,-40,-30],
[-30,-40,-40,-50,-50,-40,-40,-30],
[-20,-30,-30,-40,-40,-30,-30,-20],
[-10,-20,-20,-20,-20,-20,-20,-10],
[ 20, 20, 0, 0, 0, 0, 20, 20],
[ 20, 30, 10, 0, 0, 10, 30, 20]]
bPpieceSquare = self.reversePiece(wPpieceSquare)
bNpieceSquare = self.reversePiece(wNpieceSquare)
bBpieceSquare = self.reversePiece(wBpieceSquare)
bRpieceSquare = self.reversePiece(wRpieceSquare)
bQpieceSquare = self.reversePiece(wQpieceSquare)
bKpieceSquare = self.reversePiece(wKpieceSquare)
values = {'wP': 100, 'wR':500, 'wN':300, 'wB':300, 'wQ':900, 'wK':20000,
'bP': -100, 'bR':-500, 'bN':-300, 'bB':-300, 'bQ':-900, 'bK':-20000}
score = 0
for r in range(len(self.board)):
for c in range(len(self.board[r])):
piece = self.board[r][c]
if piece != '--':
score += values[self.board[r][c]]
#Pawns
if piece == 'wP':
score += wPpieceSquare[r][c]
elif piece == 'bP':
score += bPpieceSquare[r][c]
#Knights
elif piece == 'wN':
score += wNpieceSquare[r][c]
elif piece == 'bN':
score += bNpieceSquare[r][c]
#Bishops
elif piece == 'wB':
score += wBpieceSquare[r][c]
elif piece == 'bB':
score += bBpieceSquare[r][c]
#Rooks
elif piece == 'wR':
score += wRpieceSquare[r][c]
elif piece == 'bR':
score += bRpieceSquare[r][c]
#Queens
elif piece == 'wQ':
score += wQpieceSquare[r][c]
elif piece == 'bQ':
score += bQpieceSquare[r][c]
#Kings
elif piece == 'wK':
score += wKpieceSquare[r][c]
elif piece == 'bK':
score += bKpieceSquare[r][c]
return score
#IN DEVELOPMENT
'''
def moveOrderer(moves):
startOrdedMoves = []
end OrderedMoves = []
oppColour = 'b' if self.whiteToMove else 'w'
for i in range(len(moves)):
cap = moves[i].pieceCaptured
if cap != "--":
if cap == oppColour + "Q":
ordedMoves.append(moves[i])
if cap ==
'''
def reversePiece(self, l):
newList = []
for row in l:
newList.insert(0,row)
for r in range(8):
for c in range(8):
newList[r][c] = newList[r][c] * -1
return newList
class castleRights():
def __init__(self, wks, bks, wqs, bqs):
self.wks = wks
self.bks = bks
self.wqs = wqs
self.bqs = bqs
class Move():
# able to allow chess notation to python array location
ranksToRows = {"1":7,"2":6,"3":5,"4":4,
"5":3,"6":2,"7":1,"8":0}
rowsToRanks = {v: k for k, v in ranksToRows.items()}
filesToCols = {"a":0,"b":1,"c":2,"d":3,
"e":4,"f":5,"g":6,"h":7}
colsToFiles = {v: k for k, v in filesToCols.items()}
def __init__(self, startSq, endSq, board, isEnpassantMove = False, isCastleMove = False):
# start location/square
self.startRow = startSq[0]
self.startCol = startSq[1]
# end location/square
self.endRow = endSq[0]
self.endCol = endSq[1]
# piece moved/captured
self.pieceMoved = board[self.startRow][self.startCol]
self.pieceCaptured = board[self.endRow][self.endCol]
# bool to see if either black or white pawn has been moved to the end row
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
# to compare the moves
self.moveID = self.startRow * 1000 + self.startCol * 100 + self.endRow * 10 + self.endCol
# move class Object Equality
def __eq__(self, other):
if isinstance(other, Move):
return self.moveID == other.moveID
#with use of the fileToRank dictionaries we can print out the move in chess notation (e2e4)
def getChessNot(self):
return self.getRankFile(self.startRow, self.startCol) + self.getRankFile(self.endRow, self.endCol)
def getRankFile(self, r, c):
return self.colsToFiles[c] + self.rowsToRanks[r]