-
Notifications
You must be signed in to change notification settings - Fork 0
/
convert_matrix.py
186 lines (170 loc) · 6.98 KB
/
convert_matrix.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
import re, numpy, Queue
sgf = open('test.sgf', 'r')
board = numpy.zeros((19,19))
for line in sgf:
for coordinates in re.findall('\WAW\[\w\w\]|\WW\[\w\w\]', line):
print (coordinates)
board[ord(coordinates[-3]) - 97][ord(coordinates[-2]) - 97] = 1
for coordinates in re.findall('\WAB\[\w\w\]|\W\B\[\w\w\]', line):
print (coordinates)
board[ord(coordinates[-3]) - 97][ord(coordinates[-2]) - 97] = -1
print(numpy.matrix(board))
print('\n\n')
q = Queue.Queue()
# 0 state is no adjacent stones
# 1 white only adjacent stones
# 2 black only adjacent stones
# 3 mixed adjacent stones
state = 0
#-1 black stone
# 1 white stone
# 0 neutral (unvisited)
# 2 white territory
#-2 black territory
# 3 visited space
# 4 neutral (visited)
for a in range(19):
for b in range(19):
state = 0
q.put((a,b))
while (True):
if q.empty():
if state == 0:
#mark everything 4 neutral (visited)
for x in range(19):
for y in range(19):
if board[x][y] == 3:
board[x][y] = 4
if state == 1:
#mark everything 2 white territory
for x in range(19):
for y in range(19):
if board[x][y] == 3:
board[x][y] = 2
if state == 2:
#mark everything -2 black territory
for x in range(19):
for y in range(19):
if board[x][y] == 3:
board[x][y] = -2
if state == 3:
#mark everything 4 neutral (visited)
for x in range(19):
for y in range(19):
if board[x][y] == 3:
board[x][y] = 4
break
(i,j) = q.get()
# if this is an unvisited space
if board[i][j] == 0:
# mark this space as new visited
board[i][j] = 3
# check to the left
if i - 1 >= 0:
# if the space to the left is unvisited
if board[i-1][j] == 0:
# add it to the queue
q.put((i-1,j))
# if it is a black stone
elif board[i-1][j] == -1:
# if there havn't been stones or there have only been black
# stones
if state == 2 or state == 0:
state = 2
# if there have been any white stones
else:
state = 3
# if it is a white stone
elif board[i-1][j] == 1:
# if there havn't been stones or there have only bee white
# stones
if state == 1 or state == 0:
state = 1
# if there havve been any black stones
else:
state = 3
# if it is a neutral stone
elif board[i-1][j] == 4:
state = 3
# check above
if j - 1 >= 0:
# if the space above is unvisited
if board[i][j-1] == 0:
# add it to the queue
q.put((i,j-1))
# if it is a black stone
elif board[i][j-1] == -1:
# if there havn't been stones or there have only been black
# stones
if state == 2 or state == 0:
state = 2
# if there have been any white stones
else:
state = 3
# if it is a white stone
elif board[i][j-1] == 1:
# if there havn't been stones or there have only bee white
# stones
if state == 1 or state == 0:
state = 1
# if there have been any black stones
else:
state = 3
# if it is a neutral stone
elif board[i-1][j] == 4:
state = 3
# check to the right
if i + 1 <= 18:
# if the space to the right is unvisited
if board[i+1][j] == 0:
# add it to the queue
q.put((i+1,j))
# if it is a black stone
elif board[i+1][j] == -1:
# if there havn't been stones or there have only been black
# stones
if state == 2 or state == 0:
state = 2
# if there have been any white stones
else:
state = 3
# if it is a white stone
elif board[i+1][j] == 1:
# if there havn't been stones or there have only bee white
# stones
if state == 1 or state == 0:
state = 1
# if there have been any black stones
else:
state = 3
# if it is a neutral stone
elif board[i-1][j] == 4:
state = 3
# check below
if j + 1 <= 18:
# if the space below is unvisited
if board[i][j+1] == 0:
# add it to the queue
q.put((i,j-1))
# if it is a black stone
elif board[i][j+1] == -1:
# if there havn't been stones or there have only been black
# stones
if state == 2 or state == 0:
state = 2
# if there have been any white stones
else:
state = 3
# if it is a white stone
elif board[i][j+1] == 1:
# if there havn't been stones or there have only bee white
# stones
if state == 1 or state == 0:
state = 1
# if there have been any black stones
else:
state = 3
# if it is a neutral stone
elif board[i-1][j] == 4:
state = 3
print(numpy.matrix(board))