Skip to content

Commit 8f4059c

Browse files
committed
go-counting: Rename class name & move constants
1 parent 97e6d7f commit 8f4059c

File tree

3 files changed

+45
-44
lines changed

3 files changed

+45
-44
lines changed

exercises/go-counting/example.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11

2-
class GoCounting:
2+
BLACK = "B"
3+
WHITE = "W"
4+
NONE = ""
5+
STONES = [BLACK, WHITE]
6+
DIRECTIONS = [(0, 1), (0, -1), (1, 0), (-1, 0)]
37

4-
none = ""
5-
white = "W"
6-
black = "B"
7-
stones = [black, white]
8-
directions = [(0, 1), (0, -1), (1, 0), (-1, 0)]
98

9+
class Board:
1010
def __init__(self, board):
1111
self.board = board.splitlines()
1212
self.width = len(self.board[0])
@@ -15,21 +15,21 @@ def __init__(self, board):
1515
def valid(self, x, y):
1616
return x >= 0 and x < self.width and y >= 0 and y < self.height
1717

18-
def walk_board(self, x, y,
19-
visited_territory=[],
20-
visited_coords=[],
21-
visited_stones=[]):
18+
def walk(self, x, y,
19+
visited_territory=[],
20+
visited_coords=[],
21+
visited_stones=[]):
2222
if not (x, y) in visited_coords and self.valid(x, y):
2323
s = self.board[y][x]
24-
if s in self.stones:
24+
if s in STONES:
2525
if s not in visited_stones:
2626
return (visited_territory, visited_stones + [s])
2727
else: # s is empty
28-
for d in self.directions:
29-
visited = self.walk_board(x + d[0], y + d[1],
30-
visited_territory + [(x, y)],
31-
visited_coords + [(x, y)],
32-
visited_stones)
28+
for d in DIRECTIONS:
29+
visited = self.walk(x + d[0], y + d[1],
30+
visited_territory + [(x, y)],
31+
visited_coords + [(x, y)],
32+
visited_stones)
3333
visited_territory = visited[0]
3434
visited_stones = visited[1]
3535

