diff --git a/AlphaGo/__init__.py b/AlphaGo/__init__.py new file mode 100644 index 0000000..ae1e483 --- /dev/null +++ b/AlphaGo/__init__.py @@ -0,0 +1,7 @@ +# -*- coding: utf-8 -*- +# vim:fenc=utf-8 +# $File: __init__.py +# $Date: Thu Nov 30 23:1446 2017 +0800 +# $Author: renyong15 © +# + diff --git a/AlphaGo/game.py b/AlphaGo/game.py index 192697a..88e5d17 100644 --- a/AlphaGo/game.py +++ b/AlphaGo/game.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # vim:fenc=utf-8 # $File: game.py -# $Date: Tue Nov 28 14:4726 2017 +0800 +# $Date: Fri Dec 01 01:3738 2017 +0800 # $Author: renyong15 © # from __future__ import print_function @@ -46,6 +46,17 @@ class Executor: return False, block return True, block + def _find_boarder(self, vertex): + block = [] + status = [False] * (self.game.size * self.game.size) + self._bfs(vertex, utils.EMPTY, block, status, False) + border = [] + for b in block: + for n in self._neighbor(b): + if not (n in block): + border.append(n) + return border + def _is_qi(self, color, vertex): nei = self._neighbor(vertex) for n in nei: @@ -134,14 +145,50 @@ class Executor: self.game.past.append(copy.copy(self.game.board)) return True + def _find_empty(self): + idx = [i for i,x in enumerate(self.game.board) if x == utils.EMPTY ][0] + return self.game._deflatten(idx) + + + + + def get_score(self): + ''' + return score from BLACK perspective. + ''' + _board = copy.copy(self.game.board) + while utils.EMPTY in self.game.board: + vertex = self._find_empty() + boarder = self._find_boarder(vertex) + boarder_color = set(map(lambda v: self.game.board[self.game._flatten(v)], boarder)) + if boarder_color == {utils.BLACK}: + self.game.board[self.game._flatten(vertex)] = utils.BLACK + elif boarder_color == {utils.WHITE}: + self.game.board[self.game._flatten(vertex)] = utils.WHITE + else: + self.game.board[self.game._flatten(vertex)] = utils.UNKNOWN + + score = 0 + for i in self.game.board: + if i == utils.BLACK: + score += 1 + elif i == utils.WHITE: + score -= 1 + score -= self.game.komi + + self.game.board = _board + return score + + class Game: def __init__(self, size=19, komi=6.5): self.size = size self.komi = 6.5 self.board = [utils.EMPTY] * (self.size * self.size) - self.strategy = strategy() - self.executor = Executor(game=self) + #self.strategy = strategy() + self.strategy = None + self.executor = Executor(game = self) self.history = [] self.past = deque(maxlen=8) for i in range(8): @@ -151,6 +198,12 @@ class Game: x, y = vertex return (y - 1) * self.size + (x - 1) + def _deflatten(self, idx): + x = idx % self.size + 1 + y = idx // self.size + 1 + return (x,y) + + def clear(self): self.board = [utils.EMPTY] * (self.size * self.size) diff --git a/AlphaGo/strategy.py b/AlphaGo/strategy.py index 99a8e4d..55e73c6 100644 --- a/AlphaGo/strategy.py +++ b/AlphaGo/strategy.py @@ -1,3 +1,5 @@ +import os,sys +sys.path.append(os.path.join(os.path.dirname(__file__), os.path.pardir)) import numpy as np import utils import time diff --git a/AlphaGo/test.py b/AlphaGo/test.py index 59c5a26..66bf131 100644 --- a/AlphaGo/test.py +++ b/AlphaGo/test.py @@ -1,7 +1,7 @@ # -*- coding: utf-8 -*- # vim:fenc=utf-8 # $File: test.py -# $Date: Tue Nov 28 14:4717 2017 +0800 +# $Date: Fri Dec 01 01:3722 2017 +0800 # $Author: renyong15 © # @@ -15,23 +15,24 @@ res = e.run_cmd('1 protocol_version') print(e.known_commands) print(res) -res = e.run_cmd('2 name') -print(res) -res = e.run_cmd('3 known_command quit') -print(res) +#res = e.run_cmd('2 name') +#print(res) -res = e.run_cmd('4 unknown_command quitagain') -print(res) +#res = e.run_cmd('3 known_command quit') +#print(res) -res = e.run_cmd('5 list_commands') -print(res) +#res = e.run_cmd('4 unknown_command quitagain') +#print(res) -res = e.run_cmd('6 komi 6') -print(res) +#res = e.run_cmd('5 list_commands') +#print(res) -res = e.run_cmd('7 play BLACK C3') -print(res) +#res = e.run_cmd('6 komi 6') +#print(res) + +#res = e.run_cmd('7 play BLACK C3') +#print(res) # res = e.run_cmd('play BLACK C4') # res = e.run_cmd('play BLACK C5') @@ -40,9 +41,9 @@ print(res) # print(res) -res = e.run_cmd('8 genmove WHITE') -print(res) -g.show_board() +#res = e.run_cmd('8 genmove WHITE') +#print(res) +#g.show_board() # res = e.run_cmd('8 genmove BLACK') # print(res) @@ -105,33 +106,33 @@ g.show_board() # print(res) # #g.show_board() # -# res = e.run_cmd('play BLACK P16') -# res = e.run_cmd('play BLACK P17') -# res = e.run_cmd('play BLACK P18') -# res = e.run_cmd('play BLACK P19') -# res = e.run_cmd('play BLACK Q16') -# res = e.run_cmd('play BLACK R16') -# res = e.run_cmd('play BLACK S16') -# -# res = e.run_cmd('play WHITE S18') -# res = e.run_cmd('play WHITE S17') -# res = e.run_cmd('play WHITE Q19') -# res = e.run_cmd('play WHITE Q18') -# res = e.run_cmd('play WHITE Q17') -# res = e.run_cmd('play WHITE R18') -# res = e.run_cmd('play WHITE R17') -# res = e.run_cmd('play BLACK S19') +res = e.run_cmd('play BLACK P16') +res = e.run_cmd('play BLACK P17') +res = e.run_cmd('play BLACK P18') +res = e.run_cmd('play BLACK P19') +res = e.run_cmd('play BLACK Q16') +res = e.run_cmd('play BLACK R16') +res = e.run_cmd('play BLACK S16') + +res = e.run_cmd('play WHITE S18') +res = e.run_cmd('play WHITE S17') +res = e.run_cmd('play WHITE Q19') +res = e.run_cmd('play WHITE Q18') +res = e.run_cmd('play WHITE Q17') +res = e.run_cmd('play WHITE R18') +res = e.run_cmd('play WHITE R17') +res = e.run_cmd('play BLACK S19') # print(res) # #g.show_board() # -# res = e.run_cmd('play WHITE R19') +res = e.run_cmd('play WHITE R19') # g.show_board() # -# res = e.run_cmd('play BLACK S19') +res = e.run_cmd('play BLACK S19') # print(res) # g.show_board() # -# res = e.run_cmd('play BLACK S19') +res = e.run_cmd('play BLACK S19') # print(res) # # @@ -153,7 +154,10 @@ g.show_board() # # res = e.run_cmd('play BLACK G19') # res = e.run_cmd('play BLACK G17') -# g.show_board() - +g.show_board() +res = e.run_cmd('play WHITE S18') +g.show_board() +res = g.executor.get_score() +print(res)