13 #include "replay/replay.h"
14 #include "montecarlo/montecarlo.h"
15 #include "random/random.h"
16 #include "joseki/joseki.h"
17 #include "t-unit/test.h"
19 #include "distributed/distributed.h"
27 bool debug_boardprint
= true;
28 long verbose_logs
= 0;
42 static struct engine
*(*engine_init
[E_MAX
])(char *arg
, struct board
*b
) = {
45 engine_montecarlo_init
,
47 engine_distributed_init
,
51 static struct engine
*init_engine(enum engine_id engine
, char *e_arg
, struct board
*b
)
53 char *arg
= e_arg
? strdup(e_arg
) : e_arg
;
54 assert(engine
< E_MAX
);
55 struct engine
*e
= engine_init
[engine
](arg
, b
);
60 static void done_engine(struct engine
*e
)
62 if (e
->done
) e
->done(e
);
63 if (e
->data
) free(e
->data
);
67 static void usage(char *name
)
69 fprintf(stderr
, "Pachi version %s\n", PACHI_VERSION
);
70 fprintf(stderr
, "Usage: %s [-e random|replay|montecarlo|uct|distributed]\n"
71 " [-d DEBUG_LEVEL] [-D] [-s RANDOM_SEED] [-t TIME_SETTINGS] [-u TEST_FILENAME]\n"
72 " [-g [HOST:]GTP_PORT] [-l [HOST:]LOG_PORT] [-f FBOOKFILE] [ENGINE_ARGS]\n", name
);
75 int main(int argc
, char *argv
[])
77 enum engine_id engine
= E_UCT
;
78 struct time_info ti_default
= { .period
= TT_NULL
};
79 char *testfile
= NULL
;
80 char *gtp_port
= NULL
;
81 char *log_port
= NULL
;
83 char *fbookfile
= NULL
;
85 seed
= time(NULL
) ^ getpid();
88 while ((opt
= getopt(argc
, argv
, "e:d:Df:g:l:s:t:u:")) != -1) {
91 if (!strcasecmp(optarg
, "random")) {
93 } else if (!strcasecmp(optarg
, "replay")) {
95 } else if (!strcasecmp(optarg
, "montecarlo")) {
96 engine
= E_MONTECARLO
;
97 } else if (!strcasecmp(optarg
, "uct")) {
99 } else if (!strcasecmp(optarg
, "distributed")) {
100 engine
= E_DISTRIBUTED
;
101 } else if (!strcasecmp(optarg
, "joseki")) {
104 fprintf(stderr
, "%s: Invalid -e argument %s\n", argv
[0], optarg
);
109 debug_level
= atoi(optarg
);
112 debug_boardprint
= false;
115 fbookfile
= strdup(optarg
);
118 gtp_port
= strdup(optarg
);
121 log_port
= strdup(optarg
);
127 /* Time settings to follow; if specified,
128 * GTP time information is ignored. Useful
129 * e.g. when you want to force your bot to
130 * play weaker while giving the opponent
131 * reasonable time to play, or force play
132 * by number of simulations in timed games. */
133 /* Please see timeinfo.h:time_parse()
134 * description for syntax details. */
135 if (!time_parse(&ti_default
, optarg
)) {
136 fprintf(stderr
, "%s: Invalid -t argument %s\n", argv
[0], optarg
);
139 ti_default
.ignore_gtp
= true;
140 assert(ti_default
.period
!= TT_NULL
);
143 testfile
= strdup(optarg
);
152 open_log_port(log_port
);
156 fprintf(stderr
, "Random seed: %d\n", seed
);
158 struct board
*b
= board_init(fbookfile
);
159 struct time_info ti
[S_MAX
];
160 ti
[S_BLACK
] = ti_default
;
161 ti
[S_WHITE
] = ti_default
;
165 e_arg
= argv
[optind
];
166 struct engine
*e
= init_engine(engine
, e_arg
, b
);
174 open_gtp_connection(>p_sock
, gtp_port
);
179 while (fgets(buf
, 4096, stdin
)) {
181 fprintf(stderr
, "IN: %s", buf
);
183 enum parse_code c
= gtp_parse(b
, e
, ti
, buf
);
184 if (c
== P_ENGINE_RESET
) {
185 ti
[S_BLACK
] = ti_default
;
186 ti
[S_WHITE
] = ti_default
;
187 if (!e
->keep_on_clear
) {
190 e
= init_engine(engine
, e_arg
, b
);
192 } else if (c
== P_UNKNOWN_COMMAND
&& gtp_port
) {
193 /* The gtp command is a weak identity check,
194 * close the connection with a wrong peer. */
198 if (!gtp_port
) break;
199 open_gtp_connection(>p_sock
, gtp_port
);