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 usage(char *name
)
77 fprintf(stderr
, "Pachi version %s\n", PACHI_VERSION
);
78 fprintf(stderr
, "Usage: %s [-e random|replay|montecarlo|uct|distributed|dcnn]\n"
79 " [-d DEBUG_LEVEL] [-D] [-r RULESET] [-s RANDOM_SEED] [-t TIME_SETTINGS] [-u TEST_FILENAME]\n"
80 " [-g [HOST:]GTP_PORT] [-l [HOST:]LOG_PORT] [-f FBOOKFILE] [ENGINE_ARGS]\n", name
);
83 int main(int argc
, char *argv
[])
85 enum engine_id engine
= E_UCT
;
86 struct time_info ti_default
= { .period
= TT_NULL
};
87 char *testfile
= NULL
;
88 char *gtp_port
= NULL
;
89 char *log_port
= NULL
;
91 char *chatfile
= NULL
;
92 char *fbookfile
= NULL
;
95 seed
= time(NULL
) ^ getpid();
98 while ((opt
= getopt(argc
, argv
, "c:e:d:Df:g:l:r:s:t:u:")) != -1) {
101 chatfile
= strdup(optarg
);
104 if (!strcasecmp(optarg
, "random")) {
106 } else if (!strcasecmp(optarg
, "replay")) {
108 } else if (!strcasecmp(optarg
, "montecarlo")) {
109 engine
= E_MONTECARLO
;
110 } else if (!strcasecmp(optarg
, "uct")) {
112 } else if (!strcasecmp(optarg
, "distributed")) {
113 engine
= E_DISTRIBUTED
;
114 } else if (!strcasecmp(optarg
, "patternscan")) {
115 engine
= E_PATTERNSCAN
;
116 } else if (!strcasecmp(optarg
, "patternplay")) {
117 engine
= E_PATTERNPLAY
;
118 } else if (!strcasecmp(optarg
, "joseki")) {
121 } else if (!strcasecmp(optarg
, "dcnn")) {
125 fprintf(stderr
, "%s: Invalid -e argument %s\n", argv
[0], optarg
);
130 debug_level
= atoi(optarg
);
133 debug_boardprint
= false;
136 fbookfile
= strdup(optarg
);
139 gtp_port
= strdup(optarg
);
142 log_port
= strdup(optarg
);
145 ruleset
= strdup(optarg
);
151 /* Time settings to follow; if specified,
152 * GTP time information is ignored. Useful
153 * e.g. when you want to force your bot to
154 * play weaker while giving the opponent
155 * reasonable time to play, or force play
156 * by number of simulations in timed games. */
157 /* Please see timeinfo.h:time_parse()
158 * description for syntax details. */
159 if (!time_parse(&ti_default
, optarg
)) {
160 fprintf(stderr
, "%s: Invalid -t argument %s\n", argv
[0], optarg
);
163 ti_default
.ignore_gtp
= true;
164 assert(ti_default
.period
!= TT_NULL
);
167 testfile
= strdup(optarg
);
175 dcnn_quiet_caffe(argc
, argv
);
177 open_log_port(log_port
);
181 fprintf(stderr
, "Random seed: %d\n", seed
);
183 struct board
*b
= board_init(fbookfile
);
185 if (!board_set_rules(b
, ruleset
)) {
186 fprintf(stderr
, "Unknown ruleset: %s\n", ruleset
);
191 struct time_info ti
[S_MAX
];
192 ti
[S_BLACK
] = ti_default
;
193 ti
[S_WHITE
] = ti_default
;
199 e_arg
= argv
[optind
];
200 struct engine
*e
= init_engine(engine
, e_arg
, b
);
208 open_gtp_connection(>p_sock
, gtp_port
);
213 while (fgets(buf
, 4096, stdin
)) {
215 fprintf(stderr
, "IN: %s", buf
);
217 enum parse_code c
= gtp_parse(b
, e
, ti
, buf
);
218 if (c
== P_ENGINE_RESET
) {
219 ti
[S_BLACK
] = ti_default
;
220 ti
[S_WHITE
] = ti_default
;
221 if (!e
->keep_on_clear
) {
224 e
= init_engine(engine
, e_arg
, b
);
226 } else if (c
== P_UNKNOWN_COMMAND
&& gtp_port
) {
227 /* The gtp command is a weak identity check,
228 * close the connection with a wrong peer. */
232 if (!gtp_port
) break;
233 open_gtp_connection(>p_sock
, gtp_port
);