-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgtp_engine.py
255 lines (223 loc) · 9.69 KB
/
gtp_engine.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import sys
from board import Board
from lookup_players import MCPlayerQ
from utils import p2cd, eprint, Color, c_cd2cp, color2c
from param_players import CNNPlayer
class GtpEngine:
def __init__(self, N,Player_class, verbose=False):
self.Player_class=Player_class
self.player = self.Player_class(N)
self.verbose = verbose
self.last_move_pass = False #Used to finish the game when the other player passes
# commented commands are not implemented yet
self.administrative_commands = ['protocol_version', 'name', 'version', 'known_command', 'list_commands', 'quit']
self.setup_commands = ['boardsize', 'clear_board', 'komi',
# 'fixed_handicap', place_free_handicap, set_free_handicap
]
self.core_play_commands = ['play', 'genmove',
'undo',
]
self.tournament_commands = [ # 'time_settings', 'time_left',
#'final_score',
# 'final_status_list'
]
self.reggression_commands = [ # 'load_sgf', 'reg_genmove'
]
self.debug_commands = ['showboard']
self.known_commands = self.administrative_commands + self.setup_commands + self.core_play_commands \
+ self.tournament_commands + self.reggression_commands + self.debug_commands
def gtp_session(self):
quit = False
while not quit:
# read and respond
# read command of the form:
# [id] command_name [arguments]\n
line = raw_input().strip()
if line == '':
continue
if line[0] == '#':
continue
line_list = line.split()
# print(line)
# print(line_list)
try:
int(line_list[0])
id = line_list[0]
cmd = line_list[1]
args = line_list[2:]
except ValueError:
id = ''
cmd = line_list[0]
args = line_list[1:]
if self.verbose:
eprint('line:{} command:{} args:{}'.format(line, cmd, args))
# process command and return response of the form:
# =[id] result
# ?[id] error_message
success = True
result = ''
if cmd == 'protocol_version':
result = '2'
elif cmd == 'name':
result = 'ndsgo'
elif cmd == 'version':
result = ''
elif cmd == 'known_command':
if len(args) < 1:
result = 'syntax error'
success = False
else:
command = args[0]
if command in self.known_commands:
result = 'true'
else:
result = 'false'
elif cmd == 'list_commands':
result = ''
for e in self.known_commands:
result += e + "\n"
result = result[:-1] # remove the last \n
elif cmd == 'quit':
result = ''
quit = True
elif cmd == 'boardsize':
# TODO: test this code
if len(args) < 1:
result = 'syntax error'
success = False
else:
try:
size = int(args[0])
if self.player.player_file.has_key(size):
if self.Player_class is CNNPlayer:
#success = False
#result = 'unacceptable size'
#eprint('Cannot change size because this is not implmeneted for CNNPlayer. '
# 'It would cause an error')
success=True
elif self.Player_class is MCPlayerQ:
self.player = self.Player_class(size,epsilon=0.0)
self.player.load_Q(self.player.player_file[size])
else:
success = False
result = 'unacceptable size'
except Exception as err:
if self.verbose:
eprint('Exception in gtp_engine.boardsize. Error:{}'.format(err))
success = False
result = 'syntax error'
elif cmd == 'clear_board':
# board.clear_board() function is called inside player.new_game()
# we call new_game() since if we are learning we need to reset the episode_history for the game
self.player.new_game()
elif cmd == 'komi':
if len(args) < 1:
result = 'syntax error'
success = False
else:
try:
new_komi = float(args[0])
self.player.board.komi = new_komi
except Exception as err:
if self.verbose:
eprint('Exception in gtp_engine.komi. Error:{}'.format(err))
success = False
result = 'syntax error'
elif cmd == 'play':
# e.g. play black A1
if len(args) < 2:
result = 'syntax error'
success = False
else:
eprint('IN PLAY: {} {}'.format(args[0], args[1]))
if args[1] == 'PASS':
eprint('in play. Move is PASS.')
self.last_move_pass=True
success = True
else:
try:
(c, p) = c_cd2cp(args[0] + ' ' + args[1], self.player.board.N)
res = self.player.board.play(c, p)
if self.verbose:
eprint("args:{} c:{} p:{} res:{}".format(args, c, p, res))
if res < 0:
if self.verbose:
eprint("Illegal move in gtp_engine. play. res:{}".format(res))
success = False
result = 'illegal move'
else:
result = ''
except Exception as err:
if self.verbose:
eprint('Exception in gtp_engine.play(). Error:{}'.format(err))
success = False
result = 'syntax error'
elif cmd == 'genmove':
if len(args) < 1:
result = 'syntax error'
success = False
else:
#FIRST VERIFY IF THE OTHER PLAYER HAS NOT PASSED
if self.last_move_pass==True:
eprint('Since the other program passed we also pass.')
eprint('This is trusting the other program knows better when the game has finished.')
self.last_move_pass=False
result='pass'
else:
color = args[0].lower()
if color == 'black':
color = 'b'
if color == 'white':
color = 'w'
if color != 'b' and color != 'w':
success = False
result = 'syntax error'
else:
c = color2c(color)
mov = self.player.genmove(c)
if mov is None:
result = 'pass'
else:
result = p2cd(mov, self.player.board.N)
elif cmd == 'undo':
if len(self.player.board.move_history) == 0:
success = False
result = 'cannot undo'
else:
self.player.board.undo()
# elif cmd == 'final_score':
# score = self.player.board.final_score()
# if score > 0:
# result = "B+{}".format(score)
# elif score < 0:
# result = "W+{}".format(abs(score))
# else:
# result = '0'
elif cmd == 'showboard':
result = self.player.board.__str__()
else:
result = 'unknown command'
success = False
# Return response of the form
# =[id] result (SUCCESS)
# ?[id] error_message (FAILURE)
print('{}{} {}\n'.format('=' if success else '?', id, result))
sys.stdout.flush()
def main(boardsize):
#player = MCPlayerQ(3)
#player.load_Q(player_file[3])
#player=CNNPlayer(9)
if boardsize==2 or boardsize==3:
gtp = GtpEngine(boardsize, MCPlayerQ, verbose=True)
elif boardsize==9 or boardsize==19:
gtp = GtpEngine(boardsize, CNNPlayer, verbose=True)
#gtp = GtpEngine(9, CNNPlayer, verbose=True)
#gtp = GtpEngine(3, MCPlayerQ, verbose=True)
gtp.gtp_session()
if __name__ == '__main__':
if len(sys.argv)== 2:
boardsize=int(sys.argv[1])
else:
boardsize=9
eprint('In gtp_engine.py. boardsize:{}'.format(boardsize))
main(boardsize)