-
Notifications
You must be signed in to change notification settings - Fork 0
/
play.py
137 lines (116 loc) · 3.58 KB
/
play.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import pexpect
player2 ='random_player/Gomoku2.py'
player1 = 'mcts_player/Gomoku4.py'
#player2 ='flat_mc_player/Gomoku3.py'
win1 = 0
win2 = 0
numTimeout = 0
draw = 0
timeout = 2
def getMove(p,color):
p.sendline('genmove '+color)
p.expect([pexpect.TIMEOUT,'= [A-Z][0-9]','= resign','= pass'])
if p.after==pexpect.TIMEOUT:
return 'timeout'
return p.after.decode("utf-8")[2:]
def playMove(p,color,move):
p.sendline('play ' + color + ' ' + move)
def setupPlayer(p):
p.sendline('boardsize 7')
p.sendline('clear_board')
p.sendline('timelimit {}'.format(timeout))
def playSingleGame(alternative=False):
if not alternative:
p1 = pexpect.spawn('python3 ' + player1, timeout=timeout+1)
p2 = pexpect.spawn('python3 ' + player2, timeout=timeout+1)
else:
p1 = pexpect.spawn('python3 ' + player2, timeout=timeout+1)
p2 = pexpect.spawn('python3 ' + player1, timeout=timeout+1)
ob = pexpect.spawn('python3 mcts_player/Gomoku4.py')
setupPlayer(p1)
setupPlayer(p2)
result = None
numTimeout = 0
sw=0
while 1:
if sw == 0:
move = getMove(p1,'b')
assert(move != 'pass')
if move == 'resign':
result = 2
break
elif move == 'timeout':
result = 2
break
playMove(p2, 'b', move)
playMove(ob, 'b', move)
else:
move = getMove(p2, 'w')
assert(move != 'pass')
if move == 'resign':
result = 1
break
elif move == 'timeout':
result = 1
break
playMove(p1, 'w', move)
playMove(ob, 'w', move)
ob.expect('= ')
ob.sendline('showboard')
ob.expect("=.+(\s?\[.+\]\n?)+")
print(ob.after.decode('utf-8'))
sw = 1-sw
print(move)
ob.sendline('gogui-rules_final_result')
ob.expect(['= black','= white','= draw','= unknown'])
status = ob.after.decode("utf-8")[2:]
if status == 'black':
result = 1
break
elif status == 'white':
result = 2
break
elif status == 'draw':
result = 0
break
else:
assert(status == 'unknown')
return result,numTimeout
def playGames(numGame=1):
global win1,win2,draw,numTimeout
print("player1:", player1)
print("player2:", player2)
for i in range(0, numGame):
print("Game: ", i+1)
if i < numGame / 2:
alter = False
else:
alter = True
result, timeout = playSingleGame(alternative=alter)
if timeout > 0:
numTimeout += 1
else:
if result == 0:
print("draw")
draw += 1
else:
if result == 1 and alter == False or result == 2 and alter == True:
print("player1 wins")
win1 += 1
else:
assert(result == 1 and alter == True or result == 2 and alter == False)
win2+=1
print("player2 wins")
def outputResult():
print('player1 win',win1,'player2 win',win2,'draw',draw)
def saveResult():
f = open("game_results.txt", "w")
f.write("player 1: {}\n".format(player1))
f.write("player 2: {}\n".format(player2))
f.write("player 1 wins {}\n".format(win1))
f.write("player 2 wins {}\n".format(win2))
f.write("draw {}\n".format(draw))
f.close()
playGames()
outputResult()
saveResult()