-
Notifications
You must be signed in to change notification settings - Fork 16
/
play.py
206 lines (182 loc) · 5.75 KB
/
play.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
# -*- coding: utf-8 -*-
import numpy as np
from conf import conf
SIZE = conf['SIZE']
SWAP_INDEX = [1, 0, 3, 2, 5, 4, 7, 6, 9, 8, 11, 10, 13, 12, 15, 14]
def index2coord(index):
y = index // SIZE
x = index - SIZE * y
return x, y
def coord2index(x, y):
return y * SIZE + x
def legal_moves(board):
# Occupied places
mask1 = board[0,:,:,0].reshape(-1) != 0
mask2 = board[0,:,:,1].reshape(-1) != 0
mask = mask1 + mask2
# Ko situations
ko_mask = ((board[0,:,:,2] - board[0,:,:,0]))
if (ko_mask == 1).sum() == 1:
mask += (ko_mask == 1).reshape(-1)
# Pass is always legal
mask = np.append(mask, 0)
return mask
def get_real_board(board):
player = board[0,0,0,-1]
if player == 1:
real_board = board[0,:,:,0] - board[0,:,:,1]
else:
real_board = board[0,:,:,1] - board[0,:,:,0]
return real_board
def _show_board(board, policy):
real_board = get_real_board(board)
if policy is not None:
index = policy.argmax()
x, y = index2coord(index)
color = "B" if board[0][0][0][-1] == 1 else "W"
string = "To play: %s\n" % color
for j, row in enumerate(real_board):
for i, c in enumerate(row):
if c == 1:
string += u"○ "
elif c == -1:
string += u"● "
elif policy is not None and i == x and j == y:
string += u"X "
else:
string += u". "
string += "\n"
if policy is not None and y == SIZE:
string += "Pass policy"
return string
def show_board(board, policy=None, history=1):
results = []
for i in reversed(range(history)):
tmp_board = np.copy(board)
tmp_board = tmp_board[:,:,:,i:]
if i % 2 == 1:
tmp_board[:,:,:,-1] *= -1
results.append(_show_board(tmp_board, policy))
return "\n".join(results)
dxdys = [(1, 0), (-1, 0), (0, 1), (0, -1)]
def capture_group(x, y, real_board, group=None):
if group is None:
group = [(x, y)]
c = real_board[y][x]
for dx, dy in dxdys:
nx = x + dx
ny = y + dy
if (nx, ny) in group:
continue
if not(0 <= nx < SIZE and 0 <= ny < SIZE):
continue
dc = real_board[ny][nx]
if dc == 0:
return None
elif dc == c:
group.append( (nx, ny) )
group = capture_group(nx, ny, real_board, group=group)
if group == None:
return None
return group
def take_stones(x, y, board):
real_board = get_real_board(board)
_player = 1 if board[0,0,0,-1] == 1 else -1
for dx, dy in dxdys: # We need to check capture
nx = x + dx
ny = y + dy
if not(0 <= nx < SIZE and 0 <= ny < SIZE):
continue
if real_board[ny][nx] == 0:
continue
if real_board[ny][nx] == _player:
continue
group = capture_group(nx, ny, real_board)
if group:
for _x, _y in group:
assert board[0,_y,_x,1] == 1
board[0,_y,_x,1] = 0
real_board[_y][_x] = 0
for dx, dy in dxdys + [(0, 0)]: # We need to check self sucide.
nx = x + dx
ny = y + dy
if not(0 <= nx < SIZE and 0 <= ny < SIZE):
continue
if real_board[ny][nx] == 0:
continue
if real_board[ny][nx] != _player:
continue
group = capture_group(nx, ny, real_board)
if group:
for _x, _y in group:
# Sucide
assert board[0,_y,_x,0] == 1
board[0,_y,_x,0] = 0
real_board[_y][_x] = 0
return board
def make_play(x, y, board):
player = board[0,0,0,-1]
board[:,:,:,2:16] = board[:,:,:,0:14]
if y != SIZE:
assert board[0,y,x,1] == 0
assert board[0,y,x,0] == 0
board[0,y,x,0] = 1 # Careful here about indices
board = take_stones(x, y, board)
else:
# "Skipping", player
pass
# swap_players
board[:,:,:,range(16)] = board[:,:,:,SWAP_INDEX]
player = -1 if player == 1 else 1
board[:,:,:,-1] = player
return board, player
def _color_adjoint(i, j, color, board):
# TOP
SIZE1 = len(board)
SIZE2 = len(board[0])
if i > 0 and board[i-1][j] == 0:
board[i-1][j] = color
_color_adjoint(i - 1, j, color, board)
# BOTTOM
if i < SIZE1 - 1 and board[i+1][j] == 0:
board[i+1][j] = color
_color_adjoint(i + 1, j, color, board)
# LEFT
if j > 0 and board[i][j - 1] == 0:
board[i][j - 1] = color
_color_adjoint(i, j - 1, color, board)
# RIGHT
if j < SIZE2 - 1 and board[i][j + 1] == 0:
board[i][j + 1] = color
_color_adjoint(i, j + 1, color, board)
return board
def color_board(real_board, color):
board = np.copy(real_board)
for i, row in enumerate(board):
for j, v in enumerate(row):
if v == color:
_color_adjoint(i, j, color, board)
return board
def get_winner(board):
real_board = get_real_board(board)
points = _get_points(real_board)
black = points.get(1, 0) + points.get(2, 0)
white = points.get(-1, 0) + points.get(-2, 0) + conf['KOMI']
if black > white:
return 1, black, white
elif black == white:
return 0, black, white
else:
return -1, black, white
def _get_points(real_board):
colored1 = color_board(real_board, 1)
colored2 = color_board(real_board, -1)
total = colored1 + colored2
unique, counts = np.unique(total, return_counts=True)
points = dict(zip(unique, counts))
return points
def game_init():
board = np.zeros((1, SIZE, SIZE, 17), dtype=np.int32)
player = 1
board[:,:,:,-1] = player
return board, player