forked from ymgaq/Pyaq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gtp.py
executable file
·109 lines (94 loc) · 3 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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
from sys import stderr, stdout, stdin
from board import *
import numpy as np
from search import Tree
cmd_list = [
"protocol_version", "name", "version", "list_commands",
"boardsize", "komi", "time_settings", "time_left",
"clear_board", "genmove", "play", "undo",
"gogui-play_sequence", "showboard", "quit"
]
def include(str, cmd):
return str.find(cmd) >= 0
def send(res_cmd):
stdout.write("= " + res_cmd + "\n\n")
stdout.flush()
def args(str):
arg_list = str.split()
if arg_list[0] == "=":
arg_list.pop(0)
arg_list.pop(0)
return arg_list
def call_gtp(main_time, byoyomi, quick=False, clean=False, use_gpu=True):
b = Board()
tree = Tree(use_gpu=use_gpu)
tree.main_time = main_time
tree.byoyomi = byoyomi
while 1:
str = stdin.readline().rstrip("\r\n")
if str == "":
continue
elif include(str, "protocol_version"):
send("2")
elif include(str, "name"):
send("Pyaq")
elif include(str, "version"):
send("1.0")
elif include(str, "list_commands"):
stdout.write("=")
for cmd in cmd_list:
stdout.write(cmd + "\n")
send("")
elif include(str, "boardsize"):
bs = int(args(str)[0])
if bs != BSIZE:
stdout.write("?invalid boardsize\n\n")
send("")
elif include(str, "komi"):
send("")
elif include(str, "time_settings"):
arg_list = args(str)
tree.main_time = arg_list[0]
tree.left_time = tree.main_time
tree.byoyomi = arg_list[1]
elif include(str, "time_left"):
tree.left_time = float(args(str)[1])
elif include(str, "clear_board"):
b.clear()
tree.clear()
send("")
elif include(str, "genmove"):
if quick:
move = rv2ev(np.argmax(tree.evaluate(b)[0][0]))
else:
move, win_rate = tree.search(b, 0, ponder=False, clean=clean)
if win_rate < 0.1:
send("resign")
else:
b.play(move)
send(ev2str(move))
elif include(str, "play"):
b.play(str2ev(args(str)[1]), not_fill_eye=False)
send("")
elif include(str, "undo"):
history = b.history[:-1]
b.clear()
tree.clear()
for v in history:
b.play(v, not_fill_eye=False)
send("")
elif include(str, "gogui-play_sequence"):
arg_list = args(str)
for i in range(1, len(arg_list) + 1, 2):
b.play(str2ev(arg_list[i]), not_fill_eye=False)
send("")
elif include(str, "showboard"):
b.showboard()
send("")
elif include(str, "quit"):
send("")
break
else:
stdout.write("?unknown_command\n\n")