minor fixed

This commit is contained in:
rtz19970824 2017-12-03 19:18:20 +08:00
commit a60ab99e59
5 changed files with 113 additions and 40 deletions

7
AlphaGo/__init__.py Normal file
View File

@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
# $File: __init__.py
# $Date: Thu Nov 30 23:1446 2017 +0800
# $Author: renyong15 © <mails.tsinghua.edu.cn>
#

View File

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# vim:fenc=utf-8 # vim:fenc=utf-8
# $File: game.py # $File: game.py
# $Date: Tue Nov 28 14:4726 2017 +0800 # $Date: Fri Dec 01 01:3738 2017 +0800
# $Author: renyong15 © <mails.tsinghua.edu.cn> # $Author: renyong15 © <mails.tsinghua.edu.cn>
# #
from __future__ import print_function from __future__ import print_function
@ -46,6 +46,17 @@ class Executor:
return False, block return False, block
return True, 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): def _is_qi(self, color, vertex):
nei = self._neighbor(vertex) nei = self._neighbor(vertex)
for n in nei: for n in nei:
@ -134,14 +145,50 @@ class Executor:
self.game.past.append(copy.copy(self.game.board)) self.game.past.append(copy.copy(self.game.board))
return True 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: class Game:
def __init__(self, size=19, komi=6.5): def __init__(self, size=19, komi=6.5):
self.size = size self.size = size
self.komi = 6.5 self.komi = 6.5
self.board = [utils.EMPTY] * (self.size * self.size) self.board = [utils.EMPTY] * (self.size * self.size)
self.strategy = strategy() #self.strategy = strategy()
self.executor = Executor(game=self) self.strategy = None
self.executor = Executor(game = self)
self.history = [] self.history = []
self.past = deque(maxlen=8) self.past = deque(maxlen=8)
for i in range(8): for i in range(8):
@ -151,6 +198,12 @@ class Game:
x, y = vertex x, y = vertex
return (y - 1) * self.size + (x - 1) 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): def clear(self):
self.board = [utils.EMPTY] * (self.size * self.size) self.board = [utils.EMPTY] * (self.size * self.size)

View File

@ -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 numpy as np
import utils import utils
import time import time

View File

@ -1,7 +1,7 @@
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# vim:fenc=utf-8 # vim:fenc=utf-8
# $File: test.py # $File: test.py
# $Date: Tue Nov 28 14:4717 2017 +0800 # $Date: Fri Dec 01 01:3722 2017 +0800
# $Author: renyong15 © <mails.tsinghua.edu.cn> # $Author: renyong15 © <mails.tsinghua.edu.cn>
# #
@ -15,23 +15,24 @@ res = e.run_cmd('1 protocol_version')
print(e.known_commands) print(e.known_commands)
print(res) print(res)
res = e.run_cmd('2 name')
print(res)
res = e.run_cmd('3 known_command quit') #res = e.run_cmd('2 name')
print(res) #print(res)
res = e.run_cmd('4 unknown_command quitagain') #res = e.run_cmd('3 known_command quit')
print(res) #print(res)
res = e.run_cmd('5 list_commands') #res = e.run_cmd('4 unknown_command quitagain')
print(res) #print(res)
res = e.run_cmd('6 komi 6') #res = e.run_cmd('5 list_commands')
print(res) #print(res)
res = e.run_cmd('7 play BLACK D4') #res = e.run_cmd('6 komi 6')
print(res) #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 C4')
# res = e.run_cmd('play BLACK C5') # res = e.run_cmd('play BLACK C5')
@ -40,9 +41,9 @@ print(res)
# print(res) # print(res)
res = e.run_cmd('8 genmove WHITE') #res = e.run_cmd('8 genmove WHITE')
print(res) #print(res)
g.show_board() #g.show_board()
# res = e.run_cmd('8 genmove BLACK') # res = e.run_cmd('8 genmove BLACK')
# print(res) # print(res)
@ -105,33 +106,33 @@ g.show_board()
# print(res) # print(res)
# #g.show_board() # #g.show_board()
# #
# res = e.run_cmd('play BLACK P16') res = e.run_cmd('play BLACK P16')
# res = e.run_cmd('play BLACK P17') res = e.run_cmd('play BLACK P17')
# res = e.run_cmd('play BLACK P18') res = e.run_cmd('play BLACK P18')
# res = e.run_cmd('play BLACK P19') res = e.run_cmd('play BLACK P19')
# res = e.run_cmd('play BLACK Q16') res = e.run_cmd('play BLACK Q16')
# res = e.run_cmd('play BLACK R16') res = e.run_cmd('play BLACK R16')
# res = e.run_cmd('play BLACK S16') res = e.run_cmd('play BLACK S16')
#
# res = e.run_cmd('play WHITE S18') res = e.run_cmd('play WHITE S18')
# res = e.run_cmd('play WHITE S17') res = e.run_cmd('play WHITE S17')
# res = e.run_cmd('play WHITE Q19') res = e.run_cmd('play WHITE Q19')
# res = e.run_cmd('play WHITE Q18') res = e.run_cmd('play WHITE Q18')
# res = e.run_cmd('play WHITE Q17') res = e.run_cmd('play WHITE Q17')
# res = e.run_cmd('play WHITE R18') res = e.run_cmd('play WHITE R18')
# res = e.run_cmd('play WHITE R17') res = e.run_cmd('play WHITE R17')
# res = e.run_cmd('play BLACK S19') res = e.run_cmd('play BLACK S19')
# print(res) # print(res)
# #g.show_board() # #g.show_board()
# #
# res = e.run_cmd('play WHITE R19') res = e.run_cmd('play WHITE R19')
# g.show_board() # g.show_board()
# #
# res = e.run_cmd('play BLACK S19') res = e.run_cmd('play BLACK S19')
# print(res) # print(res)
# g.show_board() # g.show_board()
# #
# res = e.run_cmd('play BLACK S19') res = e.run_cmd('play BLACK S19')
# print(res) # print(res)
# #
# #
@ -153,7 +154,10 @@ g.show_board()
# #
# res = e.run_cmd('play BLACK G19') # res = e.run_cmd('play BLACK G19')
# res = e.run_cmd('play BLACK G17') # 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)

7
__init__.py Normal file
View File

@ -0,0 +1,7 @@
# -*- coding: utf-8 -*-
# vim:fenc=utf-8
# $File: __init__.py
# $Date: Thu Nov 30 23:1430 2017 +0800
# $Author: renyong15 © <mails.tsinghua.edu.cn>
#