13 #include "replay/replay.h"
14 #include "montecarlo/montecarlo.h"
15 #include "random/random.h"
16 #include "patternscan/patternscan.h"
17 #include "joseki/joseki.h"
18 #include "t-unit/test.h"
20 #include "distributed/distributed.h"
28 bool debug_boardprint
= true;
29 long verbose_logs
= 0;
44 static struct engine
*(*engine_init
[E_MAX
])(char *arg
, struct board
*b
) = {
47 engine_patternscan_init
,
48 engine_montecarlo_init
,
50 engine_distributed_init
,
54 static struct engine
*init_engine(enum engine_id engine
, char *e_arg
, struct board
*b
)
56 char *arg
= e_arg
? strdup(e_arg
) : e_arg
;
57 assert(engine
< E_MAX
);
58 struct engine
*e
= engine_init
[engine
](arg
, b
);
63 static void done_engine(struct engine
*e
)
65 if (e
->done
) e
->done(e
);
66 if (e
->data
) free(e
->data
);
70 static void usage(char *name
)
72 fprintf(stderr
, "Pachi version %s\n", PACHI_VERSION
);
73 fprintf(stderr
, "Usage: %s [-e random|replay|montecarlo|uct|distributed]\n"
74 " [-d DEBUG_LEVEL] [-D] [-s RANDOM_SEED] [-t TIME_SETTINGS] [-u TEST_FILENAME]\n"
75 " [-g [HOST:]GTP_PORT] [-l [HOST:]LOG_PORT] [-f FBOOKFILE] [ENGINE_ARGS]\n", name
);
78 int main(int argc
, char *argv
[])
80 enum engine_id engine
= E_UCT
;
81 struct time_info ti_default
= { .period
= TT_NULL
};
82 char *testfile
= NULL
;
83 char *gtp_port
= NULL
;
84 char *log_port
= NULL
;
86 char *fbookfile
= NULL
;
88 seed
= time(NULL
) ^ getpid();
91 while ((opt
= getopt(argc
, argv
, "e:d:Df:g:l:s:t:u:")) != -1) {
94 if (!strcasecmp(optarg
, "random")) {
96 } else if (!strcasecmp(optarg
, "replay")) {
98 } else if (!strcasecmp(optarg
, "montecarlo")) {
99 engine
= E_MONTECARLO
;
100 } else if (!strcasecmp(optarg
, "uct")) {
102 } else if (!strcasecmp(optarg
, "distributed")) {
103 engine
= E_DISTRIBUTED
;
104 } else if (!strcasecmp(optarg
, "patternscan")) {
105 engine
= E_PATTERNSCAN
;
106 } else if (!strcasecmp(optarg
, "joseki")) {
109 fprintf(stderr
, "%s: Invalid -e argument %s\n", argv
[0], optarg
);
114 debug_level
= atoi(optarg
);
117 debug_boardprint
= false;
120 fbookfile
= strdup(optarg
);
123 gtp_port
= strdup(optarg
);
126 log_port
= strdup(optarg
);
132 /* Time settings to follow; if specified,
133 * GTP time information is ignored. Useful
134 * e.g. when you want to force your bot to
135 * play weaker while giving the opponent
136 * reasonable time to play, or force play
137 * by number of simulations in timed games. */
138 /* Please see timeinfo.h:time_parse()
139 * description for syntax details. */
140 if (!time_parse(&ti_default
, optarg
)) {
141 fprintf(stderr
, "%s: Invalid -t argument %s\n", argv
[0], optarg
);
144 ti_default
.ignore_gtp
= true;
145 assert(ti_default
.period
!= TT_NULL
);
148 testfile
= strdup(optarg
);
157 open_log_port(log_port
);
161 fprintf(stderr
, "Random seed: %d\n", seed
);
163 struct board
*b
= board_init(fbookfile
);
164 struct time_info ti
[S_MAX
];
165 ti
[S_BLACK
] = ti_default
;
166 ti
[S_WHITE
] = ti_default
;
170 e_arg
= argv
[optind
];
171 struct engine
*e
= init_engine(engine
, e_arg
, b
);
179 open_gtp_connection(>p_sock
, gtp_port
);
184 while (fgets(buf
, 4096, stdin
)) {
186 fprintf(stderr
, "IN: %s", buf
);
188 enum parse_code c
= gtp_parse(b
, e
, ti
, buf
);
189 if (c
== P_ENGINE_RESET
) {
190 ti
[S_BLACK
] = ti_default
;
191 ti
[S_WHITE
] = ti_default
;
192 if (!e
->keep_on_clear
) {
195 e
= init_engine(engine
, e_arg
, b
);
197 } else if (c
== P_UNKNOWN_COMMAND
&& gtp_port
) {
198 /* The gtp command is a weak identity check,
199 * close the connection with a wrong peer. */
203 if (!gtp_port
) break;
204 open_gtp_connection(>p_sock
, gtp_port
);