@@ -15,79 +15,116 @@ def __init__(self, size=19):
15
15
self .turns_played = 0
16
16
self .current_player = BLACK
17
17
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
+
19
29
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 :
21
31
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 :
23
33
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 :
25
35
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 :
27
37
q = q + 1
28
38
return q
29
39
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
+ """
31
50
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 ]))
40
59
return tuple (pos )
41
60
42
61
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
+
43
71
lib_considered = []
44
72
curr_liberties = np .ones ((self .size , self .size ))* (- 1 )
45
73
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 ):
48
76
# 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
51
79
52
- if self .board [i ][ j ] == 0 :
80
+ if self .board [x ][ y ] == EMPTY :
53
81
continue
54
82
# The first position picked
55
83
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 ):
58
86
lib_set .append (p )
59
87
60
88
# 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 ):
63
91
lib_set .append (p )
64
- j = j + 1
92
+ y = y + 1
65
93
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 ):
68
96
lib_set .append (p )
69
- i = i + 1
97
+ x = x + 1
70
98
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 ):
73
101
lib_set .append (p )
74
- i = i - 1
102
+ x = x - 1
75
103
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 ):
78
106
lib_set .append (p )
79
- j = j - 1
107
+ y = y - 1
80
108
81
- i = icopy
82
- j = jcopy
109
+ x = xcopy
110
+ y = ycopy
83
111
# Combine the liberty position of the cluster found
84
112
lib_set = set (tuple (lib_set ))
85
- curr_liberties [i ][ j ] = len (lib_set )
113
+ curr_liberties [x ][ y ] = len (lib_set )
86
114
87
115
return curr_liberties
88
116
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 )
91
128
future = self .copy ()
92
129
future .do_move (action )
93
130
future_liberties = future .update_current_liberties ()
0 commit comments