-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathChessEngine.py
More file actions
604 lines (556 loc) · 28.4 KB
/
ChessEngine.py
File metadata and controls
604 lines (556 loc) · 28.4 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
"""
This class is responsible for storing all the information about the current state of a chess game. It will also be
responsible for determining the valid moves at the current state. It will also keep a move log.
"""
class GameState():
def __init__(self):
# 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, 'K', 'Q', 'R', 'B', 'N', or 'P'
# "--" - represents an empty space with no piece.
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.moveLog = []
self.whiteToMove = True
self.whiteKingLocation = (7, 4)
self.blackKingLocation = (0, 4)
self.inCheck = False
self.pins = []
self.checks = []
self.checkMate = False
self.staleMate = False
self.enPassantPossible = () #square where en passant capture can happen
#castling rights
self.whiteCastleKingside = True
self.whiteCastleQueenside = True
self.blackCastleKingside = True
self.blackCastleQueenside = True
self.castleRightsLog = [(CastleRights(self.whiteCastleKingside, self.blackCastleKingside, self.whiteCastleQueenside, self.blackCastleQueenside))]
'''
Make the move that is passed as a parameter
'''
def makeMove(self, move):
self.board[move.endRow][move.endCol] = move.pieceMoved
self.board[move.startRow][move.startCol] = "--"
self.moveLog.append(move) #log move
self.whiteToMove = not self.whiteToMove #swap players
#update the kings location if moved
if move.pieceMoved == "wK":
self.whiteKingLocation = (move.endRow, move.endCol)
elif move.pieceMoved == "bK":
self.blackKingLocation = (move.endRow, move.endCol)
#if pawn moves twice, next move can capture enpassant
if move.pieceMoved[1] == 'p' and abs(move.startRow - move.endRow) == 2:
self.enPassantPossible = ((move.endRow + move.startRow)//2, move.endCol)
else:
self.enPassantPossible = ()
#if en passant move must update the board to capture the pawn
if move.enPassant:
self.board[move.startRow][move.endCol] = "--"
#if pawn promotion change piece
if move.pawnPromotion:
run = True
import pygame as p
while run:
for e in p.event.get():
if (e.type == p.KEYDOWN):
if (e.key == p.K_q):
self.board[move.endRow][move.endCol] = move.pieceMoved[0] + 'Q'
run = False
elif (e.key == p.K_r):
self.board[move.endRow][move.endCol] = move.pieceMoved[0] + 'R'
run = False
elif (e.key == p.K_b):
self.board[move.endRow][move.endCol] = move.pieceMoved[0] + 'B'
run = False
elif (e.key == p.K_n):
self.board[move.endRow][move.endCol] = move.pieceMoved[0] + 'N'
run = False
#promotedPiece = input("Promote to Q, R, B, or N:") #we can make this part of the UI later
#self.board[move.endRow][move.endCol] = move.pieceMoved[0] + promotedPiece
#update castling rights
self.updateCastleRights(move)
self.castleRightsLog.append(CastleRights(self.whiteCastleKingside, self.blackCastleKingside, self.whiteCastleQueenside, self.blackCastleQueenside))
#castle moves
if move.castle:
if move.endCol - move.startCol == 2: # king side
self.board[move.endRow][move.endCol - 1] = self.board[move.endRow][move.endCol + 1] # move rook
self.board[move.endRow][move.endCol + 1] = "--" #empty space where rook was
else: #queenside
self.board[move.endRow][move.endCol + 1] = self.board[move.endRow][move.endCol - 2] # move rook
self.board[move.endRow][move.endCol - 2] = "--" # empty space where rook was
'''
Undo the last move made
'''
def undoMove(self):
if len(self.moveLog) != 0: # make sure that there is a move to undo
move = self.moveLog.pop()
self.board[move.startRow][move.startCol] = move.pieceMoved #put piece on starting square
self.board[move.endRow][move.endCol] = move.pieceCaptured #put back captured piece
self.whiteToMove = not self.whiteToMove # switch turns back
# update the kings position if needed
if move.pieceMoved == "wK":
self.whiteKingLocation = (move.startRow, move.startCol)
elif move.pieceMoved == "bK":
self.blackKingLocation = (move.startRow, move.startCol)
#undo enpassant is different
if move.enPassant:
self.board[move.endRow][move.endCol] = "--" #removes the pawn that was added in the wrong square
self.board[move.startRow][move.endCol] = move.pieceCaptured #puts the pawn back on the correct square it was captured on
self.enPassantPossible = (move.endRow, move.endCol) # allow an en passant to happen on the enxt move
#undo a 2 square pawn advance should make enPassantPossible = () again
if move.pieceMoved[1] == "p" and abs(move.startRow - move.endRow) == 2:
self.enPassantPossible = ()
#give back castle rights if move took them away
self.castleRightsLog.pop() #remove last move updates
castleRights = self.castleRightsLog[-1]
self.whiteCastleKingside = castleRights.wks
self.blackCastleKingside = castleRights.bks
self.whiteCastleQueenside = castleRights.wqs
self.blackCastleQueenside = castleRights.bqs
#undo castle
if move.castle:
if move.endCol - move.startCol == 2: #king side
self.board[move.endRow][move.endCol + 1] = self.board[move.endRow][move.endCol - 1] # move rook
self.board[move.endRow][move.endCol - 1] = "--" # empty space where rook was
else: #queenside
self.board[move.endRow][move.endCol - 2] = self.board[move.endRow][move.endCol + 1] # move rook
self.board[move.endRow][move.endCol + 1] = "--" # empty space where rook was
'''
All moves with considering checks
'''
def getValidMoves(self):
moves = []
self.inCheck, self.pins, self.checks = self.checkForPinsAndChecks()
if self.whiteToMove:
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: #only 1 check, block check or move king
moves = self.getAllPossibleMoves()
#to block a check you must move a piece into one of the squares between the enemy piece and king
check = self.checks[0] #check information
checkRow = check[0]
checkCol = check[1]
pieceChecking = self.board[checkRow][checkCol] #enemy piece causing the check
validSquares = [] #squares that pieces can move to
#if knight, must capture knight or move king, other pieces can be blocked
if pieceChecking[1] == 'N':
validSquares = [(checkRow, checkCol)]
else:
for i in range(1, 8):
validSquare = (kingRow + check[2] * i, kingCol + check[3] * i) #check [2] and check [3] are the check directions
validSquares.append(validSquare)
if validSquare[0] == checkRow and validSquare[1] == checkCol: #once you get to piece end checks
break
#get rid of any moves that don't block check or move king
for i in range(len(moves) - 1, -1, -1): #go through backwards when you are removing from a list as iterating
if moves[i].pieceMoved[1] != 'K': #move doesn't move king so it must block or capture
if not (moves[i].endRow, moves[i].endCol) in validSquares: #move doesn't block check or capture piece
moves.remove(moves[i])
else: #double check, king has to move
self.getKingMoves(kingRow, kingCol, moves)
else: #not in check so all moves are fine
moves = self.getAllPossibleMoves()
if len(moves) == 0:
if self.inCheck:
self.checkMate = True
else:
self.staleMate = True
else:
self.checkMate = False
self.staleMate = False
return moves
'''
All moves without considering checks
'''
def getAllPossibleMoves(self):
moves = []
for r in range(len(self.board)):
for c in range(len(self.board[r])):
turn = self.board[r][c][0]
if (turn == "w" and self.whiteToMove) or (turn == "b" and not self.whiteToMove):
piece = self.board[r][c][1]
self.moveFunctions[piece](r, c, moves)
return moves
'''
Get pawn moves given starting row and column, append the new moves to the list 'moves'
'''
def getPawnMoves(self, r, c, moves):
piecePinned = False
pinDirection = ()
for i in range(len(self.pins)-1, -1, -1):
if self.pins[i][0] == r and self.pins[i][1] == c:
piecePinned = True
pinDirection = (self.pins[i][2], self.pins[i][3])
self.pins.remove(self.pins[i])
break
if self.whiteToMove:
moveAmount = -1
startRow = 6
backRow = 0
enemyColor = "b"
else:
moveAmount = 1
startRow = 1
backRow = 7
enemyColor = "w"
pawnPromotion = False
if self.board[r+moveAmount][c] == "--": # 1 square move
if not piecePinned or pinDirection == (moveAmount, 0):
if r + moveAmount == backRow: #if piece gets to back rank then it is a pawn promotion
pawnPromotion = True
moves.append(Move((r, c), (r+moveAmount, c), self.board, pawnPromotion = pawnPromotion))
if r == startRow and self.board[r+2*moveAmount][c] == "--": # 2 square moves
moves.append(Move((r, c), (r +2*moveAmount, c), self.board))
if c - 1 >= 0: # captures to the left
if not piecePinned or pinDirection == (moveAmount, -1):
if self.board[r +moveAmount][c - 1][0] == enemyColor:
if r+moveAmount == backRow: # if piece gets to back rank then it is a pawn promotion
pawnPromotion = True
moves.append(Move((r,c), (r+moveAmount, c-1), self.board, pawnPromotion=pawnPromotion))
if (r+moveAmount, c-1) == self.enPassantPossible:
moves.append(Move((r, c), (r+moveAmount, c-1), self.board, enPassant=True))
if c + 1 <= 7: # captures to the right
if not piecePinned or pinDirection == (moveAmount, 1):
if self.board[r +moveAmount][c + 1][0] == enemyColor:
if r+moveAmount == backRow: # if piece gets to back rank then it is a pawn promotion
pawnPromotion = True
moves.append(Move((r,c), (r+moveAmount, c+1), self.board, pawnPromotion=pawnPromotion))
if (r+moveAmount, c+1) == self.enPassantPossible:
moves.append(Move((r, c), (r+moveAmount, c+1), self.board, enPassant=True))
"""
Get rook moves given starting row and column, append the new moves to the list 'moves'
"""
def getRookMoves(self, r, c, moves):
piecePinned = False
pinDirection = ()
for i in range(len(self.pins)-1, -1, -1):
if self.pins[i][0] == r and self.pins[i][1] == c:
piecePinned = True
pinDirection = (self.pins[i][2], self.pins[i][3])
if self.board[r][c][1] != 'Q': #can't remove queen from pin on rook moves, only remove it on bishop moves
self.pins.remove(self.pins[i])
break
directions = ((-1, 0), (0, -1), (1, 0), (0, 1)) # up down left right
enemyColor = "b" if self.whiteToMove else "w"
for d in directions:
for i in range(1, 8):
endRow = r + d[0] * i
endCol = c + d[1] * i
if 0 <= endRow < 8 and 0 <= endCol < 8: # on board
if not piecePinned or pinDirection == d or pinDirection == (-d[0], -d[1]):
endPiece = self.board[endRow][endCol]
if endPiece == "--": # empty space valid
moves.append(Move((r, c), (endRow, endCol), self.board))
elif endPiece[0] == enemyColor: # enemy piece valid
moves.append(Move((r, c), (endRow, endCol), self.board))
break
else: # friendly piece invalid
break
else: # off board
break
"""
Get knight moves given starting row and column, append the new moves to the list 'moves'
"""
def getKnightMoves(self, r, c, moves):
piecePinned = False
for i in range(len(self.pins)-1, -1, -1):
if self.pins[i][0] == r and self.pins[i][1] == c:
piecePinned = True
self.pins.remove(self.pins[i])
break
knightMoves = ((-2, -1), (-2, 1), (-1, -2), (-1, 2), (1, -2), (1, 2), (2, -1), (2, 1))
allyColor = "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: # on board
if not piecePinned:
endPiece = self.board[endRow][endCol]
if endPiece[0] != allyColor: # not an ally piece (empty or enemy piece)
moves.append(Move((r, c), (endRow, endCol), self.board))
"""
Get bishop moves given starting row and column, append the new moves to the list 'moves'
"""
def getBishopMoves(self, r, c, moves):
piecePinned = False
pinDirection = ()
for i in range(len(self.pins)-1, -1, -1):
if self.pins[i][0] == r and self.pins[i][1] == c:
piecePinned = True
pinDirection = (self.pins[i][2], self.pins[i][3])
self.pins.remove(self.pins[i])
break
directions = ((-1, -1), (-1, 1), (1, -1), (1, 1))
enemyColor = "b" if self.whiteToMove else "w"
for d in directions:
for i in range(1, 8): # bishop can move max of 7 squares
endRow = r + d[0] * i
endCol = c + d[1] * i
if 0 <= endRow < 8 and 0 <= endCol < 8: # it is on board
if not piecePinned or pinDirection == d or pinDirection == (-d[0], -d[1]):
endPiece = self.board[endRow][endCol]
if endPiece == "--": # empty space valid
moves.append(Move((r, c), (endRow, endCol), self.board))
elif endPiece[0] == enemyColor: # enemy piece valid
moves.append(Move((r, c), (endRow, endCol), self.board))
break
else: # friendly piece invalid
break
else: # off board
break
"""
Get queen moves given starting row and column, append the mew moves to the list 'moves'
"""
def getQueenMoves(self, r, c, moves):
self.getRookMoves(r, c, moves)
self.getBishopMoves(r, c, moves)
"""
Get king moves given starting row and column, append the mew moves to the list 'moves'
"""
def getKingMoves(self, r, c, moves):
rowMoves = (-1, -1, -1, 0, 0, 1, 1, 1)
colMoves = (-1, 0, 1, -1, 1, -1, 0, 1)
allyColor = "w" if self.whiteToMove else "b"
for i in range(8):
endRow = r + rowMoves[i]
endCol = c + colMoves[i]
if 0 <= endRow < 8 and 0 <= endCol < 8:
endPiece = self.board[endRow][endCol]
if endPiece[0] != allyColor: # not an ally piece (empty or enemy piece)
# place king on end square and check for checks
if allyColor == "w":
self.whiteKingLocation = (endRow, endCol)
else:
self.blackKingLocation = (endRow, endCol)
inCheck, pins, checks = self.checkForPinsAndChecks()
if not inCheck:
moves.append(Move((r, c), (endRow, endCol), self.board))
#place king back on original location
if allyColor == "w":
self.whiteKingLocation = (r, c)
else:
self.blackKingLocation = (r, c)
self.getCastleMoves(r, c, moves, allyColor)
'''
Generate castle moves for the king at (r,c) and add them to the list of moves
'''
def getCastleMoves(self, r, c, moves, allyColor):
inCheck = self.squareUnderAttack (r, c, allyColor)
if inCheck:
print("oof")
return #cant castle in check
if (self.whiteToMove and self.whiteCastleKingside) or (not self.whiteToMove and self.blackCastleKingside): #can't castle if given up rights
self.getKingsideCastleMoves(r, c, moves, allyColor)
if (self.whiteToMove and self.whiteCastleQueenside) or (not self.whiteToMove and self.blackCastleQueenside):
self.getQueensideCastleMoves(r, c, moves, allyColor)
'''
Generate kingside castle moves for the king at (r, c). This method will only be called if player still has castle rights kingside
'''
def getKingsideCastleMoves(self, r, c, moves, allyColor):
#check if two square between king and rook are clear and not under attack
if self.board[r][c+1] == "--" and self.board[r][c+2] == "--" and \
not self.squareUnderAttack(r,c+1, allyColor) and not self.squareUnderAttack(r, c+2, allyColor):
moves.append(Move((r, c), (r, c+2), self.board, castle=True))
'''
Generate queenside castle moves for the king at (r, c). This method will only be called if player still has castle rights queenside
'''
def getQueensideCastleMoves(self, r, c, moves, allyColor):
#check if three square between king and rook are clear and not under attack
if self.board[r][c-1] == "--" and self.board[r][c-2] == "--" and self.board[r][c-3] == "--" and \
not self.squareUnderAttack(r,c-1, allyColor) and not self.squareUnderAttack(r, c-2, allyColor):
moves.append(Move((r, c), (r, c-2), self.board, castle=True))
'''
Returns if the square is under attack
'''
def squareUnderAttack(self, r, c, allyColor):
#check outward from square
enemyColor = "w" if allyColor == "b" else "b"
directions = ((-1, 0), (0, -1), (1, 0), (0, 1), (-1, -1), (-1, 1), (1, -1), (1, 1))
for j in range(len(directions)):
d = directions[j]
for i in range(1, 8):
endRow = r + d[0] * i
endCol = c + d[1] * i
if 0 <= endRow < 8 and 0 <= endCol < 8:
endPiece = self.board[endRow][endCol]
if endPiece[0] == allyColor and endPiece[1] != 'K':
break
elif endPiece[0] == enemyColor:
type = endPiece[1]
#--------------------------------------------------------------
if (0 <= j <= 3 and type == 'R') or \
(4 <= j <= 7 and type == 'B') or \
(i == 1 and type == 'p' and (
(enemyColor == 'w' and 6 <= j <= 7) or (enemyColor == 'b' and 4 <= j <= 5))) or \
(type == 'Q') or (i == 1 and type == 'K'):
return True
else: # enemy piece not applying check:
break
else:
break # off board
# check for knight checks
knightMoves = ((-2, -1), (-2, 1), (-1, -2), (-1, 2,), (1, -2), (1, 2), (2, -1), (2, 1))
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] == enemyColor and endPiece[1] == 'N': # enemy knight attacking king
return True
return False
'''
Returns if the player is in check, a list of pins, and a list of checks
'''
def checkForPinsAndChecks(self):
pins = [] #squares where the allied pinned piece is and direction pinned from
checks = [] #squares where enemy is applying a check
inCheck = False
if self.whiteToMove:
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]
#check outward from king for pins and checks, keep track of pins
directions = ((-1, 0), (0, -1), (1, 0), (0, 1), (-1, -1), (-1, 1), (1, -1), (1, 1))
for j in range(len(directions)):
d = directions[j]
possiblePin = () #reset possible pins
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 possiblePin == (): #1st allied piece could be pinned
possiblePin = (endRow, endCol, d[0], d[1])
else: #2nd allied piece, so no pin or check possible in this direction
break
elif endPiece[0] == enemyColor:
type = endPiece[1]
#5 possibilities here in this complex conditional
#1.) orthogonally away from king and piece is a rook
#2.) diagonally away from king and piece is a bishop
#3.) 1 square away diagonally from king and piece is a pawn
#4.) any direction and piece is a queen
#5.) any direction 1 square away and piece is a king (this is necessary to prevent a king move to a square controlled by another king)
if (0 <= j <= 3 and type == 'R') or \
(4 <= j <= 7 and type == 'B') or \
(i == 1 and type == 'p' and ((enemyColor == 'w' and 6 <= j <= 7) or (enemyColor == 'b' and 4 <= j <= 5))) or \
(type == 'Q') or (i == 1 and type == 'K'):
if possiblePin == (): #no piece blocking, so check
inCheck = True
checks.append((endRow, endCol, d[0], d[1]))
break
else: #piece blocking so pin
pins.append(possiblePin)
break
else: #enemy piece not applying check:
break
else:
break #off board
#check for knight checks
knightMoves = ((-2, -1), (-2, 1), (-1, -2), (-1, 2,), (1, -2), (1, 2), (2, -1), (2, 1))
for m in knightMoves:
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': #enemy knight attacking king
inCheck = True
checks.append((endRow, endCol, m[0], m[1]))
return inCheck, pins, checks
def updateCastleRights(self,move):
if move.pieceMoved == "wK":
self.whiteCastleQueenside = False
self.whiteCastleKingside = False
elif move.pieceMoved == "bK":
self.blackCastleQueenside = False
self.blackCastleKingside = False
elif move.pieceMoved == "wR":
if move.startRow == 7:
if move.startCol == 7:
self.whiteCastleKingside = False
elif move.startCol == 0:
self.whiteCastleQueenside = False
elif move.pieceMoved == "bR":
if move.startRow == 0:
if move.startCol == 7:
self.blackCastleKingside = False
elif move.startCol == 0 :
self.blackCastleQueenside = False
class CastleRights():
def __init__(self, wks, bks, wqs, bqs):
self.wks = wks
self.bks = bks
self.wqs = wqs
self.bqs = bqs
class Move():
# map keys to values
# key : values
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, enPassant = False, pawnPromotion = False, castle = False):
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.enPassant = enPassant
self.pawnPromotion = pawnPromotion
self.castle = castle
if enPassant:
self.pieceCaptured = "bp" if self.pieceMoved == "wp" else "wp" #enpassant captures opposite coloured pawn
self.isCapture = self.pieceCaptured != "--"
self.moveID = self.startRow * 1000 + self.startCol * 100 + self.endRow * 10 + self.endCol
'''
Overriding the equals method
'''
def __eq__(self, other):
if isinstance(other, Move):
return self.moveID == other.moveID
return False
def getChessNotation(self):
# you can add to make this like real chess notation
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]
def __str__(self):
if self.castle:
return "O-O" if self.endCol == 6 else "O-O-O"
endSquare = self.getRankFile(self.endRow, self.endCol)
#pawn moves
if self.pieceMoved[1] == "p":
if self.isCapture:
return self.colsToFiles[self.startCol] + "x" + endSquare
else:
return endSquare
#pawn promotions
#two of the same type moving to the same square
moveString = self.pieceMoved[1]
if self.isCapture:
moveString += "x"
return moveString + endSquare