-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path_2013452_2013368.py
506 lines (429 loc) · 17.5 KB
/
_2013452_2013368.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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
import copy
import timeit
class Node_1:
def __init__(self,
board: list,
prev_board: list = None,):
self.board = copy.deepcopy(board)
self.prev_board = copy.deepcopy(prev_board)
class CoGanh:
def __init__(self):
# 2: Initial value, processing
# 1: can move
# 0: can't move
self.moveBoard = []
def getPosition(self, board, player):
result = []
for i in range(5):
for j in range(5):
if board[i][j] == player:
result.append((i, j))
return result
def checkTrap(self, prev_board, board, opponent):
x, y = 0, 0
for i in range(5):
for j in range(5):
if prev_board[i][j] == opponent and board[i][j] == 0:
x, y = i, j
break
isTrap = False
# HORIZONTAL
if x > 0 and x < 4:
if board[x - 1][y] == opponent and board[x + 1][y] == opponent:
isTrap = True
# VERTICAL
if y > 0 and y < 4:
if board[x][y - 1] == opponent and board[x][y + 1] == opponent:
isTrap = True
# DIAGONAL
if ((x + y) % 2 == 0 and (x > 0 and x < 4) and (y > 0 and y < 4)):
if board[x - 1][y - 1] == opponent and board[x + 1][y + 1] == opponent:
isTrap = True
if board[x - 1][y + 1] == opponent and board[x + 1][y - 1] == opponent:
isTrap = True
if isTrap:
return (x, y)
return None
# If 0, the chessman at this position can move. Otherwise, it can't
def cantMove(self, board, position):
x, y = position[0], position[1]
if self.moveBoard[x][y] == 0:
return True
elif self.moveBoard[x][y] == 1:
return False
# PART I
if x > 0:
if board[x - 1][y] == 0:
self.moveBoard[x][y] = 1
return False
if x < 4:
if board[x + 1][y] == 0:
self.moveBoard[x][y] = 1
return False
if y > 0:
if board[x][y - 1] == 0:
self.moveBoard[x][y] = 1
return False
if y < 4:
if board[x][y + 1] == 0:
self.moveBoard[x][y] = 1
return False
if (x + y) % 2 == 0:
if x > 0 and y > 0:
if board[x - 1][y - 1] == 0:
self.moveBoard[x][y] = 1
return False
if x < 4 and y > 0:
if board[x + 1][y - 1] == 0:
self.moveBoard[x][y] = 1
return False
if x > 0 and y < 4:
if board[x - 1][y + 1] == 0:
self.moveBoard[x][y] = 1
return False
if x < 4 and y < 4:
if board[x + 1][y + 1] == 0:
self.moveBoard[x][y] = 1
return False
# PART II
player = board[x][y]
board[x][y] = 2
result = True
if x > 0:
if board[x - 1][y] == player:
result = result and self.cantMove(board, (x - 1, y))
if x < 4:
if board[x + 1][y] == player:
result = result and self.cantMove(board, (x + 1, y))
if y > 0:
if board[x][y - 1] == player:
result = result and self.cantMove(board, (x, y - 1))
if y < 4:
if board[x][y + 1] == player:
result = result and self.cantMove(board, (x, y + 1))
if (x + y) % 2 == 0:
if x > 0 and y > 0:
if board[x - 1][y - 1] == player:
result = result and self.cantMove(board, (x - 1, y - 1))
if x < 4 and y > 0:
if board[x + 1][y - 1] == player:
result = result and self.cantMove(board, (x + 1, y - 1))
if x > 0 and y < 4:
if board[x - 1][y + 1] == player:
result = result and self.cantMove(board, (x - 1, y + 1))
if x < 4 and y < 4:
if board[x + 1][y + 1] == player:
result = result and self.cantMove(board, (x + 1, y + 1))
if result:
later = False
if x > 0 and not later:
if board[x - 1][y] == 2: later = True
if x < 4 and not later:
if board[x + 1][y] == 2: later = True
if y > 0 and not later:
if board[x][y - 1] == 2: later = True
if y < 4 and not later:
if board[x][y + 1] == 2: later = True
if (x + y) % 2 == 0:
if x > 0 and y > 0 and not later:
if board[x - 1][y - 1] == 2: later = True
if x < 4 and y > 0 and not later:
if board[x + 1][y - 1] == 2: later = True
if x > 0 and y < 4 and not later:
if board[x - 1][y + 1] == 2: later = True
if x < 4 and y < 4 and not later:
if board[x + 1][y + 1] == 2: later = True
if not later:
self.moveBoard[x][y] = 0
else:
self.moveBoard[x][y] = 1
return result
def ganh(self, board, position):
x, y = position[0], position[1]
player = board[x][y]
opponent = -1 * board[x][y]
# HORIZONTAL
if x > 0 and x < 4:
if board[x - 1][y] == opponent and board[x + 1][y] == opponent:
board[x - 1][y], board[x + 1][y] = player, player
# VERTICAL
if y > 0 and y < 4:
if board[x][y - 1] == opponent and board[x][y + 1] == opponent:
board[x][y - 1], board[x][y + 1] = player, player
# DIAGONAL
if ((x + y) % 2 == 0 and (x > 0 and x < 4) and (y > 0 and y < 4)):
if board[x - 1][y - 1] == opponent and board[x + 1][y + 1] == opponent:
board[x - 1][y - 1], board[x + 1][y + 1] = player, player
if board[x - 1][y + 1] == opponent and board[x + 1][y - 1] == opponent:
board[x - 1][y + 1], board[x + 1][y - 1] = player, player
def chan(self, board, player):
# Init moveBoard
self.moveBoard = []
for i in range(5):
tmp = []
for j in range(5):
tmp.append(2)
self.moveBoard.append(tmp)
pos = self.getPosition(board, player)
# Check which position is "chan"ed
for p in pos:
if self.cantMove(board, p):
board[p[0]][p[1]] = -1 * player
for i in range(5):
for j in range(5):
if board[i][j] == 2:
board[i][j] = player
# Return Node_1 and a position
def move_gen(self, node: Node_1, position: tuple, trap):
x, y = position[0], position[1]
player = node.board[x][y]
opponent = -1 * player
result = []
if trap != None:
x1, y1 = trap[0], trap[1]
if (
(x1 == x + 1 and y1 == y) or
(x1 == x - 1 and y1 == y) or
(x1 == x and y1 == y + 1) or
(x1 == x and y1 == y - 1) or
((x + y) % 2 == 0 and
(
(x1 == x - 1 and y1 == y - 1) or
(x1 == x + 1 and y1 == y - 1) or
(x1 == x - 1 and y1 == y + 1) or
(x1 == x + 1 and y1 == y + 1)
)
)
):
tmp_board = copy.deepcopy(node.board)
tmp_board[x1][y1] = copy.deepcopy(player)
tmp_board[x][y] = 0
self.ganh(tmp_board, (x1, y1))
self.chan(tmp_board, opponent)
tmp = Node_1(tmp_board, node.board)
result.append(((tmp, (x1, y1), position, True)))
return result
# UP
if x > 0:
if node.board[x - 1][y] == 0:
tmp_board = copy.deepcopy(node.board)
tmp_board[x - 1][y] = copy.deepcopy(player)
tmp_board[x][y] = 0
self.ganh(tmp_board, (x - 1, y))
self.chan(tmp_board, opponent)
tmp = Node_1(tmp_board, node.board)
result.append((tmp, (x - 1, y), position, False))
# DOWN
if x < 4:
if node.board[x + 1][y] == 0:
tmp_board = copy.deepcopy(node.board)
tmp_board[x + 1][y] = copy.deepcopy(player)
tmp_board[x][y] = 0
self.ganh(tmp_board, (x + 1, y))
self.chan(tmp_board, opponent)
tmp = Node_1(tmp_board, node.board)
result.append((tmp, (x + 1, y), position, False))
# LEFT
if y > 0:
if node.board[x][y - 1] == 0:
tmp_board = copy.deepcopy(node.board)
tmp_board[x][y - 1] = copy.deepcopy(player)
tmp_board[x][y] = 0
self.ganh(tmp_board, (x, y - 1))
self.chan(tmp_board, opponent)
tmp = Node_1(tmp_board, node.board)
result.append((tmp, (x, y - 1), position, False))
# RIGHT
if y < 4:
if node.board[x][y + 1] == 0:
tmp_board = copy.deepcopy(node.board)
tmp_board[x][y + 1] = copy.deepcopy(player)
tmp_board[x][y] = 0
self.ganh(tmp_board, (x, y + 1))
self.chan(tmp_board, opponent)
tmp = Node_1(tmp_board, node.board)
result.append((tmp, (x, y + 1), position, False))
# DIAGONAL
if (x + y) % 2 == 0:
# UP LEFT
if x > 0 and y > 0:
if node.board[x - 1][y - 1] == 0:
tmp_board = copy.deepcopy(node.board)
tmp_board[x - 1][y - 1] = copy.deepcopy(player)
tmp_board[x][y] = 0
self.ganh(tmp_board, (x - 1, y - 1))
self.chan(tmp_board, opponent)
tmp = Node_1(tmp_board, node.board)
result.append((tmp, (x - 1, y - 1), position, False))
# UP RIGHT
if x > 0 and y < 4:
if node.board[x - 1][y + 1] == 0:
tmp_board = copy.deepcopy(node.board)
tmp_board[x - 1][y + 1] = copy.deepcopy(player)
tmp_board[x][y] = 0
self.ganh(tmp_board, (x - 1, y + 1))
self.chan(tmp_board, opponent)
tmp = Node_1(tmp_board, node.board)
result.append((tmp, (x - 1, y + 1), position, False))
# DOWN LEFT
if x < 4 and y > 0:
if node.board[x + 1][y - 1] == 0:
tmp_board = copy.deepcopy(node.board)
tmp_board[x + 1][y - 1] = copy.deepcopy(player)
tmp_board[x][y] = 0
self.ganh(tmp_board, (x + 1, y - 1))
self.chan(tmp_board, opponent)
tmp = Node_1(tmp_board, node.board)
result.append((tmp, (x + 1, y - 1), position, False))
# DOWN RIGHT
if x < 4 and y < 4:
if node.board[x + 1][y + 1] == 0:
tmp_board = copy.deepcopy(node.board)
tmp_board[x + 1][y + 1] = copy.deepcopy(player)
tmp_board[x][y] = 0
self.ganh(tmp_board, (x + 1, y + 1))
self.chan(tmp_board, opponent)
tmp = Node_1(tmp_board, node.board)
result.append((tmp, (x + 1, y + 1), position, False))
return result
def end_game(self, board, notice = True):
score = sum(map(sum, board))
if score == 16:
if notice:
print("\nO WIN!!!")
return True
elif score == -16:
if notice:
print("\nX WIN!!!")
return True
return False
def X_win(self, board):
score = sum(map(sum, board))
if score == -16:
return True
return False
def O_win(self, board):
score = sum(map(sum, board))
if score == 16:
return True
return False
class Solver:
def __init__(self,
depth: int = 2,
prev_board: list = None,
board: list = None,
player: int = -1,):
self.depth = depth
self.prev_board = copy.deepcopy(prev_board)
self.board = copy.deepcopy(board)
self.player = player
self.opponent = -1 * player
self.start = None
self.end = None
def evaluate(self, board):
result = sum(map(sum, board))
if self.player == -1:
result *= -1
return result
def play(self, node, dp, alpha = -100, beta = 100):
if dp > self.depth:
return
# LEAF NODE
if dp == self.depth:
return self.evaluate(node.board)
cg = CoGanh()
trap = None
# PLAYER
if dp % 2 == 0:
if node.prev_board != None:
trap = cg.checkTrap(node.prev_board, node.board, self.opponent)
successor = []
pos = cg.getPosition(node.board, self.player)
for p in pos:
ans = cg.move_gen(node, p, trap)
if ans != None:
successor += cg.move_gen(node, p, trap)
isTrapped = False
if len(successor) > 0:
for s in successor:
if s[3]:
isTrapped = True
break
for s in successor:
if isTrapped:
if not s[3]:
continue
if self.player == -1:
if cg.X_win(s[0].board):
if dp == 0:
self.start = s[2]
self.end = s[1]
return 75
else:
if cg.O_win(s[0].board):
if dp == 0:
self.start = s[2]
self.end = s[1]
return 75
value = self.play(s[0], dp + 1, alpha, beta)
if value > alpha:
alpha = value
if dp == 0:
self.start = s[2]
self.end = s[1]
if alpha >= beta:
return alpha
return alpha
# OPPONENT
else:
if node.prev_board != None:
trap = cg.checkTrap(node.prev_board, node.board, self.player)
successor = []
pos = cg.getPosition(node.board, self.opponent)
for p in pos:
ans = cg.move_gen(node, p, trap)
if ans != None:
successor += cg.move_gen(node, p, trap)
isTrapped = False
if len(successor) > 0:
for s in successor:
if s[3]:
isTrapped = True
break
for s in successor:
if isTrapped:
if not s[3]:
continue
if self.player == -1:
if cg.O_win(s[0].board):
return -50
else:
if cg.X_win(s[0].board):
return -50
value = self.play(s[0], dp + 1, alpha, beta)
if value < beta:
beta = value
if beta <= alpha:
return beta
return beta
def solv(self):
node = Node_1(self.board, self.prev_board)
score = self.play(node, 0)
if self.start == None or self.end == None:
return None
return (self.start, self.end)
def move(prev_board, board, player, remain_time_x, remain_time_o):
start = timeit.default_timer()
# Use depth = 2 when fighting online with 'random move' bot
# Use depth >= 4 when fighting offline with another team's bot
depth = 4
# Using Minimax
solver = Solver(depth, prev_board, board, player)
result = solver.solv()
stop = timeit.default_timer()
time_step = stop - start
if player == 1:
remain_time_o -= time_step
else:
remain_time_x -= time_step
return result