forked from pnprog/goreviewpartner
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgtp.py
223 lines (187 loc) · 5.42 KB
/
gtp.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
import subprocess
import threading, queue
from time import sleep,time
from toolbox import log
class GtpException(Exception):
pass
class gtp():
def __init__(self,command):
self.c=1
print(command)
self.process=subprocess.Popen(command, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
self.size=0
self.command_line=command[0]+" "+" ".join(command[1:])
self.stderr_queue=queue.Queue()
threading.Thread(target=self.consume_stderr).start()
####low level function####
def consume_stderr(self):
while 1:
try:
err_line=self.process.stderr.readline()
if err_line:
self.stderr_queue.put(err_line)
else:
log("leaving consume_stderr thread")
return
except Exception as e:
import sys, os
exc_type, exc_obj, exc_tb = sys.exc_info()
fname = os.path.split(exc_tb.tb_frame.f_code.co_filename)[1]
log(exc_type, fname, exc_tb.tb_lineno)
log("leaving consume_stderr thread due to exception")
return
def quick_evaluation(self,color):
return "Feature not implemented"
def write(self,txt):
try:
print('txt:',txt)
self.process.stdin.write(txt.encode()+b'\r\n')
self.process.stdin.flush()
except Exception as e:
log("Error while writting to stdin\n"+str(e))
#self.process.stdin.write(str(self.c)+" "+txt+"\n")
self.c+=1
def readline(self):
answer=self.process.stdout.readline().decode('utf8')
print('answer:',answer)
while answer in ("\n","\r\n","\r"):
answer=self.process.stdout.readline().decode('utf8')
return answer
####hight level function####
def boardsize(self,size=19):
self.size=size
self.write("boardsize "+str(size))
answer=self.readline()
if answer[0]=="=":return True
else:return False
def reset(self):
self.write("clear_board")
answer=self.readline()
if answer[0]=="=":return True
else:return False
def komi(self,k):
self.write("komi "+str(k))
answer=self.readline()
if answer[0]=="=":return True
else:return False
def place_black(self,move):
self.write("play black "+move)
answer=self.readline()
if answer[0]=="=":return True
else:return False
def place(self,move,color):
if color==1:
return self.place_black(move)
else:
return self.place_white(move)
def place_white(self,move):
self.write("play white "+move)
answer=self.readline()
if answer[0]=="=":return True
else:return False
def name(self):
self.write("name")
answer=self.readline().strip()
try:
return " ".join(answer.split(" ")[1:])
except Exception as e:
raise GtpException("GtpException in name()\nanswer='"+answer+"'\n"+str(e))
def version(self):
self.write("version")
answer=self.readline().strip()
try:
return answer.split(" ")[1]
except Exception as e:
raise GtpException("GtpException in version()\nanswer='"+answer+"'\n"+str(e))
def play_black(self):
self.write("genmove black")
answer=self.readline().strip()
try:
return answer.split(" ")[1]
except Exception as e:
raise GtpException("GtpException in genmove_black()\nanswer='"+answer+"'\n"+str(e))
def play_white(self):
self.write("genmove white")
answer=self.readline().strip()
try:
return answer.split(" ")[1]
except Exception as e:
raise GtpException("GtpException in genmove_white()\nanswer='"+answer+"'\n"+str(e))
def set_free_handicap(self,positions):
stones=""
for p in positions:
stones+=p+" "
log("Setting handicap stones at",stones.strip())
self.write("set_free_handicap "+stones.strip())
answer=self.readline().strip()
try:
if answer[0]=="=":
return True
else:
return False
except Exception as e:
raise GtpException("GtpException in set_free_handicap()\nanswer='"+answer+"'\n"+str(e))
def undo_resign(self):
#just do nothing
pass
def undo(self):
self.write("undo")
answer=self.readline()
try:
if answer[0]=="=":
return True
else:
return False
except Exception as e:
raise GtpException("GtpException in undo()\nanswer='"+answer+"'\n"+str(e))
def countlib(self,move):
self.write("countlib "+move)
answer=self.readline()
return " ".join(answer.split(" ")[1:])
#is that needed?
def final_score(self):
self.write("final_score")
answer=self.readline()
return " ".join(answer.split(" ")[1:])
#is that needed?
def final_status(self,move):
self.write("final_status "+move)
answer=self.readline()
answer=answer.strip()
return " ".join(answer.split(" ")[1:])
def set_time(self,main_time=30,byo_yomi_time=30,byo_yomi_stones=1):
self.write("time_settings "+str(main_time)+" "+str(byo_yomi_time)+" "+str(byo_yomi_stones))
answer=self.readline()
try:
if answer[0]=="=":return True
else:return False
except Exception as e:
raise GtpException("GtpException in set_time()\nanswer='"+answer+"'\n"+str(e))
def quit(self):
self.write("quit")
answer=self.readline()
if answer[0]=="=":return True
else:return False
def terminate(self):
t=10
while 1:
self.quitting_thread.join(0.0)
if not self.quitting_thread.is_alive():
log("The bot has quitted properly")
break
elif t==0:
log("The bot is still running...")
log("Forcefully closing it now!")
break
t-=1
log("Waiting for the bot to close",t,"s")
sleep(1)
try: self.process.kill()
except: pass
try: self.process.stdin.close()
except: pass
def close(self):
log("Now closing")
self.quitting_thread=threading.Thread(target=self.quit)
self.quitting_thread.start()
threading.Thread(target=self.terminate).start()