-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChessEngine.py
641 lines (592 loc) · 31.7 KB
/
ChessEngine.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
"""
Storing all the information about the current state of chess game.
Determining valid moves at current state.
It will keep move log.
"""
class GameState:
def __init__(self):
"""
Board is an 8x8 2d list, each element in 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: 'R', 'N', 'B', 'Q', 'K' 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.white_to_move = True
self.move_log = []
self.white_king_location = (7, 4)
self.black_king_location = (0, 4)
self.checkmate = False
self.stalemate = False
self.in_check = False
self.pins = []
self.checks = []
self.enpassant_possible = () # coordinates for the square where en-passant capture is possible
self.enpassant_possible_log = [self.enpassant_possible]
self.current_castling_rights = CastleRights(True, True, True, True)
self.castle_rights_log = [CastleRights(self.current_castling_rights.wks, self.current_castling_rights.bks,
self.current_castling_rights.wqs, self.current_castling_rights.bqs)]
def makeMove(self, move):
"""
Takes a Move as a parameter and executes it.
(this will not work for castling, pawn promotion and en-passant)
"""
self.board[move.start_row][move.start_col] = "--"
self.board[move.end_row][move.end_col] = move.piece_moved
self.move_log.append(move) # log the move so we can undo it later
self.white_to_move = not self.white_to_move # switch players
# update king's location if moved
if move.piece_moved == "wK":
self.white_king_location = (move.end_row, move.end_col)
elif move.piece_moved == "bK":
self.black_king_location = (move.end_row, move.end_col)
# pawn promotion to Queen
if move.is_pawn_promotion:
self.board[move.end_row][move.end_col] = move.piece_moved[0] + "Q"
# enpassant move
if move.is_enpassant_move:
self.board[move.start_row][move.end_col] = "--" # capturing the pawn
# update enpassant_possible variable
if move.piece_moved[1] == "p" and abs(move.start_row - move.end_row) == 2: # only on 2 square pawn advance
self.enpassant_possible = ((move.start_row + move.end_row) // 2, move.start_col)
else:
self.enpassant_possible = ()
# castle move
if move.is_castle_move:
if move.end_col - move.start_col == 2: # king-side castle move
self.board[move.end_row][move.end_col - 1] = self.board[move.end_row][
move.end_col + 1] # moves the rook to its new square
self.board[move.end_row][move.end_col + 1] = '--' # erase old rook
else: # queen-side castle move
self.board[move.end_row][move.end_col + 1] = self.board[move.end_row][
move.end_col - 2] # moves the rook to its new square
self.board[move.end_row][move.end_col - 2] = '--' # erase old rook
self.enpassant_possible_log.append(self.enpassant_possible)
# update castling rights - whenever it is a rook or king move
self.updateCastleRights(move)
self.castle_rights_log.append(CastleRights(self.current_castling_rights.wks, self.current_castling_rights.bks,
self.current_castling_rights.wqs, self.current_castling_rights.bqs))
def undoMove(self):
"""
Undo the last move
"""
if len(self.move_log) != 0: # make sure that there is a move to undo
move = self.move_log.pop()
self.board[move.start_row][move.start_col] = move.piece_moved
self.board[move.end_row][move.end_col] = move.piece_captured
self.white_to_move = not self.white_to_move # swap players
# update the king's position if needed
if move.piece_moved == "wK":
self.white_king_location = (move.start_row, move.start_col)
elif move.piece_moved == "bK":
self.black_king_location = (move.start_row, move.start_col)
# undo en passant move
if move.is_enpassant_move:
self.board[move.end_row][move.end_col] = "--" # leave landing square blank
self.board[move.start_row][move.end_col] = move.piece_captured
self.enpassant_possible_log.pop()
self.enpassant_possible = self.enpassant_possible_log[-1]
# undo castle rights
self.castle_rights_log.pop() # get rid of the new castle rights from the move we are undoing
self.current_castling_rights = self.castle_rights_log[
-1] # set the current castle rights to the last one in the list
# undo the castle move
if move.is_castle_move:
if move.end_col - move.start_col == 2: # king-side
self.board[move.end_row][move.end_col + 1] = self.board[move.end_row][move.end_col - 1]
self.board[move.end_row][move.end_col - 1] = '--'
else: # queen-side
self.board[move.end_row][move.end_col - 2] = self.board[move.end_row][move.end_col + 1]
self.board[move.end_row][move.end_col + 1] = '--'
self.checkmate = False
self.stalemate = False
def updateCastleRights(self, move):
"""
Update the castle rights given the move
"""
if move.piece_captured == "wR":
if move.end_col == 0: # left rook
self.current_castling_rights.wqs = False
elif move.end_col == 7: # right rook
self.current_castling_rights.wks = False
elif move.piece_captured == "bR":
if move.end_col == 0: # left rook
self.current_castling_rights.bqs = False
elif move.end_col == 7: # right rook
self.current_castling_rights.bks = False
if move.piece_moved == 'wK':
self.current_castling_rights.wqs = False
self.current_castling_rights.wks = False
elif move.piece_moved == 'bK':
self.current_castling_rights.bqs = False
self.current_castling_rights.bks = False
elif move.piece_moved == 'wR':
if move.start_row == 7:
if move.start_col == 0: # left rook
self.current_castling_rights.wqs = False
elif move.start_col == 7: # right rook
self.current_castling_rights.wks = False
elif move.piece_moved == 'bR':
if move.start_row == 0:
if move.start_col == 0: # left rook
self.current_castling_rights.bqs = False
elif move.start_col == 7: # right rook
self.current_castling_rights.bks = False
def getValidMoves(self):
"""
All moves considering checks.
"""
temp_castle_rights = CastleRights(self.current_castling_rights.wks, self.current_castling_rights.bks,
self.current_castling_rights.wqs, self.current_castling_rights.bqs)
# advanced algorithm
moves = []
self.in_check, self.pins, self.checks = self.checkForPinsAndChecks()
if self.white_to_move:
king_row = self.white_king_location[0]
king_col = self.white_king_location[1]
else:
king_row = self.black_king_location[0]
king_col = self.black_king_location[1]
if self.in_check:
if len(self.checks) == 1: # only 1 check, block the check or move the king
moves = self.getAllPossibleMoves()
# to block the check you must put a piece into one of the squares between the enemy piece and your king
check = self.checks[0] # check information
check_row = check[0]
check_col = check[1]
piece_checking = self.board[check_row][check_col]
valid_squares = [] # squares that pieces can move to
# if knight, must capture the knight or move your king, other pieces can be blocked
if piece_checking[1] == "N":
valid_squares = [(check_row, check_col)]
else:
for i in range(1, 8):
valid_square = (king_row + check[2] * i,
king_col + check[3] * i) # check[2] and check[3] are the check directions
valid_squares.append(valid_square)
if valid_square[0] == check_row and valid_square[
1] == check_col: # once you get to piece and check
break
# get rid of any moves that don't block check or move king
for i in range(len(moves) - 1, -1, -1): # iterate through the list backwards when removing elements
if moves[i].piece_moved[1] != "K": # move doesn't move king so it must block or capture
if not (moves[i].end_row,
moves[i].end_col) in valid_squares: # move doesn't block or capture piece
moves.remove(moves[i])
else: # double check, king has to move
self.getKingMoves(king_row, king_col, moves)
else: # not in check - all moves are fine
moves = self.getAllPossibleMoves()
if self.white_to_move:
self.getCastleMoves(self.white_king_location[0], self.white_king_location[1], moves)
else:
self.getCastleMoves(self.black_king_location[0], self.black_king_location[1], moves)
if len(moves) == 0:
if self.inCheck():
self.checkmate = True
else:
# TODO stalemate on repeated moves
self.stalemate = True
else:
self.checkmate = False
self.stalemate = False
self.current_castling_rights = temp_castle_rights
return moves
def inCheck(self):
"""
Determine if a current player is in check
"""
if self.white_to_move:
return self.squareUnderAttack(self.white_king_location[0], self.white_king_location[1])
else:
return self.squareUnderAttack(self.black_king_location[0], self.black_king_location[1])
def squareUnderAttack(self, row, col):
"""
Determine if enemy can attack the square row col
"""
self.white_to_move = not self.white_to_move # switch to opponent's point of view
opponents_moves = self.getAllPossibleMoves()
self.white_to_move = not self.white_to_move
for move in opponents_moves:
if move.end_row == row and move.end_col == col: # square is under attack
return True
return False
def getAllPossibleMoves(self):
"""
All moves without considering checks.
"""
moves = []
for row in range(len(self.board)):
for col in range(len(self.board[row])):
turn = self.board[row][col][0]
if (turn == "w" and self.white_to_move) or (turn == "b" and not self.white_to_move):
piece = self.board[row][col][1]
self.moveFunctions[piece](row, col, moves) # calls appropriate move function based on piece type
return moves
def checkForPinsAndChecks(self):
pins = [] # squares pinned and the direction its pinned from
checks = [] # squares where enemy is applying a check
in_check = False
if self.white_to_move:
enemy_color = "b"
ally_color = "w"
start_row = self.white_king_location[0]
start_col = self.white_king_location[1]
else:
enemy_color = "w"
ally_color = "b"
start_row = self.black_king_location[0]
start_col = self.black_king_location[1]
# check outwards 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)):
direction = directions[j]
possible_pin = () # reset possible pins
for i in range(1, 8):
end_row = start_row + direction[0] * i
end_col = start_col + direction[1] * i
if 0 <= end_row <= 7 and 0 <= end_col <= 7:
end_piece = self.board[end_row][end_col]
if end_piece[0] == ally_color and end_piece[1] != "K":
if possible_pin == (): # first allied piece could be pinned
possible_pin = (end_row, end_col, direction[0], direction[1])
else: # 2nd allied piece - no check or pin from this direction
break
elif end_piece[0] == enemy_color:
enemy_type = end_piece[1]
# 5 possibilities 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
if (0 <= j <= 3 and enemy_type == "R") or (4 <= j <= 7 and enemy_type == "B") or (
i == 1 and enemy_type == "p" and (
(enemy_color == "w" and 6 <= j <= 7) or (enemy_color == "b" and 4 <= j <= 5))) or (
enemy_type == "Q") or (i == 1 and enemy_type == "K"):
if possible_pin == (): # no piece blocking, so check
in_check = True
checks.append((end_row, end_col, direction[0], direction[1]))
break
else: # piece blocking so pin
pins.append(possible_pin)
break
else: # enemy piece not applying checks
break
else:
break # off board
# check for knight checks
knight_moves = ((-2, -1), (-2, 1), (-1, 2), (1, 2), (2, -1), (2, 1), (-1, -2), (1, -2))
for move in knight_moves:
end_row = start_row + move[0]
end_col = start_col + move[1]
if 0 <= end_row <= 7 and 0 <= end_col <= 7:
end_piece = self.board[end_row][end_col]
if end_piece[0] == enemy_color and end_piece[1] == "N": # enemy knight attacking a king
in_check = True
checks.append((end_row, end_col, move[0], move[1]))
return in_check, pins, checks
def getPawnMoves(self, row, col, moves):
"""
Get all the pawn moves for the pawn located at row, col and add the moves to the list.
"""
piece_pinned = False
pin_direction = ()
for i in range(len(self.pins) - 1, -1, -1):
if self.pins[i][0] == row and self.pins[i][1] == col:
piece_pinned = True
pin_direction = (self.pins[i][2], self.pins[i][3])
self.pins.remove(self.pins[i])
break
if self.white_to_move:
move_amount = -1
start_row = 6
enemy_color = "b"
king_row, king_col = self.white_king_location
else:
move_amount = 1
start_row = 1
enemy_color = "w"
king_row, king_col = self.black_king_location
if self.board[row + move_amount][col] == "--": # 1 square pawn advance
if not piece_pinned or pin_direction == (move_amount, 0):
moves.append(Move((row, col), (row + move_amount, col), self.board))
if row == start_row and self.board[row + 2 * move_amount][col] == "--": # 2 square pawn advance
moves.append(Move((row, col), (row + 2 * move_amount, col), self.board))
if col - 1 >= 0: # capture to the left
if not piece_pinned or pin_direction == (move_amount, -1):
if self.board[row + move_amount][col - 1][0] == enemy_color:
moves.append(Move((row, col), (row + move_amount, col - 1), self.board))
if (row + move_amount, col - 1) == self.enpassant_possible:
attacking_piece = blocking_piece = False
if king_row == row:
if king_col < col: # king is left of the pawn
# inside: between king and the pawn;
# outside: between pawn and border;
inside_range = range(king_col + 1, col - 1)
outside_range = range(col + 1, 8)
else: # king right of the pawn
inside_range = range(king_col - 1, col, -1)
outside_range = range(col - 2, -1, -1)
for i in inside_range:
if self.board[row][i] != "--": # some piece beside en-passant pawn blocks
blocking_piece = True
for i in outside_range:
square = self.board[row][i]
if square[0] == enemy_color and (square[1] == "R" or square[1] == "Q"):
attacking_piece = True
elif square != "--":
blocking_piece = True
if not attacking_piece or blocking_piece:
moves.append(Move((row, col), (row + move_amount, col - 1), self.board, is_enpassant_move=True))
if col + 1 <= 7: # capture to the right
if not piece_pinned or pin_direction == (move_amount, +1):
if self.board[row + move_amount][col + 1][0] == enemy_color:
moves.append(Move((row, col), (row + move_amount, col + 1), self.board))
if (row + move_amount, col + 1) == self.enpassant_possible:
attacking_piece = blocking_piece = False
if king_row == row:
if king_col < col: # king is left of the pawn
# inside: between king and the pawn;
# outside: between pawn and border;
inside_range = range(king_col + 1, col)
outside_range = range(col + 2, 8)
else: # king right of the pawn
inside_range = range(king_col - 1, col + 1, -1)
outside_range = range(col - 1, -1, -1)
for i in inside_range:
if self.board[row][i] != "--": # some piece beside en-passant pawn blocks
blocking_piece = True
for i in outside_range:
square = self.board[row][i]
if square[0] == enemy_color and (square[1] == "R" or square[1] == "Q"):
attacking_piece = True
elif square != "--":
blocking_piece = True
if not attacking_piece or blocking_piece:
moves.append(Move((row, col), (row + move_amount, col + 1), self.board, is_enpassant_move=True))
def getRookMoves(self, row, col, moves):
"""
Get all the rook moves for the rook located at row, col and add the moves to the list.
"""
piece_pinned = False
pin_direction = ()
for i in range(len(self.pins) - 1, -1, -1):
if self.pins[i][0] == row and self.pins[i][1] == col:
piece_pinned = True
pin_direction = (self.pins[i][2], self.pins[i][3])
if self.board[row][col][
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, left, down, right
enemy_color = "b" if self.white_to_move else "w"
for direction in directions:
for i in range(1, 8):
end_row = row + direction[0] * i
end_col = col + direction[1] * i
if 0 <= end_row <= 7 and 0 <= end_col <= 7: # check for possible moves only in boundaries of the board
if not piece_pinned or pin_direction == direction or pin_direction == (
-direction[0], -direction[1]):
end_piece = self.board[end_row][end_col]
if end_piece == "--": # empty space is valid
moves.append(Move((row, col), (end_row, end_col), self.board))
elif end_piece[0] == enemy_color: # capture enemy piece
moves.append(Move((row, col), (end_row, end_col), self.board))
break
else: # friendly piece
break
else: # off board
break
def getKnightMoves(self, row, col, moves):
"""
Get all the knight moves for the knight located at row col and add the moves to the list.
"""
piece_pinned = False
for i in range(len(self.pins) - 1, -1, -1):
if self.pins[i][0] == row and self.pins[i][1] == col:
piece_pinned = True
self.pins.remove(self.pins[i])
break
knight_moves = ((-2, -1), (-2, 1), (-1, 2), (1, 2), (2, -1), (2, 1), (-1, -2),
(1, -2)) # up/left up/right right/up right/down down/left down/right left/up left/down
ally_color = "w" if self.white_to_move else "b"
for move in knight_moves:
end_row = row + move[0]
end_col = col + move[1]
if 0 <= end_row <= 7 and 0 <= end_col <= 7:
if not piece_pinned:
end_piece = self.board[end_row][end_col]
if end_piece[0] != ally_color: # so its either enemy piece or empty square
moves.append(Move((row, col), (end_row, end_col), self.board))
def getBishopMoves(self, row, col, moves):
"""
Get all the bishop moves for the bishop located at row col and add the moves to the list.
"""
piece_pinned = False
pin_direction = ()
for i in range(len(self.pins) - 1, -1, -1):
if self.pins[i][0] == row and self.pins[i][1] == col:
piece_pinned = True
pin_direction = (self.pins[i][2], self.pins[i][3])
self.pins.remove(self.pins[i])
break
directions = ((-1, -1), (-1, 1), (1, 1), (1, -1)) # diagonals: up/left up/right down/right down/left
enemy_color = "b" if self.white_to_move else "w"
for direction in directions:
for i in range(1, 8):
end_row = row + direction[0] * i
end_col = col + direction[1] * i
if 0 <= end_row <= 7 and 0 <= end_col <= 7: # check if the move is on board
if not piece_pinned or pin_direction == direction or pin_direction == (
-direction[0], -direction[1]):
end_piece = self.board[end_row][end_col]
if end_piece == "--": # empty space is valid
moves.append(Move((row, col), (end_row, end_col), self.board))
elif end_piece[0] == enemy_color: # capture enemy piece
moves.append(Move((row, col), (end_row, end_col), self.board))
break
else: # friendly piece
break
else: # off board
break
def getQueenMoves(self, row, col, moves):
"""
Get all the queen moves for the queen located at row col and add the moves to the list.
"""
self.getBishopMoves(row, col, moves)
self.getRookMoves(row, col, moves)
def getKingMoves(self, row, col, moves):
"""
Get all the king moves for the king located at row col and add the moves to the list.
"""
row_moves = (-1, -1, -1, 0, 0, 1, 1, 1)
col_moves = (-1, 0, 1, -1, 1, -1, 0, 1)
ally_color = "w" if self.white_to_move else "b"
for i in range(8):
end_row = row + row_moves[i]
end_col = col + col_moves[i]
if 0 <= end_row <= 7 and 0 <= end_col <= 7:
end_piece = self.board[end_row][end_col]
if end_piece[0] != ally_color: # not an ally piece - empty or enemy
# place king on end square and check for checks
if ally_color == "w":
self.white_king_location = (end_row, end_col)
else:
self.black_king_location = (end_row, end_col)
in_check, pins, checks = self.checkForPinsAndChecks()
if not in_check:
moves.append(Move((row, col), (end_row, end_col), self.board))
# place king back on original location
if ally_color == "w":
self.white_king_location = (row, col)
else:
self.black_king_location = (row, col)
def getCastleMoves(self, row, col, moves):
"""
Generate all valid castle moves for the king at (row, col) and add them to the list of moves.
"""
if self.squareUnderAttack(row, col):
return # can't castle while in check
if (self.white_to_move and self.current_castling_rights.wks) or (
not self.white_to_move and self.current_castling_rights.bks):
self.getKingsideCastleMoves(row, col, moves)
if (self.white_to_move and self.current_castling_rights.wqs) or (
not self.white_to_move and self.current_castling_rights.bqs):
self.getQueensideCastleMoves(row, col, moves)
def getKingsideCastleMoves(self, row, col, moves):
if self.board[row][col + 1] == '--' and self.board[row][col + 2] == '--':
if not self.squareUnderAttack(row, col + 1) and not self.squareUnderAttack(row, col + 2):
moves.append(Move((row, col), (row, col + 2), self.board, is_castle_move=True))
def getQueensideCastleMoves(self, row, col, moves):
if self.board[row][col - 1] == '--' and self.board[row][col - 2] == '--' and self.board[row][col - 3] == '--':
if not self.squareUnderAttack(row, col - 1) and not self.squareUnderAttack(row, col - 2):
moves.append(Move((row, col), (row, col - 2), self.board, is_castle_move=True))
class CastleRights:
def __init__(self, wks, bks, wqs, bqs):
self.wks = wks
self.bks = bks
self.wqs = wqs
self.bqs = bqs
class Move:
# in chess, fields on the board are described by two symbols, one of them being number between 1-8 (which is corresponding to rows)
# and the second one being a letter between a-f (corresponding to columns), in order to use this notation we need to map our [row][col] coordinates
# to match the ones used in the original chess game
ranks_to_rows = {"1": 7, "2": 6, "3": 5, "4": 4,
"5": 3, "6": 2, "7": 1, "8": 0}
rows_to_ranks = {v: k for k, v in ranks_to_rows.items()}
files_to_cols = {"a": 0, "b": 1, "c": 2, "d": 3,
"e": 4, "f": 5, "g": 6, "h": 7}
cols_to_files = {v: k for k, v in files_to_cols.items()}
def __init__(self, start_square, end_square, board, is_enpassant_move=False, is_castle_move=False):
self.start_row = start_square[0]
self.start_col = start_square[1]
self.end_row = end_square[0]
self.end_col = end_square[1]
self.piece_moved = board[self.start_row][self.start_col]
self.piece_captured = board[self.end_row][self.end_col]
# pawn promotion
self.is_pawn_promotion = (self.piece_moved == "wp" and self.end_row == 0) or (
self.piece_moved == "bp" and self.end_row == 7)
# en passant
self.is_enpassant_move = is_enpassant_move
if self.is_enpassant_move:
self.piece_captured = "wp" if self.piece_moved == "bp" else "bp"
# castle move
self.is_castle_move = is_castle_move
self.is_capture = self.piece_captured != "--"
self.moveID = self.start_row * 1000 + self.start_col * 100 + self.end_row * 10 + self.end_col
def __eq__(self, other):
"""
Overriding the equals method.
"""
if isinstance(other, Move):
return self.moveID == other.moveID
return False
def getChessNotation(self):
if self.is_pawn_promotion:
return self.getRankFile(self.end_row, self.end_col) + "Q"
if self.is_castle_move:
if self.end_col == 1:
return "0-0-0"
else:
return "0-0"
if self.is_enpassant_move:
return self.getRankFile(self.start_row, self.start_col)[0] + "x" + self.getRankFile(self.end_row,
self.end_col) + " e.p."
if self.piece_captured != "--":
if self.piece_moved[1] == "p":
return self.getRankFile(self.start_row, self.start_col)[0] + "x" + self.getRankFile(self.end_row,
self.end_col)
else:
return self.piece_moved[1] + "x" + self.getRankFile(self.end_row, self.end_col)
else:
if self.piece_moved[1] == "p":
return self.getRankFile(self.end_row, self.end_col)
else:
return self.piece_moved[1] + self.getRankFile(self.end_row, self.end_col)
def getRankFile(self, row, col):
return self.cols_to_files[col] + self.rows_to_ranks[row]
def __str__(self):
if self.is_castle_move:
return "0-0" if self.end_col == 6 else "0-0-0"
end_square = self.getRankFile(self.end_row, self.end_col)
if self.piece_moved[1] == "p":
if self.is_capture:
return self.cols_to_files[self.start_col] + "x" + end_square
else:
return end_square + "Q" if self.is_pawn_promotion else end_square
move_string = self.piece_moved[1]
if self.is_capture:
move_string += "x"
return move_string + end_square