-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmyBot.py
267 lines (239 loc) · 7.78 KB
/
myBot.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
import random
import os
from ChessEngine import GameState, Move
pieceScore = {"K": 0, "Q": 9, "R": 5, "B": 3, "N": 3, "P": 1}
knightScore = [
[1, 1, 1, 1, 1, 1, 1, 1],
[1, 2, 2, 2, 2, 2, 2, 1],
[1, 2, 3, 3, 3, 3, 2, 1],
[1, 2, 3, 4, 4, 3, 2, 1],
[1, 2, 3, 4, 4, 3, 2, 1],
[1, 2, 3, 3, 3, 3, 2, 1],
[1, 2, 2, 2, 2, 2, 2, 1],
[1, 1, 1, 1, 1, 1, 1, 1],
]
bishopScore = [
[4, 3, 2, 1, 1, 2, 3, 4],
[3, 4, 3, 2, 2, 3, 4, 3],
[2, 3, 4, 3, 3, 4, 3, 2],
[1, 2, 3, 4, 4, 3, 2, 1],
[1, 2, 3, 4, 4, 3, 2, 1],
[2, 3, 4, 3, 3, 4, 3, 2],
[3, 4, 3, 2, 2, 3, 4, 3],
[4, 3, 2, 1, 1, 2, 3, 4],
]
queenScore = [
[1, 1, 1, 3, 1, 1, 1, 1],
[1, 2, 3, 3, 3, 1, 1, 1],
[1, 4, 3, 3, 3, 4, 2, 1],
[1, 2, 3, 3, 3, 2, 2, 1],
[1, 2, 3, 3, 3, 2, 2, 1],
[1, 4, 3, 3, 3, 4, 2, 1],
[1, 1, 2, 3, 3, 1, 1, 1],
[1, 1, 1, 3, 1, 1, 1, 1],
]
#probably better try to place rooks on open files, or on same sile as other rook/Queen
rookScore = [
[4, 3, 4, 4, 4, 4, 3, 4],
[4, 4, 4, 4, 4, 4, 4, 4],
[1, 1, 2, 3, 3, 2, 1, 1],
[1, 2, 3, 4, 4, 3, 2, 1],
[1, 2, 3, 4, 4, 3, 2, 1],
[1, 1, 2, 3, 3, 2, 1, 1],
[4, 4, 4, 4, 4, 4, 4, 4],
[4, 3, 4, 4, 4, 4, 3, 4],
]
whitePawnScore = [
[8, 8, 8, 8, 8, 8, 8, 8],
[8, 8, 8, 8, 8, 8, 8, 8],
[5, 6, 6, 7, 7, 6, 6, 5],
[2, 3, 3, 5, 5, 3, 3, 2],
[1, 2, 3, 4, 4, 3, 2, 1],
[1, 1, 2, 3, 3, 2, 1, 1],
[1, 1, 1, 0, 0, 1, 1, 1],
[0, 0, 0, 0, 0, 0, 0, 0],
]
blackPawnScore = [
[0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 0, 0, 1, 1, 1],
[1, 1, 2, 3, 3, 2, 1, 1],
[1, 2, 3, 4, 4, 3, 2, 1],
[2, 3, 3, 5, 5, 3, 3, 2],
[5, 6, 6, 7, 7, 6, 6, 5],
[8, 8, 8, 8, 8, 8, 8, 8],
[8, 8, 8, 8, 8, 8, 8, 8],
]
piecePositionScores = {'N': knightScore, 'B': bishopScore, 'Q': queenScore, 'R': rookScore, 'wP': whitePawnScore, 'bP': blackPawnScore}
CHECKMATE = 1000
STALEMATE = 0
DEPTH = 4
'''
Picks and returns a random move
'''
def findRandomMove(gs, validMoves):
return validMoves[random.randint(0, len(validMoves)-1)]
#Assuming balck is the computer
def greedyKillerMachine(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 opponentsMove in opponentsMoves:
gs.makeMove(opponentsMove)
gs.getValidMoves()
if gs.checkMate:
score = CHECKMATE
elif gs.staleMate:
score = STALEMATE
else:
score = -turnMultiplier*scoreMaterial(gs.board)
if score > opponentMaxScore:
opponentMaxScore = score
gs.undoMove()
if opponentMaxScore < opponentMinMaxScore:
opponentMinMaxScore = opponentMaxScore
bestPlayerMove = playerMove
gs.undoMove()
return bestPlayerMove
'''
Find the best move based on material alone
'''
def findBestMoveGKM(gs, validMoves):
return greedyKillerMachine(gs, validMoves)
'''
Helper method to make the first recursive call
'''
def findBestMove(gs, validMoves, returnQueue):
global nextMove, counter
nextMove = None
counter = 0
# findMoveNegaMax(gs, validMoves, DEPTH, 1 if gs.whiteToMove else -1)
random.shuffle(validMoves)
findMoveNegaMaxAlphaBeta(gs, validMoves, DEPTH, 1 if gs.whiteToMove else -1, -CHECKMATE, CHECKMATE)
print(counter)
returnQueue.put(nextMove)
def findMoveMinMax(gs, validMoves, depth, whiteToMove):
global nextMove
if depth == 0:
return scoreMaterial(gs.board)
if random.randint(1, 10) <= 3:
return scoreMaterial(gs.board)
if whiteToMove:
maxScore = -CHECKMATE
for move in validMoves:
gs.makeMove(move)
nextMoves = gs.getValidMoves()
random.shuffle(nextMoves)
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()
random.shuffle(nextMoves)
score = findMoveMinMax(gs, nextMoves, depth-1, True)
if score < minScore:
minScore = score
if depth == DEPTH:
nextMove = move
gs.undoMove()
return minScore
def findMoveNegaMax(gs: GameState, validMoves: list[Move], depth: int, turnMultiplier: int):
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: GameState, validMoves: list[Move], depth: int, turnMultiplier: int, alpha: int, beta: int):
global nextMove, counter
counter += 1
if depth == 0:
return turnMultiplier*scoreBoard(gs)
#move ordering - implement later
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
print("--",move, score)
gs.undoMove()
if maxScore > alpha: #pruning happens
alpha = maxScore
if alpha >= beta:
break
return maxScore
'''
A positive score is good for the white player and a negative score is good for the black player
'''
def scoreBoard(gs: GameState):
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])):
square = gs.board[row][col]
if square != '--':
#score it positionally
piecePositionScore = 0
if square[1] != 'K':
if square[1] == 'P':
piecePositionScore = piecePositionScores[square][row][col]
else:
piecePositionScore = piecePositionScores[square[1]][row][col]
if square[0] == 'w':
score += pieceScore[square[1]] + piecePositionScore*0.1
elif square[0] == 'b':
score -= pieceScore[square[1]] - piecePositionScore*0.1
return score
'''
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
# from ChessEngine import GameState, Move, CastleRights
# gx = GameState()
# moves = gx.getAllPossibleMoves()
# print(moves)
# move = greedyKillerMachine(gx, moves)
# print(move.getChessNotation())