Skip to content

Commit 117e28a

Browse files
committed
Small changes
docstring, x,y, EMPTY
1 parent d9794a1 commit 117e28a

File tree

2 files changed

+78
-42
lines changed

2 files changed

+78
-42
lines changed

AlphaGo/go.py

Lines changed: 75 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -15,79 +15,116 @@ def __init__(self, size=19):
1515
self.turns_played = 0
1616
self.current_player = BLACK
1717

18-
def liberty_count(self, i, j):
18+
def liberty_count(self, x, y):
19+
"""Count liberty of a single position (maxium = 4).
20+
21+
Keyword arguments:
22+
x -- The column index of the position we want to calculate the liberty
23+
y -- The row index of the position we want to calculate the liberty
24+
25+
Return:
26+
q -- A interger in [0, 4]. The count of liberty of the input single position
27+
"""
28+
1929
q=0 #liberty count
20-
if i+1 < 19 and self.board[i+1][j] == 0:
30+
if x+1 < self.size and self.board[x+1][y] == EMPTY:
2131
q = q + 1
22-
if j+1 < 19 and self.board[i][j+1] == 0:
32+
if y+1 < self.size and self.board[x][y+1] == EMPTY:
2333
q = q + 1
24-
if i-1 > 0 and self.board[i-1][j] == 0:
34+
if x-1 > 0 and self.board[x-1][y] == EMPTY:
2535
q = q + 1
26-
if j -1 > 0 and self.board[i][j -1] == 0:
36+
if y -1 > 0 and self.board[x][y -1] == EMPTY:
2737
q = q + 1
2838
return q
2939

30-
def liberty_pos(self, i, j):
40+
def liberty_pos(self, x, y):
41+
"""Record the liberty position of a single position.
42+
43+
Keyword arguments:
44+
x -- The column index of the position we want to calculate the liberty
45+
y -- The row index of the position we want to calculate the liberty
46+
47+
Return:
48+
tuple(pos) -- Return a tuple of tuples consist of (x, y)s which are the liberty positions on the input single position. len(tuple(pos)) <= 4
49+
"""
3150
pos=[]
32-
if i+1<19 and self.board[i+1][j] == 0:
33-
pos.append(tuple([i+1, j]))
34-
if j+1<19 and self.board[i][j+1] == 0:
35-
pos.append(tuple([i, j+1]))
36-
if i - 1 >= 0 and self.board[i-1][j] == 0:
37-
pos.append(tuple([i-1, j]))
38-
if j - 1 >= 0 and self.board[i][j-1] == 0:
39-
pos.append(tuple([i, j-1]))
51+
if x+1<self.size and self.board[x+1][y] == EMPTY:
52+
pos.append(tuple([x+1, y]))
53+
if y+1<self.size and self.board[x][y+1] == EMPTY:
54+
pos.append(tuple([x, y+1]))
55+
if x - 1 >= 0 and self.board[x-1][y] == EMPTY:
56+
pos.append(tuple([x-1, y]))
57+
if y - 1 >= 0 and self.board[x][y-1] == EMPTY:
58+
pos.append(tuple([x, y-1]))
4059
return tuple(pos)
4160

4261
def update_current_liberties(self):
62+
"""Calculate the liberty values of the whole board
63+
64+
Keyword arguments:
65+
None. We just need the board itself.
66+
67+
Return:
68+
A matrix self.size * self.size, with entries of the liberty number of each position on the board.
69+
"""
70+
4371
lib_considered=[]
4472
curr_liberties=np.ones((self.size, self.size))*(-1)
4573

46-
for i in range(0, self.size):
47-
for j in range(0, self.size):
74+
for x in range(0, self.size):
75+
for y in range(0, self.size):
4876
# make a copy of the current coordinate, so we don't loose track after performing the search in 4 different directions
49-
icopy=i
50-
jcopy=j
77+
xcopy=x
78+
ycopy=y
5179

52-
if self.board[i][j] == 0:
80+
if self.board[x][y] == EMPTY:
5381
continue
5482
# The first position picked
5583
lib_set = []
56-
lib_c = self.liberty_count(i, j)
57-
for p in self.liberty_pos(i, j):
84+
lib_c = self.liberty_count(x, y)
85+
for p in self.liberty_pos(x, y):
5886
lib_set.append(p)
5987

6088
# Scanning through 4 directions to find the same color cluster
61-
while j+1<19 and self.board[i][j]==self.board[i][j+1]:
62-
for p in self.liberty_pos(i, j+1):
89+
while y+1<self.size and self.board[x][y]==self.board[x][y+1]:
90+
for p in self.liberty_pos(x, y+1):
6391
lib_set.append(p)
64-
j = j + 1
92+
y = y + 1
6593

66-
while i+1<19 and self.board[i][j] == self.board[i+1][j]:
67-
for p in self.liberty_pos(i+1, j):
94+
while x+1<self.size and self.board[x][y] == self.board[x+1][y]:
95+
for p in self.liberty_pos(x+1, y):
6896
lib_set.append(p)
69-
i = i + 1
97+
x = x + 1
7098

71-
while i - 1 >= 0 and self.board[i][j] == self.board[i-1][j]:
72-
for p in self.liberty_pos(i-1, j):
99+
while x - 1 >= 0 and self.board[x][y] == self.board[x-1][y]:
100+
for p in self.liberty_pos(x-1, y):
73101
lib_set.append(p)
74-
i = i - 1
102+
x = x - 1
75103

76-
while j - 1 >= 0 and self.board[i][j] == self.board[i][j-1]:
77-
for p in self.liberty_pos(i, j-1):
104+
while y - 1 >= 0 and self.board[x][y] == self.board[x][y-1]:
105+
for p in self.liberty_pos(x, y-1):
78106
lib_set.append(p)
79-
j = j - 1
107+
y = y - 1
80108

81-
i = icopy
82-
j = jcopy
109+
x = xcopy
110+
y = ycopy
83111
# Combine the liberty position of the cluster found
84112
lib_set = set(tuple(lib_set))
85-
curr_liberties[i][j] = len(lib_set)
113+
curr_liberties[x][y] = len(lib_set)
86114

87115
return curr_liberties
88116

89-
def update_future_liberties(self, action):
90-
(i,j) = action
117+
def update_future_liberties(self, x, y):
118+
"""Calculate the liberty values of the whole board after we make a new move
119+
120+
Keyword arguments:
121+
x -- The column index of the position of the future move
122+
y -- The row index of the position of the future move
123+
124+
Return:
125+
A matrix self.size * self.size, with entries of the liberty number of each position on the board, after the future move.
126+
"""
127+
action = (x, y)
91128
future = self.copy()
92129
future.do_move(action)
93130
future_liberties = future.update_current_liberties()

tests/test_liberties.py

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,8 @@ def test_curr_liberties(self):
3030
print("curr_liberties checked")
3131

3232
def test_future_liberties(self):
33-
34-
self.assertEqual(self.s.update_future_liberties((6,5))[6][5], 4)
35-
self.assertEqual(self.s.update_future_liberties((5,4))[5][4], 4)
36-
self.assertEqual(self.s.update_future_liberties((6,6))[5][6], 5)
33+
self.assertEqual(self.s.update_future_liberties(6,5)[6][5], 4)
34+
self.assertEqual(self.s.update_future_liberties(5,4)[5][4], 4)
35+
self.assertEqual(self.s.update_future_liberties(6,6)[5][6], 5)
3736

3837
print("future_liberties checked")

0 commit comments

Comments
 (0)