-
Notifications
You must be signed in to change notification settings - Fork 52
/
STry053.py
71 lines (59 loc) · 2 KB
/
STry053.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
class AI(object):
def name(self):
return 'STry053'
def __init__(self):
self.board = [[0 for _ in range(8)] for _ in range(8)]
self.board[3][3] = self.board[4][4] = 1
self.board[3][4] = self.board[4][3] = -1
self.turn = 1
def play(self, x, y):
if not (0 <= x < 8 and 0 <= y < 8):
return False
if self.board[x][y] != 0:
return False
flip_list = []
for dx in [-1, 0, 1]:
for dy in [-1, 0, 1]:
if dx == 0 and dy == 0:
continue
flip_list += self.search_line(x, y, dx, dy)
if len(flip_list) == 0:
return False
for (nx, ny) in flip_list:
self.board[nx][ny] = self.turn
self.board[x][y] = self.turn
self.turn = -self.turn
return True
def search_line(self, x, y, dx, dy):
flip_list = []
nx, ny = x + dx, y + dy
if not (0 <= nx < 8 and 0 <= ny < 8):
return []
if self.board[nx][ny] == self.turn:
return []
while True:
nx, ny = nx + dx, ny + dy
if not (0 <= nx < 8 and 0 <= ny < 8):
return []
if self.board[nx][ny] == 0:
return []
if self.board[nx][ny] == -self.turn:
flip_list.append((nx, ny))
else:
return flip_list
def alphabeta(state, depth, alpha, beta):
if depth == 0:
return evaluate(state)
if state.turn == 1:
value = -float('inf')
for action in get_actions(state):
value = max(value, alphabeta(do_action(state, action), depth - 1, alpha, beta))
alpha = max(alpha, value)
if alpha >= beta:
break
def play(self, board, color):
while True:
s = value
t = alpha
if board.put_and_reverse(s, t, color, reverse=False) > 0:
return (s, t)