-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathprincipalVariation.py
More file actions
402 lines (357 loc) · 19.5 KB
/
principalVariation.py
File metadata and controls
402 lines (357 loc) · 19.5 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
#!/usr/bin/env python3
"""
@author: Foo Zhi Yuan
ChessAI is being implemented using the concepts of bitboard and principal variation search
Requires Python 3 and Pillow(for GUI)
USAGE: python chessAI.py to play in GUI and python chessAI_console.py to play in console
"""
import math
from rating import Rating
from moves import Moves
from moveAndScore import MoveAndScore
class PrincipalVariation:
"""
Principal Variation/ NegaScout algorithm credits to https://webdocs.cs.ualberta.ca/~mmueller/courses/2014-AAAI-games-tutorial/slides/AAAI-14-Tutorial-Games-3-AlphaBeta.pdf
"""
Moves=None
Rating=None
playerIsWhite=None
maxDepth=None
def __init__(self,Moves,playerIsWhite,maxDepth):
self.Moves=Moves
self.Rating=Rating(Moves)
self.playerIsWhite=playerIsWhite
self.maxDepth=maxDepth
def max(self,value1,value2):
max=value1
if(value2>max):
max=value2
return max
#type moves: list
#sanitizeMove filters out invalid move in moves. eg: moves that will place current king in checked or being captured.
def sanitizeMove(self,moves,history,whiteKing,whiteQueen,whiteBishop,whiteKnight,whiteRook,whitePawn,blackKing,blackQueen,blackBishop,blackKnight,blackRook,blackPawn,whiteQueenCastle,whiteKingCastle,blackQueenCastle,blackKingCastle,whiteTurn):
Moves=self.Moves
moves=moves.split()
li=[]
for i in range(len(moves)):
currentMove=moves[i]
#white pieces
tempWhiteKing=Moves.makeMove(whiteKing,currentMove,"K")
tempWhiteQueen=Moves.makeMove(whiteQueen,currentMove,"Q")
tempWhiteBishop=Moves.makeMove(whiteBishop,currentMove,"B")
tempWhiteKnight=Moves.makeMove(whiteKnight,currentMove,"H")
tempWhiteRook=Moves.makeMove(whiteRook,currentMove,"R")
tempWhitePawn=Moves.makeMove(whitePawn,currentMove,"P")
#if castling, make castling move
if("WL" in currentMove or "WR" in currentMove):
tempWhiteKing,tempWhiteRook=Moves.makeCastlingMove(whiteKing,whiteRook,currentMove)
#black pieces
tempBlackKing=Moves.makeMove(blackKing,currentMove,"k")
tempBlackQueen=Moves.makeMove(blackQueen,currentMove,"q")
tempBlackBishop=Moves.makeMove(blackBishop,currentMove,"b")
tempBlackKnight=Moves.makeMove(blackKnight,currentMove,"h")
tempBlackRook=Moves.makeMove(blackRook,currentMove,"r")
tempBlackPawn=Moves.makeMove(blackPawn,currentMove,"p")
if("BL" in currentMove or "BR" in currentMove):
tempBlackKing,tempBlackRook=Moves.makeCastlingMove(blackKing,blackRook,currentMove)
tempHistory=currentMove
if(whiteTurn):
if((tempWhiteKing&Moves.whiteKing_illegalMoves(tempWhiteKing,tempWhiteQueen,tempWhiteBishop,tempWhiteKnight,tempWhiteRook,tempWhitePawn,tempBlackKing,tempBlackQueen,tempBlackBishop,tempBlackKnight,tempBlackRook,tempBlackPawn))==0):
li.append(currentMove)
elif(not whiteTurn):
if((tempBlackKing&Moves.blackKing_illegalMoves(tempWhiteKing,tempWhiteQueen,tempWhiteBishop,tempWhiteKnight,tempWhiteRook,tempWhitePawn,tempBlackKing,tempBlackQueen,tempBlackBishop,tempBlackKnight,tempBlackRook,tempBlackPawn))==0):
li.append(currentMove)
return li
def getScore(self,moveAndScore):
return moveAndScore.score
def sortMoves(self,moves,history,whiteKing,whiteQueen,whiteBishop,whiteKnight,whiteRook,whitePawn,blackKing,blackQueen,blackBishop,blackKnight,blackRook,blackPawn,whiteQueenCastle,whiteKingCastle,blackQueenCastle,blackKingCastle,whiteTurn,depth):
Moves=self.Moves
Rating=self.Rating
MoveAndScoreList=[]
li=[]
for i in range(len(moves)):
currentMove=moves[i]
try:
originalX=int(currentMove[0])
originalY=int(currentMove[1])
newX=int(currentMove[2])
newY=int(currentMove[3])
except(ValueError):
raise ValueError("Error in sortMoves: the first four character of currentMove string must be integer.")
start=(originalX*8)+originalY
end=(newX*8)+newY
##make currentMove
#white pieces
tempWhiteKing=Moves.makeMove(whiteKing,currentMove,"K")
tempWhiteQueen=Moves.makeMove(whiteQueen,currentMove,"Q")
tempWhiteBishop=Moves.makeMove(whiteBishop,currentMove,"B")
tempWhiteKnight=Moves.makeMove(whiteKnight,currentMove,"H")
tempWhiteRook=Moves.makeMove(whiteRook,currentMove,"R")
tempWhitePawn=Moves.makeMove(whitePawn,currentMove,"P")
#if castling, make castling move
if("WL" in currentMove or "WR" in currentMove):
tempWhiteKing,tempWhiteRook=Moves.makeCastlingMove(whiteKing,whiteRook,currentMove)
#black pieces
tempBlackKing=Moves.makeMove(blackKing,currentMove,"k")
tempBlackQueen=Moves.makeMove(blackQueen,currentMove,"q")
tempBlackBishop=Moves.makeMove(blackBishop,currentMove,"b")
tempBlackKnight=Moves.makeMove(blackKnight,currentMove,"h")
tempBlackRook=Moves.makeMove(blackRook,currentMove,"r")
tempBlackPawn=Moves.makeMove(blackPawn,currentMove,"p")
if("BL" in currentMove or "BR" in currentMove):
tempBlackKing,tempBlackRook=Moves.makeCastlingMove(blackKing,blackRook,currentMove)
tempHistory=currentMove
###update castling variables
#copy castling variables from previous
tempWhiteQueenCastle=whiteQueenCastle
tempWhiteKingCastle=whiteKingCastle
tempBlackQueenCastle=blackQueenCastle
tempBlackKingCastle=blackKingCastle
#update castling variable based on the currentMove we made
#if currentMove is making white castling move, we can no longer castle again for white
if("WL" in currentMove or "WR" in currentMove):
tempWhiteQueenCastle=False
tempWhiteKingCastle=False
#if currentMove is making black castling move, we can no longer castle again for black
elif("BL" in currentMove or "BR" in currentMove):
tempBlackQueenCastle=False
tempBlackKingCastle=False
else:
#if currentMove is moving whiteKing, white queen and king side castle become False
if(((1<<start)&whiteKing)!=0):
tempWhiteQueenCastle=False
tempWhiteKingCastle=False
#if currentMove is moving blackKing, black queen and king side castle become False
elif(((1<<start)&blackKing)!=0):
tempBlackQueenCastle=False
tempBlackKingCastle=False
#if currentMove is moving white left rook, white queenside castling become False
elif(((1<<start)&whiteRook&(1<<56))!=0):
tempWhiteQueenCastle=False
#if currentMove is moving white right rook, white kingside castling become False
elif(((1<<start)&whiteRook&(1<<63))!=0):
tempWhiteKingCastle=False
elif(((1<<start)&blackRook&(1<<0))!=0):
tempBlackQueenCastle=False
elif(((1<<start)&blackRook&(1<<7))!=0):
tempBlackKingCastle=False
currentScore=Rating.quickEvaluate(tempHistory,tempWhiteKing,tempWhiteQueen,tempWhiteBishop,tempWhiteKnight,tempWhiteRook,tempWhitePawn,tempBlackKing,tempBlackQueen,tempBlackBishop,tempBlackKnight,tempBlackRook,tempBlackPawn,tempWhiteQueenCastle,tempWhiteKingCastle,tempBlackQueenCastle,tempBlackKingCastle,depth+1,self.playerIsWhite)
moveAndScore=MoveAndScore(currentMove,currentScore)
MoveAndScoreList.append(moveAndScore)
#Idea:
#if this is player's turn, we need to return list sorted in descending order. Because player want to choose the most maximum score move.
#If this is opponent's turn, we need to return list sorted in ascending order. Because opponent want to choose the most minimum score move.
#if this is player's turn, sort in descending order
if(self.playerIsWhite==whiteTurn):
MoveAndScoreList.sort(key=self.getScore,reverse=True)
#if this is opponent's turn, sort in ascending order
else:
MoveAndScoreList.sort(key=self.getScore,reverse=False)
for i in range(len(MoveAndScoreList)):
li.append(MoveAndScoreList[i].move)
return li
#given current board, return the best score this board can achieves.
#player will always choose largest score as best score, opponent will always choose lowest score as best score
#Given a state of chessBoard, return the best score this state of chessBoard can achieve for current player, and the best move that can be made for current player
#rtype: int(score), string(best move)
def principalVariationSearch(self,alpha,beta,history,whiteKing,whiteQueen,whiteBishop,whiteKnight,whiteRook,whitePawn,blackKing,blackQueen,blackBishop,blackKnight,blackRook,blackPawn,whiteQueenCastle,whiteKingCastle,blackQueenCastle,blackKingCastle,whiteTurn,depth):
Moves=self.Moves
Rating=self.Rating
#if we reach our max search depth, return the best score for the board at that level
if(depth==self.maxDepth):
bestScore=Rating.evaluate(history,whiteKing,whiteQueen,whiteBishop,whiteKnight,whiteRook,whitePawn,blackKing,blackQueen,blackBishop,blackKnight,blackRook,blackPawn,whiteQueenCastle,whiteKingCastle,blackQueenCastle,blackKingCastle,depth, self.playerIsWhite)
return bestScore,"No Move"
if(whiteTurn):
moves=Moves.white_legalMoves(history,whiteKing,whiteQueen,whiteBishop,whiteKnight,whiteRook,whitePawn,blackKing,blackQueen,blackBishop,blackKnight,blackRook,blackPawn,whiteQueenCastle,whiteKingCastle,blackQueenCastle,blackKingCastle)
else:
moves=Moves.black_legalMoves(history,whiteKing,whiteQueen,whiteBishop,whiteKnight,whiteRook,whitePawn,blackKing,blackQueen,blackBishop,blackKnight,blackRook,blackPawn,whiteQueenCastle,whiteKingCastle,blackQueenCastle,blackKingCastle)
#get the first legal move for the current player. After the moves have been sorted, we assume that the first move is always the best move.
moves=self.sanitizeMove(moves,history,whiteKing,whiteQueen,whiteBishop,whiteKnight,whiteRook,whitePawn,blackKing,blackQueen,blackBishop,blackKnight,blackRook,blackPawn,whiteQueenCastle,whiteKingCastle,blackQueenCastle,blackKingCastle,whiteTurn)
#if no legal move for current player, then it must be checkmate or stalemate
#if this is player's turn, this is really bad. return -5000
#if this is opponent's turn, this is good. we want this. return 5000
#TODO: Check this AND FIX THIS!
if(len(moves)==0):
if(self.playerIsWhite==whiteTurn):
return -5000,"No Move"
else:
return 5000,"No Move"
moves=self.sortMoves(moves,history,whiteKing,whiteQueen,whiteBishop,whiteKnight,whiteRook,whitePawn,blackKing,blackQueen,blackBishop,blackKnight,blackRook,blackPawn,whiteQueenCastle,whiteKingCastle,blackQueenCastle,blackKingCastle,whiteTurn,depth)
firstLegalMoveIndex=0
b=beta
bestScore=-math.inf
####################################### throughroughly search firstLegalMove
firstLegalMove=moves[firstLegalMoveIndex]
try:
originalX=int(firstLegalMove[0])
originalY=int(firstLegalMove[1])
newX=int(firstLegalMove[2])
newY=int(firstLegalMove[3])
except(ValueError):
raise ValueError("Error in principalVariationSearch: the first four character of firstLegalMove string must be integer.")
start=(originalX*8)+originalY
end=(newX*8)+newY
#white pieces
tempWhiteKing=Moves.makeMove(whiteKing,firstLegalMove,"K")
tempWhiteQueen=Moves.makeMove(whiteQueen,firstLegalMove,"Q")
tempWhiteBishop=Moves.makeMove(whiteBishop,firstLegalMove,"B")
tempWhiteKnight=Moves.makeMove(whiteKnight,firstLegalMove,"H")
tempWhiteRook=Moves.makeMove(whiteRook,firstLegalMove,"R")
tempWhitePawn=Moves.makeMove(whitePawn,firstLegalMove,"P")
#if castling, make castling move
if("WL" in firstLegalMove or "WR" in firstLegalMove):
tempWhiteKing,tempWhiteRook=Moves.makeCastlingMove(whiteKing,whiteRook,firstLegalMove)
#black pieces
tempBlackKing=Moves.makeMove(blackKing,firstLegalMove,"k")
tempBlackQueen=Moves.makeMove(blackQueen,firstLegalMove,"q")
tempBlackBishop=Moves.makeMove(blackBishop,firstLegalMove,"b")
tempBlackKnight=Moves.makeMove(blackKnight,firstLegalMove,"h")
tempBlackRook=Moves.makeMove(blackRook,firstLegalMove,"r")
tempBlackPawn=Moves.makeMove(blackPawn,firstLegalMove,"p")
if("BL" in firstLegalMove or "BR" in firstLegalMove):
tempBlackKing,tempBlackRook=Moves.makeCastlingMove(blackKing,blackRook,firstLegalMove)
tempHistory=firstLegalMove
###update castling variables
#copy castling variables from previous
tempWhiteQueenCastle=whiteQueenCastle
tempWhiteKingCastle=whiteKingCastle
tempBlackQueenCastle=blackQueenCastle
tempBlackKingCastle=blackKingCastle
#update castling variable based on the firstLegalMove we made
#if firstLegalMove is making white castling move, we can no longer castle again for white
if("WL" in firstLegalMove or "WR" in firstLegalMove):
tempWhiteQueenCastle=False
tempWhiteKingCastle=False
#if firstLegalMove is making black castling move, we can no longer castle again for black
elif("BL" in firstLegalMove or "BR" in firstLegalMove):
tempBlackQueenCastle=False
tempBlackKingCastle=False
else:
#if firstLegalMove is moving whiteKing, white queen and king side castle become False
if(((1<<start)&whiteKing)!=0):
tempWhiteQueenCastle=False
tempWhiteKingCastle=False
#if firstLegalMove is moving blackKing, black queen and king side castle become False
elif(((1<<start)&blackKing)!=0):
tempBlackQueenCastle=False
tempBlackKingCastle=False
#if firstLegalMove is moving white left rook, white queenside castling become False
elif(((1<<start)&whiteRook&(1<<56))!=0):
tempWhiteQueenCastle=False
#if firstLegalMove is moving white right rook, white kingside castling become False
elif(((1<<start)&whiteRook&(1<<63))!=0):
tempWhiteKingCastle=False
elif(((1<<start)&blackRook&(1<<0))!=0):
tempBlackQueenCastle=False
elif(((1<<start)&blackRook&(1<<7))!=0):
tempBlackKingCastle=False
"""
# original algorithm
score,bestMove=-self.principalVariationSearch(-b,-alpha,tempHistory,tempWhiteKing,tempWhiteQueen,tempWhiteBishop,tempWhiteKnight,tempWhiteRook,tempWhitePawn,tempBlackKing,tempBlackQueen,tempBlackBishop,tempBlackKnight,tempBlackRook,tempBlackPawn,tempWhiteQueenCastle,tempWhiteKingCastle,tempBlackQueenCastle,tempBlackKingCastle,not whiteTurn,depth+1)
"""
#Alternate way of writing to original algorithm
score,bestMove=self.principalVariationSearch(-b,-alpha,tempHistory,tempWhiteKing,tempWhiteQueen,tempWhiteBishop,tempWhiteKnight,tempWhiteRook,tempWhitePawn,tempBlackKing,tempBlackQueen,tempBlackBishop,tempBlackKnight,tempBlackRook,tempBlackPawn,tempWhiteQueenCastle,tempWhiteKingCastle,tempBlackQueenCastle,tempBlackKingCastle,not whiteTurn,depth+1)
score=-score
#In principal variation search, we assume that our first move is the best move, and thus yield the best score.
bestScore=self.max(bestScore,score) #can also write bestScore=score
alpha=self.max(alpha,score)
bestMoveIndex=firstLegalMoveIndex
if(alpha>=beta):
return alpha, moves[bestMoveIndex]
b=alpha+1
####################################### simply and quickly search through remaining move to confirm our assumption that firstLegalMove is the best move
for i in range(len(moves)):
#if current iteration is firstLegalMoveIndex, skip, since we already thoroughly searched this move.
if(i==firstLegalMoveIndex):
continue
#if current iteration is not firstLegalMoveIndex, simply and quickly search through current move.
currentMove=moves[i]
try:
originalX=int(currentMove[0])
originalY=int(currentMove[1])
newX=int(currentMove[2])
newY=int(currentMove[3])
except(ValueError):
raise ValueError("Error in principalVariationSearch: the first four character of currentMove string must be integer.")
start=(originalX*8)+originalY
end=(newX*8)+newY
#white pieces
tempWhiteKing=Moves.makeMove(whiteKing,currentMove,"K")
tempWhiteQueen=Moves.makeMove(whiteQueen,currentMove,"Q")
tempWhiteBishop=Moves.makeMove(whiteBishop,currentMove,"B")
tempWhiteKnight=Moves.makeMove(whiteKnight,currentMove,"H")
tempWhiteRook=Moves.makeMove(whiteRook,currentMove,"R")
tempWhitePawn=Moves.makeMove(whitePawn,currentMove,"P")
#if castling, make castling move
if("WL" in currentMove or "WR" in currentMove):
tempWhiteKing,tempWhiteRook=Moves.makeCastlingMove(whiteKing,whiteRook,currentMove)
#black pieces
tempBlackKing=Moves.makeMove(blackKing,currentMove,"k")
tempBlackQueen=Moves.makeMove(blackQueen,currentMove,"q")
tempBlackBishop=Moves.makeMove(blackBishop,currentMove,"b")
tempBlackKnight=Moves.makeMove(blackKnight,currentMove,"h")
tempBlackRook=Moves.makeMove(blackRook,currentMove,"r")
tempBlackPawn=Moves.makeMove(blackPawn,currentMove,"p")
if("BL" in currentMove or "BR" in currentMove):
tempBlackKing,tempBlackRook=Moves.makeCastlingMove(blackKing,blackRook,currentMove)
tempHistory=currentMove
###update castling variables
#copy castling variables from previous
tempWhiteQueenCastle=whiteQueenCastle
tempWhiteKingCastle=whiteKingCastle
tempBlackQueenCastle=blackQueenCastle
tempBlackKingCastle=blackKingCastle
#update castling variable based on the currentMove we made
#if currentMove is making white castling move, we can no longer castle again for white
if("WL" in currentMove or "WR" in currentMove):
tempWhiteQueenCastle=False
tempWhiteKingCastle=False
#if currentMove is making black castling move, we can no longer castle again for black
elif("BL" in currentMove or "BR" in currentMove):
tempBlackQueenCastle=False
tempBlackKingCastle=False
else:
#if currentMove is moving whiteKing, white queen and king side castle become False
if(((1<<start)&whiteKing)!=0):
tempWhiteQueenCastle=False
tempWhiteKingCastle=False
#if currentMove is moving blackKing, black queen and king side castle become False
elif(((1<<start)&blackKing)!=0):
tempBlackQueenCastle=False
tempBlackKingCastle=False
#if currentMove is moving white left rook, white queenside castling become False
elif(((1<<start)&whiteRook&(1<<56))!=0):
tempWhiteQueenCastle=False
#if currentMove is moving white right rook, white kingside castling become False
elif(((1<<start)&whiteRook&(1<<63))!=0):
tempWhiteKingCastle=False
elif(((1<<start)&blackRook&(1<<0))!=0):
tempBlackQueenCastle=False
elif(((1<<start)&blackRook&(1<<7))!=0):
tempBlackKingCastle=False
#TODO: check this!
"""
# original algorithm
score,bestMove=-self.principalVariationSearch(-b,-alpha,tempHistory,tempWhiteKing,tempWhiteQueen,tempWhiteBishop,tempWhiteKnight,tempWhiteRook,tempWhitePawn,tempBlackKing,tempBlackQueen,tempBlackBishop,tempBlackKnight,tempBlackRook,tempBlackPawn,tempWhiteQueenCastle,tempWhiteKingCastle,tempBlackQueenCastle,tempBlackKingCastle,not whiteTurn,depth+1)
"""
#Alternate way of writing to original algorithm
score,bestMove=self.principalVariationSearch(-b,-alpha,tempHistory,tempWhiteKing,tempWhiteQueen,tempWhiteBishop,tempWhiteKnight,tempWhiteRook,tempWhitePawn,tempBlackKing,tempBlackQueen,tempBlackBishop,tempBlackKnight,tempBlackRook,tempBlackPawn,tempWhiteQueenCastle,tempWhiteKingCastle,tempBlackQueenCastle,tempBlackKingCastle,not whiteTurn,depth+1)
score=-score
if(score>alpha and score<beta):
"""
# original algorithm
score,bestMove=-self.principalVariationSearch(-beta,-score,tempHistory,tempWhiteKing,tempWhiteQueen,tempWhiteBishop,tempWhiteKnight,tempWhiteRook,tempWhitePawn,tempBlackKing,tempBlackQueen,tempBlackBishop,tempBlackKnight,tempBlackRook,tempBlackPawn,tempWhiteQueenCastle,tempWhiteKingCastle,tempBlackQueenCastle,tempBlackKingCastle,not whiteTurn,depth+1)
"""
#alternate way of writing to original algorithm
score,bestMove=self.principalVariationSearch(-beta,-score,tempHistory,tempWhiteKing,tempWhiteQueen,tempWhiteBishop,tempWhiteKnight,tempWhiteRook,tempWhitePawn,tempBlackKing,tempBlackQueen,tempBlackBishop,tempBlackKnight,tempBlackRook,tempBlackPawn,tempWhiteQueenCastle,tempWhiteKingCastle,tempBlackQueenCastle,tempBlackKingCastle,not whiteTurn,depth+1)
score=-score
##For debugging
# print("researched")
if(score>bestScore):
bestMoveIndex=i
bestScore=score
alpha=self.max(alpha,score)
if(alpha>=beta):
return alpha, moves[bestMoveIndex]
b=alpha+1
return bestScore, moves[bestMoveIndex]