-
Notifications
You must be signed in to change notification settings - Fork 0
/
chessAI.py
228 lines (199 loc) · 7.76 KB
/
chessAI.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
import random
import re
from typing import Counter
pieceScore = {"K": 0, "Q": 9, "R": 5, "B": 3, "N": 3, "p": 1}
knightScores = [[0.0, 0.1, 0.2, 0.2, 0.2, 0.2, 0.1, 0.0],
[0.1, 0.3, 0.5, 0.5, 0.5, 0.5, 0.3, 0.1],
[0.2, 0.5, 0.6, 0.65, 0.65, 0.6, 0.5, 0.2],
[0.2, 0.55, 0.65, 0.7, 0.7, 0.65, 0.55, 0.2],
[0.2, 0.5, 0.65, 0.7, 0.7, 0.65, 0.5, 0.2],
[0.2, 0.55, 0.6, 0.65, 0.65, 0.6, 0.55, 0.2],
[0.1, 0.3, 0.5, 0.55, 0.55, 0.5, 0.3, 0.1],
[0.0, 0.1, 0.2, 0.2, 0.2, 0.2, 0.1, 0.0]]
bishopScores = [[0.0, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.0],
[0.2, 0.4, 0.4, 0.4, 0.4, 0.4, 0.4, 0.2],
[0.2, 0.4, 0.5, 0.6, 0.6, 0.5, 0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, 0.3, 0.45, 0.45, 0.3, 0.25, 0.25],
[0.2, 0.2, 0.2, 0.4, 0.4, 0.2, 0.2, 0.2],
[0.25, 0.15, 0.1, 0.2, 0.2, 0.1, 0.15, 0.25],
[0.25, 0.3, 0.3, 0.0, 0.0, 0.3, 0.3, 0.25],
[0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2, 0.2]]
piecePositionScores = {"wN": knightScores,
"bN": knightScores[::-1],
"wB": bishopScores,
"bB": bishopScores[::-1],
"wQ": queenScores,
"bQ": queenScores[::-1],
"wR": rookScores,
"bR": rookScores[::-1],
"wp": pawnScores,
"bp": pawnScores[::-1]}
CHECKMATE=1000
STALEMATE=0
DEPTH=4
def findRandomMove(validMoves):
return validMoves[random.randint(0,len(validMoves)-1)]
def findBestMoveMinMaxNoRecursion(gs,validMoves):
turnMultiplier=1 if gs.whiteToMove else -1
opponentMinMaxScore=CHECKMATE
bestPlayerMove=None
random.shuffle(validMoves)
for playerMove in validMoves:
gs.makeMove(playerMove)
opponentsMoves=gs.getValidMoves()
if gs.staleMate:
opponentMaxScore=STALEMATE
elif gs.checkMate:
opponentMaxScore=-CHECKMATE
else:
opponentMaxScore=-CHECKMATE
for opponentMove in opponentsMoves:
gs.makeMove(opponentMove)
gs.getValidMoves()
if gs.checkMate:
score=CHECKMATE
elif gs.staleMate:
score=STALEMATE
else:
score=scoreMaterial(gs.board)*(-turnMultiplier)
if score>opponentMaxScore:
opponentMaxScore=score
gs.undoMove()
if opponentMaxScore<opponentMinMaxScore:
opponentMinMaxScore=opponentMaxScore
bestPlayerMove=playerMove
gs.undoMove()
return bestPlayerMove
#helper
def findBestMove(gs,validMoves,returnQueue):
global nextMove,counter
counter=0
nextMove=None
random.shuffle(validMoves)
# findMoveMinMax(gs,validMoves,DEPTH,gs.whiteToMove)
# findMoveNegaMax(gs,validMoves,DEPTH,1 if gs.whiteToMove else -1)
findMoveNegaMaxAlphaBeta(gs,validMoves,DEPTH,-CHECKMATE,CHECKMATE,1 if gs.whiteToMove else -1)
print(counter)
returnQueue.put(nextMove)
def findMoveMinMax(gs,validMoves,depth,whiteToMove):
global nextMove,counter
counter+=1
if depth==0:
return scoreMaterial(gs.board)
if whiteToMove:
maxScore=-CHECKMATE
for move in validMoves:
gs.makeMove(move)
nextMoves=gs.getValidMoves()
score=findMoveMinMax(gs,nextMoves,depth-1,False)
if score>maxScore:
maxScore=score
if depth==DEPTH:
nextMove=move
gs.undoMove()
return maxScore
else:
minScore=CHECKMATE
for move in validMoves:
gs.makeMove(move)
nextMoves=gs.getValidMoves()
score=findMoveMinMax(gs,nextMoves,depth-1,True)
if score<minScore:
minScore=score
if depth==DEPTH:
nextMove=move
gs.undoMove()
return minScore
def findMoveNegaMax(gs,validMoves,depth,turnMultiplier):
global nextMove,counter
counter+=1
if depth==0:
return turnMultiplier*scoreBoard(gs)
maxScore=-CHECKMATE
for move in validMoves:
gs.makeMove(move)
nextMoves=gs.getValidMoves()
score=-findMoveNegaMax(gs,nextMoves,depth-1,-turnMultiplier)
if score>maxScore:
maxScore=score
if depth==DEPTH:
nextMove=move
gs.undoMove()
return maxScore
def findMoveNegaMaxAlphaBeta(gs,validMoves,depth,alpha,beta,turnMultiplier):
global nextMove,counter
counter+=1
if depth==0:
return turnMultiplier*scoreBoard(gs)
#move ordering or move ranking
maxScore=-CHECKMATE
for move in validMoves:
gs.makeMove(move)
nextMoves=gs.getValidMoves()
score=-findMoveNegaMaxAlphaBeta(gs,nextMoves,depth-1,-beta,-alpha,-turnMultiplier)
if score>maxScore:
maxScore=score
if depth==DEPTH:
nextMove=move
gs.undoMove()
if maxScore>alpha:
alpha=maxScore
if alpha >=beta:
break
return maxScore
# Score the Board based on material.
def scoreMaterial(board):
score=0
for row in board:
for square in row:
if square[0]=='w':
score +=pieceScore[square[1]]
elif square[0]=='b':
score -=pieceScore[square[1]]
return score
#positive score is good for white,a negative score is good for black
def scoreBoard(gs):
if gs.checkMate:
if gs.whiteToMove:
return -CHECKMATE # black wins
else:
return CHECKMATE # white wins
elif gs.staleMate:
return STALEMATE
score = 0
for row in range(len(gs.board)):
for col in range(len(gs.board[row])):
piece = gs.board[row][col]
if piece != "--":
piecePositionScore = 0
if piece[1] != "K":
piecePositionScore = piecePositionScores[piece][row][col]
if piece[0] == "w":
score += pieceScore[piece[1]] + piecePositionScore
if piece[0] == "b":
score -= pieceScore[piece[1]] + piecePositionScore
return score