13 #include "replay/replay.h"
14 #include "montecarlo/montecarlo.h"
15 #include "random/random.h"
16 #include "patternscan/patternscan.h"
17 #include "patternplay/patternplay.h"
18 #include "joseki/joseki.h"
19 #include "t-unit/test.h"
21 #include "distributed/distributed.h"
32 bool debug_boardprint
= true;
33 long verbose_logs
= 0;
52 static struct engine
*(*engine_init
[E_MAX
])(char *arg
, struct board
*b
) = {
55 engine_patternscan_init
,
56 engine_patternplay_init
,
57 engine_montecarlo_init
,
59 engine_distributed_init
,
66 static struct engine
*init_engine(enum engine_id engine
, char *e_arg
, struct board
*b
)
68 char *arg
= e_arg
? strdup(e_arg
) : e_arg
;
69 assert(engine
< E_MAX
);
70 struct engine
*e
= engine_init
[engine
](arg
, b
);
75 static void done_engine(struct engine
*e
)
77 if (e
->done
) e
->done(e
);
78 if (e
->data
) free(e
->data
);
82 static void usage(char *name
)
84 fprintf(stderr
, "Pachi version %s\n", PACHI_VERSION
);
85 fprintf(stderr
, "Usage: %s [-e random|replay|montecarlo|uct|distributed|dcnn]\n"
86 " [-d DEBUG_LEVEL] [-D] [-r RULESET] [-s RANDOM_SEED] [-t TIME_SETTINGS] [-u TEST_FILENAME]\n"
87 " [-g [HOST:]GTP_PORT] [-l [HOST:]LOG_PORT] [-f FBOOKFILE] [ENGINE_ARGS]\n", name
);
90 int main(int argc
, char *argv
[])
92 enum engine_id engine
= E_UCT
;
93 struct time_info ti_default
= { .period
= TT_NULL
};
94 char *testfile
= NULL
;
95 char *gtp_port
= NULL
;
96 char *log_port
= NULL
;
98 char *chatfile
= NULL
;
99 char *fbookfile
= NULL
;
100 char *ruleset
= NULL
;
102 seed
= time(NULL
) ^ getpid();
105 while ((opt
= getopt(argc
, argv
, "c:e:d:Df:g:l:r:s:t:u:")) != -1) {
108 chatfile
= strdup(optarg
);
111 if (!strcasecmp(optarg
, "random")) {
113 } else if (!strcasecmp(optarg
, "replay")) {
115 } else if (!strcasecmp(optarg
, "montecarlo")) {
116 engine
= E_MONTECARLO
;
117 } else if (!strcasecmp(optarg
, "uct")) {
119 } else if (!strcasecmp(optarg
, "distributed")) {
120 engine
= E_DISTRIBUTED
;
121 } else if (!strcasecmp(optarg
, "patternscan")) {
122 engine
= E_PATTERNSCAN
;
123 } else if (!strcasecmp(optarg
, "patternplay")) {
124 engine
= E_PATTERNPLAY
;
125 } else if (!strcasecmp(optarg
, "joseki")) {
128 } else if (!strcasecmp(optarg
, "dcnn")) {
132 fprintf(stderr
, "%s: Invalid -e argument %s\n", argv
[0], optarg
);
137 debug_level
= atoi(optarg
);
140 debug_boardprint
= false;
143 fbookfile
= strdup(optarg
);
146 gtp_port
= strdup(optarg
);
149 log_port
= strdup(optarg
);
152 ruleset
= strdup(optarg
);
158 /* Time settings to follow; if specified,
159 * GTP time information is ignored. Useful
160 * e.g. when you want to force your bot to
161 * play weaker while giving the opponent
162 * reasonable time to play, or force play
163 * by number of simulations in timed games. */
164 /* Please see timeinfo.h:time_parse()
165 * description for syntax details. */
166 if (!time_parse(&ti_default
, optarg
)) {
167 fprintf(stderr
, "%s: Invalid -t argument %s\n", argv
[0], optarg
);
170 ti_default
.ignore_gtp
= true;
171 assert(ti_default
.period
!= TT_NULL
);
174 testfile
= strdup(optarg
);
182 dcnn_quiet_caffe(argc
, argv
);
184 open_log_port(log_port
);
188 fprintf(stderr
, "Random seed: %d\n", seed
);
190 struct board
*b
= board_init(fbookfile
);
192 if (!board_set_rules(b
, ruleset
)) {
193 fprintf(stderr
, "Unknown ruleset: %s\n", ruleset
);
198 struct time_info ti
[S_MAX
];
199 ti
[S_BLACK
] = ti_default
;
200 ti
[S_WHITE
] = ti_default
;
206 e_arg
= argv
[optind
];
207 struct engine
*e
= init_engine(engine
, e_arg
, b
);
215 open_gtp_connection(>p_sock
, gtp_port
);
220 while (fgets(buf
, 4096, stdin
)) {
222 fprintf(stderr
, "IN: %s", buf
);
224 enum parse_code c
= gtp_parse(b
, e
, ti
, buf
);
225 if (c
== P_ENGINE_RESET
) {
226 ti
[S_BLACK
] = ti_default
;
227 ti
[S_WHITE
] = ti_default
;
228 if (!e
->keep_on_clear
) {
231 e
= init_engine(engine
, e_arg
, b
);
233 } else if (c
== P_UNKNOWN_COMMAND
&& gtp_port
) {
234 /* The gtp command is a weak identity check,
235 * close the connection with a wrong peer. */
239 if (!gtp_port
) break;
240 open_gtp_connection(>p_sock
, gtp_port
);