-
Notifications
You must be signed in to change notification settings - Fork 1
/
ai_player.py
326 lines (288 loc) · 11.1 KB
/
ai_player.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
# !/usr/bin/python
# -*- coding: utf-8 -*-
from copy import deepcopy
import time
class AIPlayer:
"""
AI 玩家
"""
def __init__(self, color):
"""
玩家初始化
:param color: 下棋方,'X' - 黑棋,'O' - 白棋
"""
self.color = color
if self.color == 'O':
self.op_color = 'X'
else:
self.op_color = 'O'
self.second_weight = [[45, 2, 9, 9, 9, 9, 2, 45],
[2, 0, 3, 3, 3, 3, 0, 2],
[9, 3, 5, 5, 5, 5, 3, 9],
[9, 3, 5, 1, 1, 5, 3, 9],
[9, 3, 5, 1, 1, 5, 3, 9],
[9, 3, 5, 5, 5, 5, 3, 9],
[2, 0, 3, 3, 3, 3, 0, 2],
[45, 2, 9, 9, 9, 9, 2, 45]]
self.start = 0
self.base_stable_score = 10
self.corner_score = 80
self.s_corner_score = 40
self.s_line_score = 20
def get_move(self, board):
"""
根据当前棋盘状态获取最佳落子位置
:param board: 棋盘
:return: action 最佳落子位置, e.g. 'A1'
"""
if self.color == 'X':
player_name = '黑棋'
else:
player_name = '白棋'
print("请等一会,对方 {}-{} 正在思考中...".format(player_name, self.color))
virtual_board = deepcopy(board)
self.start = time.clock()
action, value = self.max_value(virtual_board, -65, 65, 0)
return action
def heuristic(self, virtual_board, flexibility, op_flexibility):
board = virtual_board._board
value = 0
s1, s2 = virtual_board.count(self.op_color), virtual_board.count(self.color)
s_sum = s1 + s2
s_diff = s1 - s2
if board[0][0] == self.color:
value += self.corner_score
for i in range(1, 8):
if board[0][i] == self.color:
value += self.base_stable_score
else:
break
for i in range(1, 8):
if board[i][0] == self.color:
value += self.base_stable_score
else:
break
else:
if board[1][1] == self.color:
value -= self.s_corner_score
if board[0][1] == self.color:
value -= self.s_line_score
if board[1][0] == self.color:
value -= self.s_line_score
if board[7][0] == self.color:
value += self.corner_score
for i in range(1, 8):
if board[7][i] == self.color:
value += self.base_stable_score
else:
break
for i in range(6, -1, -1):
if board[i][0] == self.color:
value += self.base_stable_score
else:
break
else:
if board[6][1] == self.color:
value -= self.s_corner_score
if board[7][1] == self.color:
value -= self.s_line_score
if board[6][0] == self.color:
value -= self.s_line_score
if board[0][7] == self.color:
value += self.corner_score
for i in range(6, -1, -1):
if board[0][i] == self.color:
value += self.base_stable_score
else:
break
for i in range(1, 8):
if board[i][7] == self.color:
value += self.base_stable_score
else:
break
else:
if board[1][6] == self.color:
value -= self.s_corner_score
if board[0][6] == self.color:
value -= self.s_line_score
if board[1][7] == self.color:
value -= self.s_line_score
if board[7][7] == self.color:
value += self.corner_score
for i in range(6, -1, -1):
if board[7][i] == self.color:
value -= self.base_stable_score
else:
break
for i in range(6, -1, -1):
if board[i][7] == self.color:
value += self.base_stable_score
else:
break
else:
if board[6][6] == self.color:
value -= self.s_corner_score
if board[7][6] == self.color:
value -= self.s_line_score
if board[6][7] == self.color:
value -= self.s_line_score
if board[0][0] == self.op_color:
value -= self.corner_score
for i in range(1, 8):
if board[0][i] == self.op_color:
value -= self.base_stable_score
else:
break
for i in range(1, 8):
if board[i][0] == self.op_color:
value -= self.base_stable_score
else:
break
else:
if board[1][1] == self.op_color:
value += self.s_corner_score
if board[0][1] == self.op_color:
value += self.s_line_score
if board[1][0] == self.op_color:
value += self.s_line_score
if board[7][0] == self.op_color:
value -= self.corner_score
for i in range(1, 8):
if board[7][i] == self.op_color:
value -= self.base_stable_score
else:
break
for i in range(6, -1, -1):
if board[i][0] == self.op_color:
value -= self.base_stable_score
else:
break
else:
if board[6][1] == self.op_color:
value += self.s_corner_score
if board[7][1] == self.op_color:
value += self.s_line_score
if board[6][0] == self.op_color:
value += self.s_line_score
if board[0][7] == self.op_color:
value -= self.corner_score
for i in range(6, -1, -1):
if board[0][i] == self.op_color:
value -= self.base_stable_score
else:
break
for i in range(1, 8):
if board[i][7] == self.op_color:
value -= self.base_stable_score
else:
break
else:
if board[1][6] == self.op_color:
value += self.s_corner_score
if board[0][6] == self.op_color:
value += self.s_line_score
if board[1][7] == self.op_color:
value += self.s_line_score
if board[7][7] == self.op_color:
value -= self.corner_score
for i in range(6, -1, -1):
if board[7][i] == self.op_color:
value -= self.base_stable_score
else:
break
for i in range(6, -1, -1):
if board[i][7] == self.op_color:
value -= self.base_stable_score
else:
break
else:
if board[6][6] == self.op_color:
value += self.s_corner_score
if board[7][6] == self.op_color:
value += self.s_line_score
if board[6][7] == self.op_color:
value += self.s_line_score
if flexibility == 0:
flexibility = -100
if s_sum < 46:
value += int(2.3 * flexibility) - op_flexibility
if s_sum < 30:
value += s_diff
else:
for i in range(8):
for j in range(8):
if board[i][j] == self.color:
value += self.second_weight[i][j]
elif board[i][j] == self.op_color:
value -= self.second_weight[i][j]
return value / 3000
def max_value(self, virtual_board, alpha, beta, depth):
list_x = list(virtual_board.get_legal_actions('X'))
list_o = list(virtual_board.get_legal_actions('O'))
if len(list_x) == 0 and len(list_o) == 0:
return None, virtual_board.count(self.color) - virtual_board.count(self.op_color)
v = -65
final_action = None
if self.color == 'X':
current_color = 'X'
legal_list = list_x
op_legal_list = list_o
else:
current_color = 'O'
legal_list = list_o
op_legal_list = list_x
if len(legal_list) == 0:
return self.min_value(virtual_board, alpha, beta, depth + 1)
s_sum = virtual_board.count('X') + virtual_board.count('O')
if time.clock() - self.start >= 57.5 or (depth >= 6 and s_sum < 57):
return None, self.heuristic(virtual_board, len(legal_list), len(op_legal_list))
self.list_sort(virtual_board, legal_list)
for action in legal_list:
flipped = virtual_board._move(action, current_color)
temp_min_value = self.min_value(virtual_board, alpha, beta, depth + 1)[1]
virtual_board.backpropagation(action, flipped, current_color)
if temp_min_value > v:
final_action = action
v = temp_min_value
if v >= beta:
return None, v
if v > alpha:
alpha = v
return final_action, v
def min_value(self, virtual_board, alpha, beta, depth):
list_x = list(virtual_board.get_legal_actions('X'))
list_o = list(virtual_board.get_legal_actions('O'))
if len(list_x) == 0 and len(list_o) == 0:
return None, virtual_board.count(self.color) - virtual_board.count(self.op_color)
v = 65
final_action = None
if self.color == 'X':
current_color = 'O'
legal_list = list_o
else:
current_color = 'X'
legal_list = list_x
if len(legal_list) == 0:
return self.max_value(virtual_board, alpha, beta, depth + 1)
self.list_sort(virtual_board, legal_list)
for action in legal_list:
flipped = virtual_board._move(action, current_color)
temp_max_value = self.max_value(virtual_board, alpha, beta, depth + 1)[1]
virtual_board.backpropagation(action, flipped, current_color)
if temp_max_value < v:
final_action = action
v = temp_max_value
if v <= alpha:
return None, v
if v < beta:
beta = v
return final_action, v
def list_sort(self, virtual_board, legal_list):
for i in range(len(legal_list)):
for j in range(len(legal_list) - i - 1):
x1, y1 = virtual_board.board_num(legal_list[j])
x2, y2 = virtual_board.board_num(legal_list[j + 1])
if self.second_weight[x1][y1] < self.second_weight[x2][y2]:
tmp = legal_list[j]
legal_list[j] = legal_list[j + 1]
legal_list[j + 1] = tmp