-
Notifications
You must be signed in to change notification settings - Fork 1
/
sgf2boards.py
executable file
·266 lines (246 loc) · 7.9 KB
/
sgf2boards.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import re
import copy
from warnings import warn
class Group():
def __init__(self, move = None, color = None, board = None):
self.COLOR = color
self.board = board
self.stones = set([move])
self.liberties = set()
self.coords = 'abcdefghijklmnopqrs'
self.add_stone(move, color, self.board)
self.compute_liberties()
def add_stone(self, move = None, color = None, board = None):
if not board: board = self.board
if move == '': return self.stones
x = self.get_coord( position=move, which='x' )
y = self.get_coord( position=move, which='y' )
top = None
bottom = None
right = None
left = None
if x < 18:
bottom = self.coords[y] + self.coords[x + 1]
if x > 0:
top = self.coords[y] + self.coords[x - 1]
if y < 18:
right = self.coords[y + 1] + self.coords[x]
if y > 0:
left = self.coords[y - 1] + self.coords[x]
if ( top in self.stones or bottom in self.stones or right in self.stones or left in self.stones ):
if color == self.COLOR:
self.stones.add(move)
board[ x ][ y ] = color
self.board = board
self.compute_liberties(self.board)
return self.stones
def get_coord( self, position='zz', which='' ):
if which == 'x':
_coord = re.search(position[ 1 ], self.coords).start()
elif which == 'y':
_coord = re.search(position[ 0 ], self.coords).start()
else:
_coord = None
return _coord
def compute_liberties(self, board = None):
if not board:
board = self.board
self.liberties = set()
for s in self.stones:
x = self.get_coord( position=s, which='x' )
y = self.get_coord( position=s, which='y' )
if x < 18:
bottom = self.coords[y] + self.coords[ x + 1]
self.liberties.add(bottom)
if x > 0:
top = self.coords[y] + self.coords[x - 1]
self.liberties.add(top)
if y < 18:
right = self.coords[y + 1] + self.coords[x]
self.liberties.add(right)
if y > 0:
left = self.coords[y - 1] + self.coords[x]
self.liberties.add(left)
_to_remove = set()
for l in self.liberties:
x = self.get_coord( position=l, which='x' )
y = self.get_coord( position=l, which='y' )
if self.board[x][y] != '.':
_to_remove.add(l)
for r in _to_remove:
self.liberties.remove(r)
return self.liberties
def merge(self, group):
if self.COLOR != group.COLOR: return self
if len(self.stones & group.stones) > 0:
self.stones = (self.stones | group.stones)
self.liberties=self.compute_liberties()
return(self)
def update_board(self, board = None):
if not board: return 0
self.board = board
self.compute_liberties()
class Kifu():
def __init__( self, file='' ):
with open(file, 'r') as f:
_read_data = f.read()
_kifu = _read_data.split(';')
self.empty = [ [ '.' for i in range(19) ] for j in range(19) ]
self.board = copy.deepcopy(self.empty)
self.coords = 'abcdefghijklmnopqrs'
self.stones = [ ]
self.alive_groups = set()
self.dead_groups = set()
self.captured = {'B': 0, 'W': 0}
self.moves = _kifu[ 2: ]
self.moves_played = 0
self.next2move = 'B'
self.pass1 = False
self.pass2 = False
self.total_moves = len(self.moves)
self.set_fileds_and_handicap(_kifu[ 1 ])
def set_fileds_and_handicap(self, _info):
fields = {
'black': 'PB',
'black_rank': 'BR',
'white': 'PW',
'white_rank': 'WR',
'handicap': 'HA',
'stone0': 'AB',
'stones': '',
'rules': 'RU',
'komi': 'KM',
'result': 'RE',
'size': 'SZ',
}
for _line in _info.split('\n'):
for key in fields:
_match = '^' + fields[ key ] + '\[(?P<' + key + '>.*)\]$'
_parse = re.search(_match, _line)
if key[ 0:5 ] == 'stone':
if _parse:
self.stones.append(_parse.group(key))
key = 'stones'
else:
if _parse: setattr(self, key, _parse.group(key))
def get_coord( self, position='zz', which='' ):
if which == 'x':
_coord = re.search(position[ 1 ], self.coords).start()
elif which == 'y':
_coord = re.search(position[ 0 ], self.coords).start()
else:
_coord = None
return _coord
def starting_board( self ):
self.next2move = 'B'
for s in self.stones:
x = self.get_coord(s, 'x')
y = self.get_coord(s, 'y')
self.board[ x ][ y ] = 'B'
g = Group(s, 'B', self.board)
self.alive_groups.add(g)
self.next2move = 'W'
return self.board
def print_board( self, board=None ):
_tmp = [ ]
if not board: board = self.board
for row in board:
_tmp.append(''.join(row))
visual_board = '\n'.join(_tmp)
return visual_board
def get_position_at( self, move=0 ):
if (move == -1 or move == 'last'):
move = len(self.moves) - 1
if (move > len(self.moves)):
warn('Not enough moves, showing last instead')
move = len(self.moves) - 1
_colors = [ 'B', 'W' ]
self.starting_board()
for m in self.moves[ 0:move ]:
m = m.rstrip('\r\n')
_match = self.next2move + '\[(?P<stone>.*)\]'
_parse = re.search(_match, m)
_stone = _parse.group('stone')
if _stone == '':
self.pass2 = self.pass1 & True
self.pass1 = True
else:
self.pass1 = False
x = self.get_coord(_stone, 'x')
y = self.get_coord(_stone, 'y')
if not self.pass1:
self.board[ x ][ y ] = self.next2move
self.get_alive_groups(m)
self.next2move = _colors[ (_colors.index(self.next2move) + 1) % len(_colors) ]
self.moves_played = move
return self.board
def get_alive_groups(self,m = None):
_to_add = set()
_match = '(?P<color>.*)\[(?P<stone>.*)\]'
_parse = re.search(_match, m)
_stone = _parse.group('stone')
_color = _parse.group('color')
for g in self.alive_groups:
_gsize_prev = len(g.stones)
g.add_stone(_stone, _color, self.board)
_gsize_after = len(g.stones)
if _gsize_after == _gsize_prev:
_to_add.add((_color,_stone))
for m in _to_add:
g = Group(m[1], m[0], self.board)
self.alive_groups.add(g)
self.merge_groups()
self.remove_dead(self.next2move)
return self.alive_groups
def merge_groups(self):
_to_remove = set()
to_ommit = set()
_to_add = set()
for g1 in self.alive_groups:
to_ommit.add(g1)
for g2 in self.alive_groups:
if g2 in to_ommit: continue
if (g1.stones <= g2.stones) and \
g2.COLOR == g1.COLOR:
_to_remove.add(g1)
elif (g2.stones <= g1.stones) and \
g2.COLOR == g1.COLOR:
_to_remove.add(g2)
elif len(g1.stones & g2.stones)>0 and \
g2.COLOR == g1.COLOR:
g2.update_board(self.board)
g = g2.merge(g1)
g.update_board(self.board)
_to_add.add(g)
_to_remove.add(g1)
_to_remove.add(g2)
for _r in _to_remove:
self.alive_groups.remove(_r)
for _a in _to_add:
self.alive_groups.add(_a)
def remove_dead(self, just_played):
for g in self.alive_groups:
if len(g.liberties) == 0 and g.COLOR != just_played: self.dead_groups.add(g)
for dg in self.dead_groups:
assert isinstance(dg, Group)
for ds in dg.stones:
x = self.get_coord(ds, 'x')
y = self.get_coord(ds, 'y')
self.board[ x ][ y ] = '.'
self.alive_groups.remove(dg)
self.captured[dg.COLOR] += len(dg.stones)
for ag in self.alive_groups:
ag.update_board(self.board)
self.dead_groups = set()
if __name__ == '__main__':
k = Kifu('sgfs/kgs-19-2015-05-new/2015-05-27-4.sgf')
b = k.starting_board()
for g in k.alive_groups:
print(g.COLOR, g.stones)
m1 = k.get_position_at(320)
print(k.print_board(m1))
for g in k.alive_groups:
print(g.COLOR,g.stones, len(g.liberties))
print(k.dead_groups)