add '()' to support python3

This commit is contained in:
mcgrady00h 2017-12-25 15:33:17 +08:00
parent 9583a14856
commit 0fdbaef1a1

View File

@ -5,7 +5,14 @@ import re
import Pyro4 import Pyro4
import time import time
import os import os
import cPickle
python_version = sys.version_info
if python_version < (3, 0):
import cPickle
else:
import _pickle as cPickle
class Data(object): class Data(object):
def __init__(self): def __init__(self):
@ -53,7 +60,7 @@ if __name__ == '__main__':
# start a name server if no name server exists # start a name server if no name server exists
if len(os.popen('ps aux | grep pyro4-ns | grep -v grep').readlines()) == 0: if len(os.popen('ps aux | grep pyro4-ns | grep -v grep').readlines()) == 0:
start_new_server = subprocess.Popen(['pyro4-ns', '&']) start_new_server = subprocess.Popen(['pyro4-ns', '&'])
print "Start Name Sever : " + str(start_new_server.pid) # + str(start_new_server.wait()) print("Start Name Sever : " + str(start_new_server.pid)) # + str(start_new_server.wait())
time.sleep(1) time.sleep(1)
# start two different player with different network weights. # start two different player with different network weights.
@ -73,12 +80,15 @@ if __name__ == '__main__':
server_list = "" server_list = ""
while (black_role_name not in server_list) or (white_role_name not in server_list): while (black_role_name not in server_list) or (white_role_name not in server_list):
server_list = subprocess.check_output(['pyro4-nsc', 'list']) if python_version < (3, 0):
print "Waiting for the server start..." server_list = subprocess.check_output(['pyro4-nsc', 'list'])
else:
server_list = subprocess.check_output(['pyro4-nsc', 'list'])
print("Waiting for the server start...")
time.sleep(1) time.sleep(1)
print server_list print(server_list)
print "Start black player at : " + str(agent_v0.pid) print("Start black player at : " + str(agent_v0.pid))
print "Start white player at : " + str(agent_v1.pid) print("Start white player at : " + str(agent_v1.pid))
data = Data() data = Data()
player = [None] * 2 player = [None] * 2
@ -109,12 +119,12 @@ if __name__ == '__main__':
board = eval(board[board.index('['):board.index(']') + 1]) board = eval(board[board.index('['):board.index(']') + 1])
for i in range(size[game_name]): for i in range(size[game_name]):
for j in range(size[game_name]): for j in range(size[game_name]):
print show[board[i * size[game_name] + j]] + " ", print(show[board[i * size[game_name] + j]] + " ",)
print "\n", print("\n",)
data.boards.append(board) data.boards.append(board)
start_time = time.time() start_time = time.time()
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:
@ -133,7 +143,7 @@ if __name__ == '__main__':
prob = eval(prob) prob = eval(prob)
data.probs.append(prob) data.probs.append(prob)
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])
# TODO: generalize the player # TODO: generalize the player
if eval(score.split(" ")[1]) > 0: if eval(score.split(" ")[1]) > 0:
data.winner = 1 data.winner = 1
@ -157,8 +167,8 @@ if __name__ == '__main__':
print(e) print(e)
subprocess.call(["kill", "-9", str(agent_v0.pid)]) subprocess.call(["kill", "-9", str(agent_v0.pid)])
subprocess.call(["kill", "-9", str(agent_v1.pid)]) subprocess.call(["kill", "-9", str(agent_v1.pid)])
print "Kill all player, finish all game." print("Kill all player, finish all game.")
subprocess.call(["kill", "-9", str(agent_v0.pid)]) subprocess.call(["kill", "-9", str(agent_v0.pid)])
subprocess.call(["kill", "-9", str(agent_v1.pid)]) subprocess.call(["kill", "-9", str(agent_v1.pid)])
print "Kill all player, finish all game." print("Kill all player, finish all game.")