-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathChessEngine.py
401 lines (362 loc) · 15.9 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
class GameState():
def __init__(self):
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.moveLog = []
self.whiteKingLocation = (7, 4)
self.blackKingLocation = (0, 4)
self.checkMate = False
self.staleMate = False # end game
def boardConvert(self):
boardC = self.board
for piece in board:
if piece[0] == 'b':
piece = piece[1].lower()
if piece[0] == 'w':
piece = piece[1].upper()
if piece[0] == '--':
piece = None
return boardC
def makeMove(self, move):
self.board[move.startRow][move.startCol] = "--"
self.board[move.endRow][move.endCol] = move.pieceMove
self.moveLog.append(move) # log the move to undo
self.whiteToMove = not self.whiteToMove # swap player
# upadate king location
if move.pieceMove == "wK":
self.whiteKingLocation = (move.endRow, move.endCol)
if move.pieceMove == "bK":
self.blackKingLocation = (move.endRow, move.endCol)
# undo last move
def undoMove(self):
if len(self.moveLog) != 0:
move = self.moveLog.pop()
self.board[move.startRow][move.startCol] = move.pieceMove
self.board[move.endRow][move.endCol] = move.pieceCaptured
self.whiteToMove = not self.whiteToMove
# upadate king location
if move.pieceMove == "wK":
self.whiteKingLocation = (move.startRow, move.startCol)
if move.pieceMove == "bK":
self.blackKingLocation = (move.startRow, move.startCol)
# considering checkmate moves[] = move((1,2),(2,3))
def getValidMoves(self):
moves = self.getAllPossibleMoves()
for i in range(len(moves) - 1, -1, -1): # loop backwards
self.makeMove(moves[i])
self.whiteToMove = not self.whiteToMove
if self.inCheck():
moves.remove(moves[i])
self.whiteToMove = not self.whiteToMove
self.undoMove()
if len(moves) == 0:
if self.inCheck():
self.checkMate = True
else:
self.staleMate = True
else:
self.checkMate = False
self.staleMate = False
return moves
# Determine if in check
def inCheck(self):
if self.whiteToMove:
return self.squareUnderAttack(self.whiteKingLocation[0], self.whiteKingLocation[1])
else:
return self.squareUnderAttack(self.blackKingLocation[0], self.blackKingLocation[1])
def squareUnderAttack(self, r, c):
self.whiteToMove = not self.whiteToMove # switch opponent's turn
oppMoves = self.getAllPossibleMoves()
self.whiteToMove = not self.whiteToMove # switch turn back
for move in oppMoves:
if move.endRow == r and move.endCol == c: # square under attack
return True
return False
# not considering checkmate
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):
peice = self.board[r][c][1]
self.moveFunctions[peice](r, c, moves)
return moves
def getPawnMoves(self, r, c, moves):
if self.whiteToMove:
if self.board[r - 1][c] == "--":
moves.append(Move((r, c), (r - 1, c), self.board))
if r == 6 and self.board[r - 2][c] == '--':
moves.append(Move((r, c), (r - 2, c), self.board))
if c - 1 >= 0:
if self.board[r - 1][c - 1][0] == 'b':
moves.append(Move((r, c), (r - 1, c - 1), self.board))
if c + 1 <= 7:
if self.board[r - 1][c + 1][0] == 'b':
moves.append(Move((r, c), (r - 1, c + 1), self.board))
if not self.whiteToMove:
if self.board[r + 1][c] == "--":
moves.append(Move((r, c), (r + 1, c), self.board))
if r == 1 and self.board[r + 2][c] == '--':
moves.append(Move((r, c), (r + 2, c), self.board))
if c - 1 >= 0:
if self.board[r + 1][c - 1][0] == 'w':
moves.append(Move((r, c), (r + 1, c - 1), self.board))
if c + 1 <= 7:
if self.board[r + 1][c + 1][0] == 'w':
moves.append(Move((r, c), (r + 1, c + 1), self.board))
def getRookMoves(self, r, c, moves):
direction = ((-1, 0), (0, -1), (1, 0), (0, 1))
enemyColor = 'b' if self.whiteToMove else 'w'
for d in direction:
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 == '--':
moves.append(Move((r, c), (endRow, endCol), self.board))
elif endpiece[0] == enemyColor:
moves.append(Move((r, c), (endRow, endCol), self.board))
break
else:
break
else:
break
def getKnightMoves(self, r, c, moves):
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:
endPiece = self.board[endRow][endCol]
if endPiece[0] != allyColor:
moves.append(Move((r, c), (endRow, endCol), self.board))
def getBishopMoves(self, r, c, moves):
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):
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 == '--':
moves.append(Move((r, c), (endRow, endCol), self.board))
elif endpiece[0] == enemyColor:
moves.append(Move((r, c), (endRow, endCol), self.board))
break
else:
break
else:
break
def getQueenMoves(self, r, c, moves):
self.getBishopMoves(r, c, moves)
self.getRookMoves(r, c, moves)
def getKingMoves(self, r, c, moves):
kingMoves = ((-1, -1), (-1, 1), (-1, 0), (0, -1), (0, 1), (1, -1), (1, 0), (1, 1))
allyColor = 'w' if self.whiteToMove else 'b'
for i in range(8):
endRow = r + kingMoves[i][0]
endCol = c + kingMoves[i][1]
if 0 <= endRow < 8 and 0 <= endCol < 8:
endPiece = self.board[endRow][endCol]
if endPiece[0] != allyColor:
moves.append(Move((r, c), (endRow, endCol), self.board))
class Move():
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):
self.startRow = startSq[0]
self.startCol = startSq[1]
self.endRow = endSq[0]
self.endCol = endSq[1]
self.pieceMove = board[self.startRow][self.startCol]
self.pieceCaptured = board[self.endRow][self.endCol]
self.moveID = self.startRow * 1000 + self.startCol * 100 + self.endRow * 10 + self.endCol
# override equal method
def __eq__(self, other):
if isinstance(other, Move):
return self.moveID == other.moveID
return False
def getChessNotation(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]
class MinMaxPruning():
def __init__(self):
self.whitePawnEval = [
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0, 5.0],
[1.0, 1.0, 2.0, 3.0, 3.0, 2.0, 1.0, 1.0],
[0.5, 0.5, 1.0, 2.5, 2.5, 1.0, 0.5, 0.5],
[0.0, 0.0, 0.0, 2.0, 2.0, 0.0, 0.0, 0.0],
[0.5, -0.5, -1.0, 0.0, 0.0, -1.0, -0.5, 0.5],
[0.5, 1.0, 1.0, -2.0, -2.0, 1.0, 1.0, 0.5],
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0]
]
self.blackPawnEval = list(reversed(self.whitePawnEval))
self.whiteKnightEval = [
[-5.0, -4.0, -3.0, -3.0, -3.0, -3.0, -4.0, -5.0],
[-4.0, -2.0, 0.0, 0.0, 0.0, 0.0, -2.0, -4.0],
[-3.0, 0.0, 1.0, 1.5, 1.5, 1.0, 0.0, -3.0],
[-3.0, 0.5, 1.5, 2.0, 2.0, 1.5, 0.5, -3.0],
[-3.0, 0.0, 1.5, 2.0, 2.0, 1.5, 0.0, -3.0],
[-3.0, 0.5, 1.0, 1.5, 1.5, 1.0, 0.5, -3.0],
[-4.0, -2.0, 0.0, 0.5, 0.5, 0.0, -2.0, -4.0],
[-5.0, -4.0, -3.0, -3.0, -3.0, -3.0, -4.0, -5.0]
]
self.blackKnightEval = list(reversed(self.whiteKnightEval))
self.whiteBishopEval = [
[-2.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -2.0],
[-1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0],
[-1.0, 0.0, 0.5, 1.0, 1.0, 0.5, 0.0, -1.0],
[-1.0, 0.5, 0.5, 1.0, 1.0, 0.5, 0.5, -1.0],
[-1.0, 0.0, 1.0, 1.0, 1.0, 1.0, 0.0, -1.0],
[-1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, -1.0],
[-1.0, 0.5, 0.0, 0.0, 0.0, 0.0, 0.5, -1.0],
[-2.0, -1.0, -1.0, -1.0, -1.0, -1.0, -1.0, -2.0]
]
self.blackBishopEval = list(reversed(self.whiteBishopEval))
self.whiteRookEval = [
[0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0],
[0.5, 1.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.5],
[-0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.5],
[-0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.5],
[-0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.5],
[-0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.5],
[-0.5, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -0.5],
[0.0, 0.0, 0.0, 0.5, 0.5, 0.0, 0.0, 0.0]
]
self.blackRookEval = list(reversed(self.whiteRookEval))
self.whiteQueenEval = [
[-2.0, -1.0, -1.0, -0.5, -0.5, -1.0, -1.0, -2.0],
[-1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, -1.0],
[-1.0, 0.0, 0.5, 0.5, 0.5, 0.5, 0.0, -1.0],
[-0.5, 0.0, 0.5, 0.5, 0.5, 0.5, 0.0, -0.5],
[0.0, 0.0, 0.5, 0.5, 0.5, 0.5, 0.0, -0.5],
[-1.0, 0.5, 0.5, 0.5, 0.5, 0.5, 0.0, -1.0],
[-1.0, 0.0, 0.5, 0.0, 0.0, 0.0, 0.0, -1.0],
[-2.0, -1.0, -1.0, -0.5, -0.5, -1.0, -1.0, -2.0]
]
self.blackQueenEval = list(reversed(self.whiteQueenEval))
self.whiteKingEval = [
[-3.0, -4.0, -4.0, -5.0, -5.0, -4.0, -4.0, -3.0],
[-3.0, -4.0, -4.0, -5.0, -5.0, -4.0, -4.0, -3.0],
[-3.0, -4.0, -4.0, -5.0, -5.0, -4.0, -4.0, -3.0],
[-3.0, -4.0, -4.0, -5.0, -5.0, -4.0, -4.0, -3.0],
[-2.0, -3.0, -3.0, -4.0, -4.0, -3.0, -3.0, -2.0],
[-1.0, -2.0, -2.0, -2.0, -2.0, -2.0, -2.0, -1.0],
[2.0, 2.0, 0.0, 0.0, 0.0, 0.0, 2.0, 2.0],
[2.0, 3.0, 1.0, 0.0, 0.0, 1.0, 3.0, 2.0]
]
self.blackKingEval = list(reversed(self.whiteKingEval))
def evaluateBoard(self, board):
totalEval = 0
for row in range(8):
for col in range(8):
totalEval += self.getPieceValue(board[row][col], row, col)
return totalEval
def getPieceValue(self, piece, r, c):
if(piece == None):
return 0
absoluteValue = 0
if piece[1] == 'p':
if piece[0] == "w":
absoluteValue = 10 + self.whitePawnEval[r][c]
else:
absoluteValue = 10 + self.blackPawnEval[r][c]
elif piece[1] == 'N':
if(piece[0] == "w"):
absoluteValue = 30 + self.whiteKnightEval[r][c]
else:
absoluteValue = 30 + self.blackKnightEval[r][c]
elif piece[1] == 'B':
if (piece[0] == "w"):
absoluteValue = 30 + self.whiteBishopEval[r][c]
else:
absoluteValue = 30 + self.blackBishopEval[r][c]
elif piece[1] == 'R':
if (piece[0] == "w"):
absoluteValue = 50 + self.whiteRookEval[r][c]
else:
absoluteValue = 50 + self.blackRookEval[r][c]
elif piece[1] == 'Q':
if (piece[0] == "w"):
absoluteValue = 90 + self.whiteQueenEval[r][c]
else:
absoluteValue = 90 + self.blackQueenEval[r][c]
elif piece[1] == 'K':
if (piece[0] == "w"):
absoluteValue = 900 + self.whiteKingEval[r][c]
else:
absoluteValue = 900 + self.blackKingEval[r][c]
if (piece[0] == 'w'):
return absoluteValue
else:
return -absoluteValue
def evaluateBoard(self, board):
totalEvaluation = 0
for row in range(8):
for col in range(8):
totalEvaluation += self.getPieceValue(board[row][col], row, col)
return totalEvaluation
def minmaxRoot(self, depth, gs):
possibleMoves = gs.getValidMoves()
bestMove = -9999
bestMoveFinal = None
for move in possibleMoves:
gs.makeMove(move)
#gs.whiteToMove = not gs.whiteToMove #switch turn
#value = max(bestMove, self.minimax(depth - 1, gs,-10000,10000,True))
value = self.minimax(depth - 1, gs , -9999, 9999, False)
gs.undoMove()
if(value > bestMove):
bestMove = value
bestMoveFinal = ()
bestMoveFinal = move
return bestMoveFinal
def minimax(self, depth, gs, alpha, beta, maximizingPlayer):
if (depth == 0):
return -self.evaluateBoard(gs.board)
possibleMoves = gs.getValidMoves()
if maximizingPlayer:
bestMove = -9999
for move in possibleMoves:
gs.makeMove(move)
temp = self.minimax(depth - 1, gs, alpha, beta, False)
bestMove = max(bestMove, temp)
alpha = max(alpha, temp)
if beta <= alpha:
gs.undoMove()
break
gs.undoMove()
return bestMove
else:
bestMove = 9999
for move in possibleMoves:
gs.makeMove(move)
temp = self.minimax(depth - 1, gs, alpha, beta,True)
bestMove = min(bestMove, temp)
#gs.undoMove()
beta = min(beta, temp)
if beta <= alpha:
gs.undoMove()
break
gs.undoMove()
return bestMove