start the player server in a more robost way.
This commit is contained in:
parent
4fc50c5f1b
commit
d115c586d4
110
AlphaGo/play.py
110
AlphaGo/play.py
@ -4,63 +4,69 @@ import re
|
|||||||
import Pyro4
|
import Pyro4
|
||||||
import time
|
import time
|
||||||
|
|
||||||
#start a name server to find the remote object
|
if __name__ == '__main__':
|
||||||
kill_old_server = subprocess.Popen(['killall', 'pyro4-ns'])
|
# start a name server to find the remote object
|
||||||
print "kill old server, the return code is : " + str(kill_old_server.wait())
|
kill_old_server = subprocess.Popen(['killall', 'pyro4-ns'])
|
||||||
time.sleep(1)
|
print "kill old server, the return code is : " + str(kill_old_server.wait())
|
||||||
start_new_server = subprocess.Popen(['pyro4-ns', '&'])
|
time.sleep(1)
|
||||||
print "Start Name Sever : " + str(start_new_server.pid)# + str(start_new_server.wait())
|
start_new_server = subprocess.Popen(['pyro4-ns', '&'])
|
||||||
time.sleep(1)
|
print "Start Name Sever : " + str(start_new_server.pid) # + str(start_new_server.wait())
|
||||||
agent_v0 = subprocess.Popen(['python', '-u', 'player.py', '--role=black'],
|
time.sleep(1)
|
||||||
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
agent_v0 = subprocess.Popen(['python', '-u', 'player.py', '--role=black'],
|
||||||
print "Start Player 0 at : " + str(agent_v0.pid)
|
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
||||||
agent_v1 = subprocess.Popen(['python', '-u', 'player.py', '--role=white', '--checkpoint_path=./checkpoints_origin/'],
|
agent_v1 = subprocess.Popen(['python', '-u', 'player.py', '--role=white', '--checkpoint_path=./checkpoints_origin/'],
|
||||||
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
|
||||||
print "Start Player 1 at : " + str(agent_v1.pid)
|
server_list = ""
|
||||||
time.sleep(5)
|
while ("black" not in server_list) or ("white" not in server_list):
|
||||||
|
server_list = subprocess.check_output(['pyro4-nsc', 'list'])
|
||||||
|
print "Waining for the server start..."
|
||||||
|
time.sleep(1)
|
||||||
|
print server_list
|
||||||
|
print "Start black player at : " + str(agent_v0.pid)
|
||||||
|
print "Start white player at : " + str(agent_v1.pid)
|
||||||
|
|
||||||
player = [None] * 2
|
player = [None] * 2
|
||||||
player[0] = Pyro4.Proxy("PYRONAME:black")
|
player[0] = Pyro4.Proxy("PYRONAME:black")
|
||||||
player[1] = Pyro4.Proxy("PYRONAME:white")
|
player[1] = Pyro4.Proxy("PYRONAME:white")
|
||||||
|
|
||||||
role = ["BLACK", "WHITE"]
|
role = ["BLACK", "WHITE"]
|
||||||
color = ['b', 'w']
|
color = ['b', 'w']
|
||||||
|
|
||||||
pattern = "[A-Z]{1}[0-9]{1}"
|
pattern = "[A-Z]{1}[0-9]{1}"
|
||||||
size = 9
|
size = 9
|
||||||
show = ['.', 'X', 'O']
|
show = ['.', 'X', 'O']
|
||||||
|
|
||||||
game_num = 0
|
game_num = 0
|
||||||
while game_num < 1:
|
while game_num < 1:
|
||||||
num = 0
|
num = 0
|
||||||
pass_flag = [False, False]
|
pass_flag = [False, False]
|
||||||
print("Start game {}".format(game_num))
|
print("Start game {}".format(game_num))
|
||||||
# end the game if both palyer chose to pass, or play too much turns
|
# end the game if both palyer chose to pass, or play too much turns
|
||||||
while not (pass_flag[0] and pass_flag[1]) and num < size ** 2 * 2:
|
while not (pass_flag[0] and pass_flag[1]) and num < size ** 2 * 2:
|
||||||
turn = num % 2
|
turn = num % 2
|
||||||
move = player[turn].run_cmd(str(num) + ' genmove ' + color[turn] + '\n')
|
move = player[turn].run_cmd(str(num) + ' genmove ' + color[turn] + '\n')
|
||||||
print role[turn] + " : " + str(move),
|
print role[turn] + " : " + str(move),
|
||||||
num += 1
|
num += 1
|
||||||
match = re.search(pattern, move)
|
match = re.search(pattern, move)
|
||||||
if match is not None:
|
if match is not None:
|
||||||
#print "match : " + str(match.group())
|
# print "match : " + str(match.group())
|
||||||
play_or_pass = match.group()
|
play_or_pass = match.group()
|
||||||
pass_flag[turn] = False
|
pass_flag[turn] = False
|
||||||
else:
|
else:
|
||||||
#print "no match"
|
# print "no match"
|
||||||
play_or_pass = ' PASS'
|
play_or_pass = ' PASS'
|
||||||
pass_flag[turn] = True
|
pass_flag[turn] = True
|
||||||
result = player[1 - turn].run_cmd(str(num) + ' play ' + color[turn] + ' ' + play_or_pass + '\n')
|
result = player[1 - turn].run_cmd(str(num) + ' play ' + color[turn] + ' ' + play_or_pass + '\n')
|
||||||
board = player[turn].run_cmd(str(num) + ' show_board')
|
board = player[turn].run_cmd(str(num) + ' show_board')
|
||||||
board = eval(board[board.index('['):board.index(']') + 1])
|
board = eval(board[board.index('['):board.index(']') + 1])
|
||||||
for i in range(size):
|
for i in range(size):
|
||||||
for j in range(size):
|
for j in range(size):
|
||||||
print show[board[i * size + j]] + " ",
|
print show[board[i * size + j]] + " ",
|
||||||
print "\n",
|
print "\n",
|
||||||
|
|
||||||
score = player[turn].run_cmd(str(num) + ' get_score')
|
score = player[turn].run_cmd(str(num) + ' get_score')
|
||||||
print "Finished : ", score.split(" ")[1]
|
print "Finished : ", score.split(" ")[1]
|
||||||
player[0].run_cmd(str(num) + ' clear_board')
|
player[0].run_cmd(str(num) + ' clear_board')
|
||||||
player[1].run_cmd(str(num) + ' clear_board')
|
player[1].run_cmd(str(num) + ' clear_board')
|
||||||
game_num += 1
|
game_num += 1
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user