-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathai.py
448 lines (365 loc) · 14.7 KB
/
ai.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
"""
LIGHT and DARK AI BOTS
Press 'A' on the keyboard to activate/deactivate AI mode (Light VS Dark)
"""
import random
from math import inf
from engine import Move
# dictionary of light pieces on the board (keys = "row,col"; values = chess piece eg "kl")
light_pieces = {}
# dictionary of dark pieces on the board (keys = "row,col"; values = chess piece eg "kl")
dark_pieces = {}
piece_values = {
"kl": 20000, "ql": 900, "rl": 500, "bl": 330, "nl": 320, "pl": 100, " ": 0,
"kd": -20000, "qd": -900, "rd": -500, "bd": -330, "nd": -320, "pd": -100
}
square_values = {
"pl": [
[55, 55, 55, 55, 55, 55, 55, 55],
[50, 50, 50, 50, 50, 50, 50, 50],
[10, 10, 20, 30, 30, 20, 10, 10],
[5, 5, 10, 25, 25, 10, 5, 5],
[0, 0, 0, 20, 20, 0, 0, 0],
[5, -5, -10, 0, 0, -10, -5, 5],
[5, 10, 10, -20, -20, 10, 10, 5],
[0, 0, 0, 0, 0, 0, 0, 0]
],
"nl": [
[-50, -40, -30, -30, -30, -30, -40, -50],
[-40, -20, 0, 0, 0, 0, -20, -40],
[-30, 0, 10, 15, 15, 10, 0, -30],
[-30, 5, 15, 20, 20, 15, 5, -30],
[-30, 0, 15, 20, 20, 15, 0, -30],
[-30, 5, 10, 15, 15, 10, 5, -30],
[-40, -20, 0, 5, 5, 0, -20, -40],
[-50, -40, -30, -30, -30, -30, -40, -50]
],
"bl": [
[-20, -10, -10, -10, -10, -10, -10, -20],
[-10, 0, 0, 0, 0, 0, 0, -10],
[-10, 0, 5, 10, 10, 5, 0, -10],
[-10, 5, 5, 10, 10, 5, 5, -10],
[-10, 0, 10, 10, 10, 10, 0, -10],
[-10, 10, 10, 10, 10, 10, 10, -10],
[-10, 5, 0, 0, 0, 0, 5, -10],
[-20, -10, -10, -10, -10, -10, -10, -20]
],
"rl": [
[0, 0, 0, 0, 0, 0, 0, 0],
[5, 10, 10, 10, 10, 10, 10, 5],
[-5, 0, 0, 0, 0, 0, 0, -5],
[-5, 0, 0, 0, 0, 0, 0, -5],
[-5, 0, 0, 0, 0, 0, 0, -5],
[-5, 0, 0, 0, 0, 0, 0, -5],
[-5, 0, 0, 0, 0, 0, 0, -5],
[0, 0, 0, 5, 5, 0, 0, 0]
],
"ql": [
[-20, -10, -10, -5, -5, -10, -10, -20],
[-10, 0, 0, 0, 0, 0, 0, -10],
[-10, 0, 5, 5, 5, 5, 0, -10],
[-5, 0, 5, 5, 5, 5, 0, -5],
[0, 0, 5, 5, 5, 5, 0, -5],
[-10, 5, 5, 5, 5, 5, 0, -10],
[-10, 0, 5, 0, 0, 0, 0, -10],
[-20, -10, -10, -5, -5, -10, -10, -20]
],
# middle game
"kl": [
[-30, -40, -40, -50, -50, -40, -40, -30],
[-30, -40, -40, -50, -50, -40, -40, -30],
[-30, -40, -40, -50, -50, -40, -40, -30],
[-30, -40, -40, -50, -50, -40, -40, -30],
[-20, -30, -30, -40, -40, -30, -30, -20],
[-10, -20, -20, -20, -20, -20, -20, -10],
[20, 20, 0, 0, 0, 0, 20, 20],
[20, 30, 10, 0, 0, 10, 30, 20]
],
# end game
# "kl":[
# [-50,-40,-30,-20,-20,-30,-40,-50],
# [-30,-20,-10, 0, 0,-10,-20,-30],
# [-30,-10, 20, 30, 30, 20,-10,-30],
# [-30,-10, 30, 40, 40, 30,-10,-30],
# [-30,-10, 30, 40, 40, 30,-10,-30],
# [-30,-10, 20, 30, 30, 20,-10,-30],
# [-30,-30, 0, 0, 0, 0,-30,-30],
# [-50,-30,-30,-30,-30,-30,-30,-50]
# ]
" ": [
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0],
[0, 0, 0, 0, 0, 0, 0, 0]
],
# middle game
"kd": [
[-20, -30, -10, 0, 0, -10, -30, -20],
[-20, -20, 0, 0, 0, 0, -20, -20],
[10, 20, 20, 20, 20, 20, 20, 10],
[20, 30, 30, 40, 40, 30, 30, 20],
[30, 40, 40, 50, 50, 40, 40, 30],
[30, 40, 40, 50, 50, 40, 40, 30],
[30, 40, 40, 50, 50, 40, 40, 30],
[30, 40, 40, 50, 50, 40, 40, 30]
],
"pd": [
[0, 0, 0, 0, 0, 0, 0, 0],
[-5, -10, -10, 20, 20, -10, -10, -5],
[-5, 5, 10, 0, 0, 10, 5, 5],
[0, 0, 0, -20, -20, 0, 0, 0],
[-5, -5, -10, -25, -25, -10, -5, -5],
[-10, -10, -20, -30, -30, -20, -10, -10],
[-50, -50, -50, -50, -50, -50, -50, -50],
[-55, -55, -55, -55, -55, -55, -55, -55]
],
"nd": [
[50, 40, 30, 30, 30, 30, 40, 50],
[40, 20, 0, -5, -5, 0, 20, 40],
[30, -5, -10, -15, -15, -10, -5, 30],
[30, 0, -15, -20, -20, -15, 0, 30],
[30, -5, -15, -20, -20, -15, -5, 30],
[30, -0, -10, -15, -15, -10, 0, 30],
[40, 20, 0, 0, 0, 0, 20, 40],
[50, 40, 30, 30, 30, 30, 40, 50]
],
"bd": [
[20, 10, 10, 10, 10, 10, 10, 20],
[10, -5, 0, 0, 0, 0, -5, 10],
[10, -10, -10, -10, -10, -10, -10, 10],
[10, 0, -10, -10, -10, -10, 0, 10],
[10, -5, -5, -10, -10, -5, -5, 10],
[10, 0, -5, -10, -10, -5, 0, 10],
[10, 0, 0, 0, 0, 0, 0, 10],
[20, 10, 10, 10, 10, 10, 10, 20]
],
"rd": [
[0, 0, 0, -5, -5, 0, 0, 0],
[5, 0, 0, 0, 0, 0, 0, 5],
[5, 0, 0, 0, 0, 0, 0, 5],
[5, 0, 0, 0, 0, 0, 0, 5],
[5, 0, 0, 0, 0, 0, 0, 5],
[5, 0, 0, 0, 0, 0, 0, 5],
[-5, -10, -10, -10, -10, -10, -10, -5],
[0, 0, 0, 0, 0, 0, 0, 0]
],
"qd": [
[20, 10, 10, 5, 5, 10, 10, 20],
[10, 0, 5, 0, 0, 0, 0, 10],
[10, -5, -5, -5, -5, -5, 0, 10],
[0, 0, -5, -5, -5, -5, 0, 5],
[5, 0, -5, -5, -5, -5, 0, 5],
[10, 0, -5, -5, -5, -5, 0, 10],
[10, 0, 0, 0, 0, 0, 0, 10],
[20, 10, 10, 5, 5, 10, 10, 20]
]
}
def order_moves(moves):
"""
Order moves
input parameter(s):
move --> List of moves to be ordered
return parameter(s):
moves_ordered --> List of moves after ordering
"""
moves_ordered = [] # list to store ordered moves
# capturing moves
for move in moves:
if (move.piece_captured != " ") and (move not in moves_ordered):
moves_ordered.append(move)
# pawn promotion
for move in moves:
if (move.piece_moved[0] == "p") and (move.end_row == 0 or move.end_row == 7):
if move not in moves_ordered:
moves_ordered.append(move)
# Advancing moves
for move in moves:
if ((move.piece_moved[1] == "l") and (move.end_row < move.start_row)) or \
((move.piece_moved[1] == "d") and (move.end_row > move.start_row)):
if move not in moves_ordered:
moves_ordered.append(move)
# remaining moves
for move in moves:
if move not in moves_ordered:
moves_ordered.append(move)
return moves_ordered
def evaluate(board):
"""
Evaluates chess board
input parameter(s):
board --> The chess board to be evaluated
return parameter(s):
score --> The board evaluation
"""
score = 0
for i in range(len(board)):
for j in range(len(board[i])):
# Add piece value and it's current square value (A Queen on d4 will be worth 900 + 5)
piece_value = piece_values[board[i][j]] + \
square_values[board[i][j]][i][j]
# Add piece value to overall board score
score += piece_value
return score
def minimax(game_state, depth, alpha=-inf, beta=inf):
"""
Determine best move to play
input parameter(s):
game_state --> Game_state object
depth --> The number of moves ahead to check before deciding best move
alpha --> alpha value to use for alpha beta pruning. Default is -inf
beta --> beta value to use for alpha beta pruning. Default is inf
return parameter(s):
best_move --> The best move to play
max_eval --> Evaluation of best_move
"""
# Breaking condition
if (depth == 0) or (game_state.check_mate) or (game_state.stale_mate):
return None, evaluate(game_state.board)
# Get valid moves
moves = game_state.get_valid_moves()[0]
moves_ordered = [] # List to store moves after ordering
if moves: # If there are valid moves
moves_ordered = order_moves(moves)
# Select a random move as best_move
best_move = random.choice(moves_ordered)
else: # If there are no valid moves
return None, evaluate(game_state.board)
if game_state.light_to_move: # If it is light's turn to play
max_eval = -inf
for move in moves_ordered:
game_state.make_move(move, True) # make move in look_ahead mode
# Call minimax on all possible moves after above move
current_eval = minimax(game_state, depth-1, alpha, beta)[1]
game_state.undo_move(True)
# Update best_move and current_eval
if current_eval > max_eval:
max_eval = current_eval
best_move = move
# alpha beta pruning
alpha = max(alpha, current_eval)
if beta <= alpha:
break
return best_move, max_eval
else: # If it is light's turn to play
min_eval = inf
for move in moves_ordered:
game_state.make_move(move, True) # make move in look_ahead mode
# Call minimax on all possible moves after above move
current_eval = minimax(game_state, depth-1, alpha, beta)[1]
game_state.undo_move(True)
# Update best_move and current_eval
if current_eval < min_eval:
min_eval = current_eval
best_move = move
# alpha beta pruning
beta = min(beta, current_eval)
if beta <= alpha:
break
return best_move, min_eval
def ai_light_move(gs):
"""
makes automated valid light moves
input parameter(s):
gs --> Game_state object
return parameter(s):
light_move --> move made for light team
gs --> Game_state object
"""
### TODO: edit to your unique algorithm (mini-max w/ pruning, etc) ###
### Static evaluation of the board can be done with 'light_pieces' and 'dark_pieces' dictionaries ###
light_move = None
move = minimax(gs, 3)[0] # Move as decided by minimax
if move:
# create move copy (only copy (start_row, start_col) & (end_row, end_col) of move object)
light_move = Move((move.start_row, move.start_col),
(move.end_row, move.end_col), gs.board)
gs.make_move(light_move) # make move
# handles pawn promotion
if (light_move.end_row == 0) and (light_move.piece_moved[0] == "p"):
gs.board[light_move.end_row][light_move.end_col] = random.choice(
("ql", "rl", "bl", "nl")) # randomly select promotion piece
# update light pieces position dictionary
# light_pieces.pop("{},{}".format(light_move.start_row, light_move.start_col))
# light_pieces["{},{}".format(light_move.end_row, light_move.end_col)] = light_move.piece_moved
# # remove pieces captured from dark_piece dictionary for faster static board evaluation in your mini-max algorithm rewrite
# if light_move.piece_captured != " " and not light_move.en_passant_captured:
# dark_pieces.pop("{},{}".format(light_move.end_row, light_move.end_col))
# elif light_move.en_passant_captured:
# dark_pieces.pop("{},{}".format(light_move.end_row+1, light_move.end_col if gs.light_to_move else light_move.end_row-1, light_move.end_col))
else:
if gs.is_in_check():
gs.check_mate = True
else:
gs.stale_mate = True
return light_move, gs
def ai_dark_move(gs):
"""
makes automated valid dark moves
input parameter(s):
gs --> Game_state object
return parameter(s):
dark_move --> move made for dark team
gs --> Game_state object
"""
### TODO: edit to your unique algorithm (mini-max w/ pruning, etc) ###
### Static evaluation of the board can be done with 'light_pieces' and 'dark_pieces' dictionaries ###
dark_move = None
move = minimax(gs, 2)[0] # Move as decided by minimax
if move:
# create move copy (only copy (start_row, start_col) & (end_row, end_col) of move object)
dark_move = Move((move.start_row, move.start_col),
(move.end_row, move.end_col), gs.board)
gs.make_move(dark_move) # make move
# handles pawn promotion
if (dark_move.end_row == 7) and (dark_move.piece_moved[0] == "p"):
gs.board[dark_move.end_row][dark_move.end_col] = random.choice(
("qd", "rd", "bd", "nd")) # randomly select promotion piece
# update dark pieces position dictionary
# dark_pieces.pop("{},{}".format(dark_move.start_row, dark_move.start_col))
# dark_pieces["{},{}".format(dark_move.end_row, dark_move.end_col)] = dark_move.piece_moved
# # remove pieces captured from light_piece dictionary for faster static board evaluation in your mini-max algorithm rewrite
# if dark_move.piece_captured != " " and not dark_move.en_passant_captured:
# light_pieces.pop("{},{}".format(dark_move.end_row, dark_move.end_col))
# elif dark_move.en_passant_captured:
# light_pieces.pop("{},{}".format(dark_move.end_row+1, dark_move.end_col if gs.light_to_move else dark_move.end_row-1, dark_move.end_col))
else:
if gs.is_in_check():
gs.check_mate = True
else:
gs.stale_mate = True
return dark_move, gs
def ai_move(gs):
"""
determines the turn for both team's AI
also saves a running memory of the board state with moves and captures
input parameter(s):
gs --> Game_state object
return parameter(s):
move --> dark or light AI move
gs --> Game_state object
"""
if not light_pieces:
for i in range(len(gs.board)):
for j in range(len(gs.board[i])):
if gs.board[i][j][1] == "l":
light_pieces["{},{}".format(i, j)] = gs.board[i][j]
if not dark_pieces:
for i in range(len(gs.board)):
for j in range(len(gs.board[i])):
if gs.board[i][j][1] == "d":
dark_pieces["{},{}".format(i, j)] = gs.board[i][j]
return ai_light_move(gs) if gs.light_to_move else ai_dark_move(gs)
def ai_reset():
"""
resets the light pieces and dark pieces dictionaries when AI mode is activated/deactivated (in case moves were made outside AI mode)
input parameter(s):
None
output parameter(s):
None
"""
light_pieces.clear()
dark_pieces.clear()