-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtest_go_sets.py
208 lines (175 loc) · 8.68 KB
/
test_go_sets.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
import re
import unittest
from go_sets import Position, LibertyTracker, N, NN, WHITE, BLACK, EMPTY
def load_board(string):
return re.sub(r'[^XO\.]+', '', string)
EMPTY_ROW = EMPTY * 19
class TestLibertyTracker(unittest.TestCase):
def test_lib_tracker_init(self):
board = load_board(BLACK + EMPTY*18 + EMPTY_ROW * 18)
libtracker = LibertyTracker.from_board(board)
self.assertEqual(len(libtracker.groups), 1)
self.assertNotEqual(libtracker.group_index[0], None)
self.assertEqual(libtracker.get_liberties(), [2] + [0] * (NN - 1))
sole_group = libtracker.groups[libtracker.group_index[0]]
self.assertEqual(sole_group.stones, {0})
self.assertEqual(sole_group.liberties, {1, N})
self.assertEqual(sole_group.color, BLACK)
def test_place_stone(self):
board = load_board(BLACK + EMPTY * 18 + EMPTY_ROW * 18)
libtracker = LibertyTracker.from_board(board)
new_lib_tracker, _ = libtracker.add_stone(BLACK, 1)
self.assertEqual(len(new_lib_tracker.groups), 1)
self.assertNotEqual(new_lib_tracker.group_index[0], None)
self.assertEqual(new_lib_tracker.get_liberties(), [3, 3] + [0] * (NN - 2))
sole_group = new_lib_tracker.groups[new_lib_tracker.group_index[0]]
self.assertEqual(sole_group.stones, {0, 1})
self.assertEqual(sole_group.liberties, {2, N, N+1})
self.assertEqual(sole_group.color, BLACK)
def test_place_stone_opposite_color(self):
board = load_board(BLACK + EMPTY * 18 + EMPTY_ROW * 18)
libtracker = LibertyTracker.from_board(board)
new_lib_tracker, _ = libtracker.add_stone(WHITE, 1)
self.assertEqual(len(new_lib_tracker.groups), 2)
self.assertNotEqual(new_lib_tracker.group_index[0], None)
self.assertNotEqual(new_lib_tracker.group_index[1], None)
self.assertEqual(new_lib_tracker.get_liberties(), [1, 2] + [0] * (NN - 2))
black_group = new_lib_tracker.groups[new_lib_tracker.group_index[0]]
white_group = new_lib_tracker.groups[new_lib_tracker.group_index[1]]
self.assertEqual(black_group.stones, {0})
self.assertEqual(black_group.liberties, {N})
self.assertEqual(black_group.color, BLACK)
self.assertEqual(white_group.stones, {1})
self.assertEqual(white_group.liberties, {2, N+1})
self.assertEqual(white_group.color, WHITE)
def test_merge_multiple_groups(self):
board = load_board('''
.X.................
X.X................
.X.................
''' + EMPTY_ROW * 16)
libtracker = LibertyTracker.from_board(board)
new_lib_tracker, _ = libtracker.add_stone(BLACK, N+1)
self.assertEqual(len(new_lib_tracker.groups), 1)
self.assertNotEqual(new_lib_tracker.group_index[N+1], None)
sole_group = new_lib_tracker.groups[new_lib_tracker.group_index[N+1]]
self.assertEqual(sole_group.stones, {1, N, N+1, N+2, 2*N+1})
self.assertEqual(sole_group.liberties, {0, 2, N+3, 2*N, 2*N+2, 3*N+1})
self.assertEqual(sole_group.color, BLACK)
new_lib_cache = new_lib_tracker.get_liberties()
for stone in sole_group.stones:
self.assertEqual(new_lib_cache[stone], 6)
def test_capture_stone(self):
board = load_board('''
.X.................
XO.................
.X.................
''' + EMPTY_ROW * 16)
libtracker = LibertyTracker.from_board(board)
new_lib_tracker, captured = libtracker.add_stone(BLACK, N+2)
self.assertEqual(len(new_lib_tracker.groups), 4)
self.assertEqual(new_lib_tracker.group_index[N+1], None)
self.assertEqual(captured, {N+1})
def test_capture_many(self):
board = load_board('''
.XX................
XOO................
.XX................
''' + EMPTY_ROW * 16)
libtracker = LibertyTracker.from_board(board)
new_lib_tracker, captured = libtracker.add_stone(BLACK, N+3)
self.assertEqual(len(new_lib_tracker.groups), 4)
self.assertEqual(new_lib_tracker.group_index[N+1], None)
self.assertEqual(captured, {N+1, N+2})
left_group = new_lib_tracker.groups[new_lib_tracker.group_index[N]]
self.assertEqual(left_group.stones, {N})
self.assertEqual(left_group.liberties, {0, N+1, 2*N})
right_group = new_lib_tracker.groups[new_lib_tracker.group_index[N+3]]
self.assertEqual(right_group.stones, {N+3})
self.assertEqual(right_group.liberties, {3, N+2, N+4, 2*N+3})
top_group = new_lib_tracker.groups[new_lib_tracker.group_index[1]]
self.assertEqual(top_group.stones, {1, 2})
self.assertEqual(top_group.liberties, {0, 3, N+1, N+2})
bottom_group = new_lib_tracker.groups[new_lib_tracker.group_index[2*N+1]]
self.assertEqual(bottom_group.stones, {2*N+1, 2*N+2})
self.assertEqual(bottom_group.liberties, {N+1, N+2, 2*N, 2*N+3, 3*N+1, 3*N+2})
new_lib_cache = new_lib_tracker.get_liberties()
for stone in top_group.stones:
self.assertEqual(new_lib_cache[stone], 4)
for stone in left_group.stones:
self.assertEqual(new_lib_cache[stone], 3)
for stone in right_group.stones:
self.assertEqual(new_lib_cache[stone], 4)
for stone in bottom_group.stones:
self.assertEqual(new_lib_cache[stone], 6)
for stone in captured:
self.assertEqual(new_lib_cache[stone], 0)
def test_capture_multiple_groups(self):
board = load_board('''
.OX................
OXX................
XX.................
''' + EMPTY_ROW * 16)
libtracker = LibertyTracker.from_board(board)
new_lib_tracker, captured = libtracker.add_stone(BLACK, 0)
self.assertEqual(len(new_lib_tracker.groups), 2)
self.assertEqual(captured, {1, N})
corner_stone = new_lib_tracker.groups[new_lib_tracker.group_index[0]]
self.assertEqual(corner_stone.stones, {0})
self.assertEqual(corner_stone.liberties, {1, N})
surrounding_stones = new_lib_tracker.groups[new_lib_tracker.group_index[2]]
self.assertEqual(surrounding_stones.stones, {2, N+1, N+2, 2*N, 2*N+1})
self.assertEqual(surrounding_stones.liberties, {1, 3, N, N+3, 2*N+2, 3*N, 3*N+1})
new_lib_cache = new_lib_tracker.get_liberties()
for stone in corner_stone.stones:
self.assertEqual(new_lib_cache[stone], 2)
for stone in surrounding_stones.stones:
self.assertEqual(new_lib_cache[stone], 7)
def test_same_friendly_group_neighboring_twice(self):
board = load_board('''
XX.................
X..................
''' + EMPTY_ROW * 17)
libtracker = LibertyTracker.from_board(board)
new_lib_tracker, captured = libtracker.add_stone(BLACK, N+1)
self.assertEqual(len(new_lib_tracker.groups), 1)
sole_group_id = new_lib_tracker.group_index[0]
sole_group = new_lib_tracker.groups[sole_group_id]
self.assertEqual(sole_group.stones, {0, 1, N, N+1})
self.assertEqual(sole_group.liberties, {2, N+2, 2*N, 2*N+1})
self.assertEqual(captured, set())
def test_same_opponent_group_neighboring_twice(self):
board = load_board('''
XX.................
X..................
''' + EMPTY_ROW * 17)
libtracker = LibertyTracker.from_board(board)
new_lib_tracker, captured = libtracker.add_stone(WHITE, N+1)
self.assertEqual(len(new_lib_tracker.groups), 2)
black_group = new_lib_tracker.groups[new_lib_tracker.group_index[0]]
self.assertEqual(black_group.stones, {0, 1, N})
self.assertEqual(black_group.liberties, {2, 2*N})
white_group = new_lib_tracker.groups[new_lib_tracker.group_index[N+1]]
self.assertEqual(white_group.stones, {N+1})
self.assertEqual(white_group.liberties, {N+2, 2*N+1})
self.assertEqual(captured, set())
class TestPosition(unittest.TestCase):
def test_capture_and_play(self):
board = load_board('''
OX.................
''' + EMPTY_ROW * 18)
position = Position(board, None, LibertyTracker.from_board(board))
captured_position = position.play_move(N, BLACK)
captured_board = load_board('''
.X.................
X..................
''' + EMPTY_ROW * 17)
self.assertEqual(captured_position.board, captured_board)
filled_in_position = captured_position.play_move(0, BLACK)
filled_in_board = load_board('''
XX.................
X..................
''' + EMPTY_ROW * 17)
self.assertEqual(filled_in_position.board, filled_in_board)
if __name__ == '__main__':
unittest.main()