delete unused parameter of _find_block, and using _find_group to replace _find_block
This commit is contained in:
parent
7693c38f44
commit
ea52096713
@ -13,25 +13,24 @@ Settings of the Go game.
|
|||||||
|
|
||||||
NEIGHBOR_OFFSET = [[1, 0], [-1, 0], [0, -1], [0, 1]]
|
NEIGHBOR_OFFSET = [[1, 0], [-1, 0], [0, -1], [0, 1]]
|
||||||
|
|
||||||
|
|
||||||
class Go:
|
class Go:
|
||||||
def __init__(self, **kwargs):
|
def __init__(self, **kwargs):
|
||||||
self.game = kwargs['game']
|
self.game = kwargs['game']
|
||||||
|
|
||||||
def _bfs(self, vertex, color, block, status, alive_break):
|
def _bfs(self, vertex, color, block, status):
|
||||||
block.append(vertex)
|
block.append(vertex)
|
||||||
status[self.game._flatten(vertex)] = True
|
status[self.game._flatten(vertex)] = True
|
||||||
nei = self._neighbor(vertex)
|
nei = self._neighbor(vertex)
|
||||||
for n in nei:
|
for n in nei:
|
||||||
if not status[self.game._flatten(n)]:
|
if not status[self.game._flatten(n)]:
|
||||||
if self.game.board[self.game._flatten(n)] == color:
|
if self.game.board[self.game._flatten(n)] == color:
|
||||||
self._bfs(n, color, block, status, alive_break)
|
self._bfs(n, color, block, status)
|
||||||
|
|
||||||
def _find_block(self, vertex, alive_break=False):
|
def _find_block(self, vertex):
|
||||||
block = []
|
block = []
|
||||||
status = [False] * (self.game.size * self.game.size)
|
status = [False] * (self.game.size * self.game.size)
|
||||||
color = self.game.board[self.game._flatten(vertex)]
|
color = self.game.board[self.game._flatten(vertex)]
|
||||||
self._bfs(vertex, color, block, status, alive_break)
|
self._bfs(vertex, color, block, status)
|
||||||
|
|
||||||
for b in block:
|
for b in block:
|
||||||
for n in self._neighbor(b):
|
for n in self._neighbor(b):
|
||||||
@ -42,7 +41,7 @@ class Go:
|
|||||||
def _find_boarder(self, vertex):
|
def _find_boarder(self, vertex):
|
||||||
block = []
|
block = []
|
||||||
status = [False] * (self.game.size * self.game.size)
|
status = [False] * (self.game.size * self.game.size)
|
||||||
self._bfs(vertex, utils.EMPTY, block, status, False)
|
self._bfs(vertex, utils.EMPTY, block, status)
|
||||||
border = []
|
border = []
|
||||||
for b in block:
|
for b in block:
|
||||||
for n in self._neighbor(b):
|
for n in self._neighbor(b):
|
||||||
@ -106,7 +105,7 @@ class Go:
|
|||||||
nei = self._neighbor(vertex)
|
nei = self._neighbor(vertex)
|
||||||
for n in nei:
|
for n in nei:
|
||||||
if self.game.board[self.game._flatten(n)] == utils.another_color(color):
|
if self.game.board[self.game._flatten(n)] == utils.another_color(color):
|
||||||
can_kill, block = self._find_block(n, alive_break=True)
|
can_kill, block = self._find_block(n)
|
||||||
if can_kill:
|
if can_kill:
|
||||||
for b in block:
|
for b in block:
|
||||||
self.game.board[self.game._flatten(b)] = utils.EMPTY
|
self.game.board[self.game._flatten(b)] = utils.EMPTY
|
||||||
|
@ -23,26 +23,32 @@ class GoEnv:
|
|||||||
x, y = vertex
|
x, y = vertex
|
||||||
return (x - 1) * self.game.size + (y - 1)
|
return (x - 1) * self.game.size + (y - 1)
|
||||||
|
|
||||||
def _bfs(self, vertex, color, block, status, alive_break):
|
def _find_group(self, start):
|
||||||
|
color = self.board[self._flatten(start)]
|
||||||
|
# print ("color : ", color)
|
||||||
|
chain = set()
|
||||||
|
frontier = [start]
|
||||||
|
has_liberty = False
|
||||||
|
while frontier:
|
||||||
|
current = frontier.pop()
|
||||||
|
# print ("current : ", current)
|
||||||
|
chain.add(current)
|
||||||
|
for n in self._neighbor(current):
|
||||||
|
# print n, self._flatten(n), self.board[self._flatten(n)],
|
||||||
|
if self.board[self._flatten(n)] == color and not n in chain:
|
||||||
|
frontier.append(n)
|
||||||
|
if self.board[self._flatten(n)] == utils.EMPTY:
|
||||||
|
has_liberty = True
|
||||||
|
return has_liberty, chain
|
||||||
|
|
||||||
|
def _bfs(self, vertex, color, block, status):
|
||||||
block.append(vertex)
|
block.append(vertex)
|
||||||
status[self._flatten(vertex)] = True
|
status[self._flatten(vertex)] = True
|
||||||
nei = self._neighbor(vertex)
|
nei = self._neighbor(vertex)
|
||||||
for n in nei:
|
for n in nei:
|
||||||
if not status[self._flatten(n)]:
|
if not status[self._flatten(n)]:
|
||||||
if self.board[self._flatten(n)] == color:
|
if self.board[self._flatten(n)] == color:
|
||||||
self._bfs(n, color, block, status, alive_break)
|
self._bfs(n, color, block, status)
|
||||||
|
|
||||||
def _find_block(self, vertex, alive_break=False):
|
|
||||||
block = []
|
|
||||||
status = [False] * (self.game.size * self.game.size)
|
|
||||||
color = self.board[self._flatten(vertex)]
|
|
||||||
self._bfs(vertex, color, block, status, alive_break)
|
|
||||||
|
|
||||||
for b in block:
|
|
||||||
for n in self._neighbor(b):
|
|
||||||
if self.board[self._flatten(n)] == utils.EMPTY:
|
|
||||||
return False, block
|
|
||||||
return True, block
|
|
||||||
|
|
||||||
def _is_qi(self, color, vertex):
|
def _is_qi(self, color, vertex):
|
||||||
nei = self._neighbor(vertex)
|
nei = self._neighbor(vertex)
|
||||||
@ -53,14 +59,14 @@ class GoEnv:
|
|||||||
self.board[self._flatten(vertex)] = color
|
self.board[self._flatten(vertex)] = color
|
||||||
for n in nei:
|
for n in nei:
|
||||||
if self.board[self._flatten(n)] == utils.another_color(color):
|
if self.board[self._flatten(n)] == utils.another_color(color):
|
||||||
can_kill, block = self._find_block(n)
|
has_liberty, group = self._find_group(n)
|
||||||
if can_kill:
|
if not has_liberty:
|
||||||
self.board[self._flatten(vertex)] = utils.EMPTY
|
self.board[self._flatten(vertex)] = utils.EMPTY
|
||||||
return True
|
return True
|
||||||
|
|
||||||
### avoid suicide
|
### avoid suicide
|
||||||
can_kill, block = self._find_block(vertex)
|
has_liberty, group = self._find_group(vertex)
|
||||||
if can_kill:
|
if not has_liberty:
|
||||||
self.board[self._flatten(vertex)] = utils.EMPTY
|
self.board[self._flatten(vertex)] = utils.EMPTY
|
||||||
return False
|
return False
|
||||||
|
|
||||||
@ -110,26 +116,11 @@ class GoEnv:
|
|||||||
nei = self._neighbor(vertex)
|
nei = self._neighbor(vertex)
|
||||||
for n in nei:
|
for n in nei:
|
||||||
if self.board[self._flatten(n)] == utils.another_color(color):
|
if self.board[self._flatten(n)] == utils.another_color(color):
|
||||||
can_kill, block = self._find_block(n, alive_break=True)
|
has_liberty, group = self._find_group(n)
|
||||||
if can_kill:
|
if not has_liberty:
|
||||||
for b in block:
|
for b in group:
|
||||||
self.board[self._flatten(b)] = utils.EMPTY
|
self.board[self._flatten(b)] = utils.EMPTY
|
||||||
|
|
||||||
def _find_group(self, start):
|
|
||||||
color = self.board[self._flatten(start)]
|
|
||||||
# print ("color : ", color)
|
|
||||||
chain = set()
|
|
||||||
frontier = [start]
|
|
||||||
while frontier:
|
|
||||||
current = frontier.pop()
|
|
||||||
# print ("current : ", current)
|
|
||||||
chain.add(current)
|
|
||||||
for n in self._neighbor(current):
|
|
||||||
# print n, self._flatten(n), self.board[self._flatten(n)],
|
|
||||||
if self.board[self._flatten(n)] == color and not n in chain:
|
|
||||||
frontier.append(n)
|
|
||||||
return chain
|
|
||||||
|
|
||||||
def _is_eye(self, color, vertex):
|
def _is_eye(self, color, vertex):
|
||||||
nei = self._neighbor(vertex)
|
nei = self._neighbor(vertex)
|
||||||
cor = self._corner(vertex)
|
cor = self._corner(vertex)
|
||||||
@ -137,7 +128,8 @@ class GoEnv:
|
|||||||
if False in ncolor:
|
if False in ncolor:
|
||||||
# print "not all neighbors are in same color with us"
|
# print "not all neighbors are in same color with us"
|
||||||
return False
|
return False
|
||||||
if set(nei) < self._find_group(nei[0]):
|
_, group = self._find_group(nei[0])
|
||||||
|
if set(nei) < group:
|
||||||
# print "all neighbors are in same group and same color with us"
|
# print "all neighbors are in same group and same color with us"
|
||||||
return True
|
return True
|
||||||
else:
|
else:
|
||||||
|
Loading…
x
Reference in New Issue
Block a user