@@ -38,18 +38,18 @@ def walk_board(self, x, y,
3838
def territoryFor(self, coord):
3939
assert len(coord) == 2
4040
x, y = coord[0], coord[1]
41-
if not self.valid(x, y) or self.board[y][x] in self.stones:
42-
return (self.none, set())
41+
if not self.valid(x, y) or self.board[y][x] in STONES:
42+
return (NONE, set())
4343

44-
visited_territory, visited_stones = self.walk_board(x, y)
44+
visited_territory, visited_stones = self.walk(x, y)
4545
result = set(visited_territory)
4646

4747
if len(visited_stones) == 1:
4848
return (visited_stones[0], result)
49-
return (self.none, result)
49+
return (NONE, result)
5050

5151
def territories(self):
52-
owners = self.stones + [self.none]
52+
owners = STONES + [NONE]
5353
result = dict([(owner, set()) for owner in owners])
5454
visited = set()
5555
for y in range(self.height):

exercises/go-counting/go_counting.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11

2-
class GoCounting:
2+
class Board:
33
"""Count territories of each player in a Go game
44
55
Args:

exercises/go-counting/go_counting_test.py

Lines changed: 23 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import unittest
22
import gocounting
33

4+
45
# Tests adapted from `problem-specifications//canonical-data.json` @ v1.1.0
56

67
board5x5 = "\n".join([
@@ -26,63 +27,63 @@
2627

2728
class GoCountingTest(unittest.TestCase):
2829
def test_5x5_for_black(self):
29-
board = gocounting.GoCounting(board5x5)
30+
board = gocounting.Board(board5x5)
3031
stone, territory = board.territoryFor((0, 1))
31-
self.assertEqual(stone, board.black)
32+
self.assertEqual(stone, gocounting.BLACK)
3233
self.assertEqual(territory, set([(0, 0), (0, 1), (1, 0)]))
3334

3435
def test_5x5_for_white(self):
35-
board = gocounting.GoCounting(board5x5)
36+
board = gocounting.Board(board5x5)
3637
stone, territory = board.territoryFor((2, 3))
37-
self.assertEqual(stone, board.white)
38+
self.assertEqual(stone, gocounting.WHITE)
3839
self.assertEqual(territory, set([(2, 3)]))
3940

4041
def test_5x5_for_open_territory(self):
41-
board = gocounting.GoCounting(board5x5)
42+
board = gocounting.Board(board5x5)
4243
stone, territory = board.territoryFor((1, 4))
43-
self.assertEqual(stone, board.none)
44+
self.assertEqual(stone, gocounting.NONE)
4445
self.assertEqual(territory, set([(0, 3), (0, 4), (1, 4)]))
4546

4647
def test_5x5_for_non_territory(self):
47-
board = gocounting.GoCounting(board5x5)
48+
board = gocounting.Board(board5x5)
4849
stone, territory = board.territoryFor((1, 1))
49-
self.assertEqual(stone, board.none)
50+
self.assertEqual(stone, gocounting.NONE)
5051
self.assertEqual(territory, set())
5152

5253
def test_5x5_for_valid_coordinate(self):
53-
board = gocounting.GoCounting(board5x5)
54+
board = gocounting.Board(board5x5)
5455
stone, territory = board.territoryFor((-1, 1))
55-
self.assertEqual(stone, board.none)
56+
self.assertEqual(stone, gocounting.NONE)
5657
self.assertEqual(territory, set())
5758

5859
def test_5x5_for_valid_coordinate2(self):
59-
board = gocounting.GoCounting(board5x5)
60+
board = gocounting.Board(board5x5)
6061
stone, territory = board.territoryFor((1, 5))
61-
self.assertEqual(stone, board.none)
62+
self.assertEqual(stone, gocounting.NONE)
6263
self.assertEqual(territory, set())
6364

6465
def test_one_territory_whole_board(self):
65-
board = gocounting.GoCounting(" ")
66+
board = gocounting.Board(" ")
6667
territories = board.territories()
67-
self.assertEqual(territories[board.black], set())
68-
self.assertEqual(territories[board.white], set())
69-
self.assertEqual(territories[board.none], set([(0, 0)]))
68+
self.assertEqual(territories[gocounting.BLACK], set())
69+
self.assertEqual(territories[gocounting.WHITE], set())
70+
self.assertEqual(territories[gocounting.NONE], set([(0, 0)]))
7071

7172
def test_two_territories_rectangular_board(self):
7273
input_board = "\n".join([
7374
" BW ",
7475
" BW "
7576
])
76-
board = gocounting.GoCounting(input_board)
77+
board = gocounting.Board(input_board)
7778
territories = board.territories()
78-
self.assertEqual(territories[board.black], set([(0, 0), (0, 1)]))
79-
self.assertEqual(territories[board.white], set([(3, 0), (3, 1)]))
80-
self.assertEqual(territories[board.none], set())
79+
self.assertEqual(territories[gocounting.BLACK], set([(0, 0), (0, 1)]))
80+
self.assertEqual(territories[gocounting.WHITE], set([(3, 0), (3, 1)]))
81+
self.assertEqual(territories[gocounting.NONE], set())
8182

8283
def test_9x9_for_open_territory(self):
83-
board = gocounting.GoCounting(board9x9)
84+
board = gocounting.Board(board9x9)
8485
stone, territory = board.territoryFor((0, 8))
85-
self.assertEqual(stone, board.none)
86+
self.assertEqual(stone, gocounting.NONE)
8687
self.assertEqual(territory,
8788
set([(2, 7), (2, 8), (1, 8), (0, 8), (0, 7)]))
8889

0 commit comments

Comments
 (0)