-
Notifications
You must be signed in to change notification settings - Fork 0
/
ai.py
223 lines (178 loc) · 5.89 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
from chess import BLACK, WHITE
INF = 10 ** 9
PAWN_EVAL_WHITE = [
[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]
]
PAWN_EVAL_BLACK = list(reversed(PAWN_EVAL_WHITE))
KNIGHT_EVAL = [
[-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, 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))
QUEEN_EVAL = [
[-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]
]
KING_EVAL_WHITE = [
[-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]
]
KING_EVAL_BLACK = list(reversed(KING_EVAL_WHITE))
PIECE_EVALS = {
'P': {
WHITE: PAWN_EVAL_WHITE,
BLACK: PAWN_EVAL_BLACK
},
'K': {
WHITE: KING_EVAL_WHITE,
BLACK: KING_EVAL_BLACK
},
'Q': {
WHITE: QUEEN_EVAL,
BLACK: QUEEN_EVAL
},
'R': {
WHITE: ROOK_EVAL_WHITE,
BLACK: ROOK_EVAL_BLACK
},
'N': {
WHITE: KNIGHT_EVAL,
BLACK: KNIGHT_EVAL
},
'B': {
WHITE: BISHOP_EVAL_WHITE,
BLACK: BISHOP_EVAL_BLACK
}
}
PIECE_COSTS = {
'P': 10,
'K': 900,
'Q': 90,
'R': 50,
'N': 30,
'B': 30,
}
def get_piece_eval(piece, pos):
if piece is None:
return 0
row, col = pos
char = piece.raw_char()
color = piece.get_color()
ev = PIECE_EVALS[char]
table = ev[color]
return table[row][col]
def get_piece_cost(piece):
if piece is None:
return 0
char = piece.raw_char()
return PIECE_COSTS[char]
def get_absolute_value(piece, pos):
if piece is None:
return 0
return get_piece_cost(piece) + get_piece_eval(piece, pos)
def get_piece_value(piece, pos):
if piece is None:
return 0
value = get_absolute_value(piece, pos)
color = piece.get_color()
if color == BLACK:
value = -1 * value
return value
def evaluate_board(board):
totalEvaluation = 0
for i, row in enumerate(board):
for j, cell in enumerate(row):
totalEvaluation = totalEvaluation + get_piece_value(cell, (i, j))
return totalEvaluation
def minimax_eval(depth_left, game, alpha, beta, is_maximising_player):
if depth_left == 0:
return evaluate_board(game.get_board())
# if is_maximising_player:
# return evaluate_board(game.get_board())
# else:
# return evaluate_board(game.get_board()) * -1
new_game_moves = game.moves_without_check()
if is_maximising_player:
for move in new_game_moves:
game.make_move(*move, True)
score = minimax_eval(depth_left - 1, game, alpha, beta, not is_maximising_player)
game.undo(True)
if score >= beta:
return beta
if score > alpha:
alpha = score
return alpha
else:
for move in new_game_moves:
game.make_move(*move, True)
score = minimax_eval(depth_left - 1, game, alpha, beta, not is_maximising_player)
game.undo(True)
if score <= alpha:
return alpha
if score < beta:
beta = score
return beta
def minimax_root(depth_left, game, is_maximising_player):
new_game_moves = game.moves_without_check()
best_move_found = new_game_moves[0]
if is_maximising_player:
best_move = -INF
else:
best_move = INF
for move in new_game_moves:
game.make_move(*move, True)
value = minimax_eval(depth_left - 1, game, -INF, INF, not is_maximising_player)
game.undo()
if value > best_move and is_maximising_player:
best_move = value
best_move_found = move
elif value < best_move and not is_maximising_player:
best_move = value
best_move_found = move
return best_move_found