Repository navigation
Expand file tree
/
Copy pathAlpha_Beta.py
More file actions
331 lines (269 loc) · 12.7 KB
/
Copy pathAlpha_Beta.py
File metadata and controls
331 lines (269 loc) · 12.7 KB
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
import numpy as np
from state import UltimateTTT_Move, State
from state import State, State_2
from collections import defaultdict
import time
import copy
TIME_OUT = 5.5
start_time = 0
transposition_table = defaultdict(lambda: {"score": None, "depth": -1, "flag": None})
def check_win_condition(board):
row_sum = np.sum(board, 1)
col_sum = np.sum(board, 0)
diag_sum_top_left = board.trace()
diag_sum_top_right = board[::-1].trace()
player_one_wins = any(row_sum == 3) + any(col_sum == 3)
player_one_wins += (diag_sum_top_left == 3) + (diag_sum_top_right == 3)
if player_one_wins:
return 1
player_two_wins = any(row_sum == -3) + any(col_sum == -3)
player_two_wins += (diag_sum_top_left == -3) + (diag_sum_top_right == -3)
if player_two_wins:
return -1
return 0
def real_evaluate_position(board, row, col, player):
evaluation = 0
points = [0.2, 0.17, 0.2, 0.17, 0.22, 0.17, 0.2, 0.17, 0.2]
board[row, col] = player
evaluation += player * points[3 * row + col]
if board[0, 0] + board[0, 1] + board[0, 2] == 2 * player or board[1, 0] + board[1, 1] + board[1, 2] == 2 * player or \
board[2, 0] + \
board[2, 1] + board[2, 2] == 2 * player:
evaluation += player * 1
if board[0, 0] + board[1, 0] + board[2, 0] == 2 * player or board[0, 1] + board[1, 1] + board[2, 1] == 2 * player or \
board[0, 2] + \
board[1, 2] + board[2, 2] == 2 * player:
evaluation += player * 1
if board[0, 0] + board[1, 1] + board[2, 2] == 2 * player or board[0, 2] + board[1, 1] + board[2, 0] == 2 * player:
evaluation += player * 1
if board[0, 0] + board[0, 1] + board[0, 2] == 3 * player or board[1, 0] + board[1, 1] + board[1, 2] == 3 * player or \
board[2, 0] + \
board[2, 1] + board[2, 2] == 3 * player:
evaluation += player * 5
if board[0, 0] + board[1, 0] + board[2, 0] == 3 * player or board[0, 1] + board[1, 1] + board[2, 1] == 3 * player or \
board[0, 2] + \
board[1, 2] + board[2, 2] == 3 * player:
evaluation += player * 5
if board[0, 0] + board[1, 1] + board[2, 2] == 3 * player or board[0, 2] + board[1, 1] + board[2, 0] == 3 * player:
evaluation += player * 5
board[row, col] = -player
if board[0, 0] + board[0, 1] + board[0, 2] == -3 * player or board[1, 0] + board[1, 1] + board[
1, 2] == -3 * player or board[2, 0] + \
board[2, 1] + board[2, 2] == -3 * player:
evaluation += player * 2
if board[0, 0] + board[1, 0] + board[2, 0] == -3 * player or board[0, 1] + board[1, 1] + board[
2, 1] == -3 * player or board[0, 2] + \
board[1, 2] + board[2, 2] == -3 * player:
evaluation += player * 2
if board[0, 0] + board[1, 1] + board[2, 2] == -3 * player or board[0, 2] + board[1, 1] + board[2, 0] == -3 * player:
evaluation += player * 2
board[row, col] = player
evaluation += check_win_condition(board) * 15
board[row, col] = 0
return evaluation
def real_evaluate_board(board):
evaluation = 0
points = [0.2, 0.17, 0.2, 0.17, 0.22, 0.17, 0.2, 0.17, 0.2]
for cell in range(9):
evaluation += board[cell // 3, cell % 3] * points[cell]
if board[0, 0] + board[0, 1] + board[0, 2] == 2 or board[1, 0] + board[1, 1] + board[1, 2] == 2 or board[2, 0] + \
board[2, 1] + board[2, 2] == 2:
evaluation += 6
if board[0, 0] + board[1, 0] + board[2, 0] == 2 or board[0, 1] + board[1, 1] + board[2, 1] == 2 or board[0, 2] + \
board[1, 2] + board[2, 2] == 2:
evaluation += 6
if board[0, 0] + board[1, 1] + board[2, 2] == 2 or board[0, 2] + board[1, 1] + board[2, 0] == 2:
evaluation += 7
if (board[0, 0] + board[0, 1] == -2 and board[0, 2] == 1) or (
board[0, 1] + board[0, 2] == -2 and board[0, 0] == 1) or (
board[0, 0] + board[0, 2] == -2 and board[0, 1] == 1):
evaluation += 9
if (board[1, 0] + board[1, 1] == -2 and board[1, 2] == 1) or (
board[1, 1] + board[1, 2] == -2 and board[1, 0] == 1) or (
board[1, 0] + board[1, 2] == -2 and board[1, 1] == 1):
evaluation += 9
if (board[2, 0] + board[2, 1] == -2 and board[2, 2] == 1) or (
board[2, 1] + board[2, 2] == -2 and board[2, 0] == 1) or (
board[2, 0] + board[2, 2] == -2 and board[2, 1] == 1):
evaluation += 9
if (board[0, 0] + board[1, 0] == -2 and board[2, 0] == 1) or (
board[1, 0] + board[2, 0] == -2 and board[0, 0] == 1) or (
board[0, 0] + board[2, 0] == -2 and board[1, 0] == 1):
evaluation += 9
if (board[0, 1] + board[1, 1] == -2 and board[2, 1] == 1) or (
board[1, 1] + board[2, 1] == -2 and board[0, 1] == 1) or (
board[0, 1] + board[2, 1] == -2 and board[1, 1] == 1):
evaluation += 9
if (board[0, 2] + board[1, 2] == -2 and board[2, 2] == 1) or (
board[1, 2] + board[2, 2] == -2 and board[0, 2] == 1) or (
board[0, 2] + board[2, 2] == -2 and board[1, 2] == 1):
evaluation += 9
if (board[0, 0] + board[1, 1] == -2 and board[2, 2] == 1) or (
board[1, 1] + board[2, 2] == -2 and board[0, 0] == 1) or (
board[0, 0] + board[2, 2] == -2 and board[1, 1] == 1):
evaluation += 9
if (board[0, 2] + board[1, 1] == -2 and board[2, 0] == 1) or (
board[1, 1] + board[2, 0] == -2 and board[0, 2] == 1) or (
board[0, 2] + board[2, 0] == -2 and board[1, 1] == 1):
evaluation += 9
if board[0, 0] + board[0, 1] + board[0, 2] == -2 or board[1, 0] + board[1, 1] + board[1, 2] == -2 or board[2, 0] + \
board[2, 1] + board[2, 2] == -2:
evaluation -= 6
if board[0, 0] + board[1, 0] + board[2, 0] == -2 or board[0, 1] + board[1, 1] + board[2, 1] == -2 or board[0, 2] + \
board[1, 2] + board[2, 2] == -2:
evaluation -= 6
if board[0, 0] + board[1, 1] + board[2, 2] == -2 or board[0, 2] + board[1, 1] + board[2, 0] == -2:
evaluation -= 7
if (board[0, 0] + board[0, 1] == 2 and board[0, 2] == -1) or (
board[0, 1] + board[0, 2] == 2 and board[0, 0] == -1) or (
board[0, 0] + board[0, 2] == 2 and board[0, 1] == -1):
evaluation -= 9
if (board[1, 0] + board[1, 1] == 2 and board[1, 2] == -1) or (
board[1, 1] + board[1, 2] == 2 and board[1, 0] == -1) or (
board[1, 0] + board[1, 2] == 2 and board[1, 1] == -1):
evaluation -= 9
if (board[2, 0] + board[2, 1] == 2 and board[2, 2] == -1) or (
board[2, 1] + board[2, 2] == 2 and board[2, 0] == -1) or (
board[2, 0] + board[2, 2] == 2 and board[2, 1] == -1):
evaluation -= 9
if (board[0, 0] + board[1, 0] == 2 and board[2, 0] == -1) or (
board[1, 0] + board[2, 0] == 2 and board[0, 0] == -1) or (
board[0, 0] + board[2, 0] == 2 and board[1, 0] == -1):
evaluation -= 9
if (board[0, 1] + board[1, 1] == 2 and board[2, 1] == -1) or (
board[1, 1] + board[2, 1] == 2 and board[0, 1] == -1) or (
board[0, 1] + board[2, 1] == 2 and board[1, 1] == -1):
evaluation -= 9
if (board[0, 2] + board[1, 2] == 2 and board[2, 2] == -1) or (
board[1, 2] + board[2, 2] == 2 and board[0, 2] == -1) or (
board[0, 2] + board[2, 2] == 2 and board[1, 2] == -1):
evaluation -= 9
if (board[0, 0] + board[1, 1] == 2 and board[2, 2] == -1) or (
board[1, 1] + board[2, 2] == 2 and board[0, 0] == -1) or (
board[0, 0] + board[2, 2] == 2 and board[1, 1] == -1):
evaluation -= 9
if (board[0, 2] + board[1, 1] == 2 and board[2, 0] == -1) or (
board[1, 1] + board[2, 0] == 2 and board[0, 2] == -1) or (
board[0, 2] + board[2, 0] == 2 and board[1, 1] == -1):
evaluation -= 9
evaluation += check_win_condition(board) * 12
return evaluation
def evaluation_function(state: State):
eval_mul = [1.4, 1, 1.4, 1, 1.75, 1, 1.4, 1, 1.4]
evaluation = 0
for i in range(9):
evaluation += 1.5 * real_evaluate_board(state.blocks[i]) * eval_mul[i]
evaluation += state.global_cells[i] * eval_mul[i]
result = check_win_condition(state.global_cells.reshape(3, 3))
evaluation += result * 5000
evaluation += real_evaluate_board(state.global_cells.reshape(3, 3)) * 150
return evaluation
def recurse(state: State, depth, alpha, beta):
global transposition_table
#check transposition table
hash_key = hash(repr(state))
if transposition_table[hash_key]["depth"] >= depth:
if transposition_table[hash_key]["flag"] == "exact":
return transposition_table[hash_key]["score"]
elif transposition_table[hash_key]["flag"] == "lowerbound":
alpha = max(alpha, transposition_table[hash_key]["score"])
elif transposition_table[hash_key]["flag"] == "upperbound":
beta = min(beta, transposition_table[hash_key]["score"])
if alpha >= beta:
return transposition_table[hash_key]["score"]
if state.game_over or depth == 0 or time.time() - start_time >= TIME_OUT:
return evaluation_function(state)
valid_moves = state.get_valid_moves
if state.player_to_move == state.X:
max_utility = -float('inf')
for move in valid_moves:
child_state = copy.deepcopy(state)
child_state.act_move(move)
utility = recurse(child_state, depth - 1, alpha, beta)
if utility > max_utility:
max_utility = utility
if max_utility > alpha:
alpha = max_utility
if alpha >= beta:
break
# Store the result in the transposition table
transposition_table[hash_key]["score"] = alpha
transposition_table[hash_key]["depth"] = depth
transposition_table[hash_key]["flag"] = "exact"
return alpha
else:
min_utility = float('inf')
for move in valid_moves:
child_state = copy.deepcopy(state)
child_state.act_move(move)
utility = recurse(child_state, depth - 1, alpha, beta)
if utility < min_utility:
min_utility = utility
if min_utility < beta:
beta = min_utility
if beta <= alpha:
break
# Store the result in the transposition table
transposition_table[hash_key]["score"] = beta
transposition_table[hash_key]["depth"] = depth
transposition_table[hash_key]["flag"] = "exact"
return beta
numMoves = 0
def select_move(cur_state, remain_time):
global start_time
start_time = time.time()
valid_moves = cur_state.get_valid_moves
if len(valid_moves) == 0:
return None
for move in valid_moves:
child_state = copy.deepcopy(cur_state)
child_state.act_move(move)
if child_state.game_over is True:
return move
global numMoves
if numMoves == 0 and cur_state.player_to_move == cur_state.X:
numMoves += 1
return UltimateTTT_Move(4, 1, 1, cur_state.X)
scores = np.zeros(len(valid_moves))
for i in range(len(valid_moves)):
scores[i] += real_evaluate_position(cur_state.blocks[valid_moves[i].index_local_board], valid_moves[i].x,
valid_moves[i].y, valid_moves[i].value) * 45
for i in range(len(valid_moves)):
child_state = None
child_state = copy.deepcopy(cur_state)
child_state.act_move(valid_moves[i])
utility = 0
alpha = -float('inf')
beta = float('inf')
if cur_state.free_move is True:
if numMoves < 17:
utility = recurse(child_state, 3, alpha, beta)
elif numMoves < 20:
utility = recurse(child_state, 4, alpha, beta)
elif numMoves < 25:
utility = recurse(child_state, 5, alpha, beta)
else:
utility = recurse(child_state, 6, alpha, beta)
else:
if numMoves < 15:
utility = recurse(child_state, 4, alpha, beta)
elif numMoves < 20:
utility = recurse(child_state, 5, alpha, beta)
else:
utility = recurse(child_state, 6, alpha, beta)
scores[i] += utility
best_move = None
if valid_moves[0].value == 1:
best_score = -float('inf')
for i in range(len(valid_moves)):
if scores[i] > best_score:
best_score = scores[i]
best_move = valid_moves[i]
else:
best_score = float('inf')
for i in range(len(valid_moves)):
if scores[i] < best_score:
best_score = scores[i]
best_move = valid_moves[i]
numMoves += 1
return best_move