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 long verbose_logs
= 0;
43 static struct engine
*(*engine_init
[E_MAX
])(char *arg
, struct board
*b
) = {
46 engine_patternscan_init
,
47 engine_montecarlo_init
,
49 engine_distributed_init
,
53 static struct engine
*init_engine(enum engine_id engine
, char *e_arg
, struct board
*b
)
55 char *arg
= e_arg
? strdup(e_arg
) : e_arg
;
56 assert(engine
< E_MAX
);
57 struct engine
*e
= engine_init
[engine
](arg
, b
);
62 static void done_engine(struct engine
*e
)
64 if (e
->done
) e
->done(e
);
65 if (e
->data
) free(e
->data
);
69 static void usage(char *name
)
71 fprintf(stderr
, "Pachi version %s\n", PACHI_VERSION
);
72 fprintf(stderr
, "Usage: %s [-e random|replay|patternscan|montecarlo|uct|distributed]\n"
73 " [-d DEBUG_LEVEL] [-s RANDOM_SEED] [-t TIME_SETTINGS] [-u TEST_FILENAME]"
74 " [-g [HOST:]GTP_PORT] [-l [HOST:]LOG_PORT] [ENGINE_ARGS]\n", name
);
77 int main(int argc
, char *argv
[])
79 enum engine_id engine
= E_UCT
;
80 struct time_info ti_default
= { .period
= TT_NULL
};
81 char *testfile
= NULL
;
82 char *gtp_port
= NULL
;
83 char *log_port
= NULL
;
86 seed
= time(NULL
) ^ getpid();
89 while ((opt
= getopt(argc
, argv
, "e:d:g:l:s:t:u:")) != -1) {
92 if (!strcasecmp(optarg
, "random")) {
94 } else if (!strcasecmp(optarg
, "patternscan")) {
95 engine
= E_PATTERNSCAN
;
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
, "joseki")) {
107 fprintf(stderr
, "%s: Invalid -e argument %s\n", argv
[0], optarg
);
112 debug_level
= atoi(optarg
);
115 gtp_port
= strdup(optarg
);
118 log_port
= strdup(optarg
);
124 /* Time settings to follow; if specified,
125 * GTP time information is ignored. Useful
126 * e.g. when you want to force your bot to
127 * play weaker while giving the opponent
128 * reasonable time to play, or force play
129 * by number of simulations in timed games. */
130 /* Please see timeinfo.h:time_parse()
131 * description for syntax details. */
132 if (!time_parse(&ti_default
, optarg
)) {
133 fprintf(stderr
, "%s: Invalid -t argument %s\n", argv
[0], optarg
);
136 ti_default
.ignore_gtp
= true;
137 assert(ti_default
.period
!= TT_NULL
);
140 testfile
= strdup(optarg
);
149 open_log_port(log_port
);
153 fprintf(stderr
, "Random seed: %d\n", seed
);
155 struct board
*b
= board_init();
156 struct time_info ti
[S_MAX
];
157 ti
[S_BLACK
] = ti_default
;
158 ti
[S_WHITE
] = ti_default
;
162 e_arg
= argv
[optind
];
163 struct engine
*e
= init_engine(engine
, e_arg
, b
);
171 open_gtp_connection(>p_sock
, gtp_port
);
176 while (fgets(buf
, 4096, stdin
)) {
178 fprintf(stderr
, "IN: %s", buf
);
180 enum parse_code c
= gtp_parse(b
, e
, ti
, buf
);
181 if (c
== P_ENGINE_RESET
) {
182 ti
[S_BLACK
] = ti_default
;
183 ti
[S_WHITE
] = ti_default
;
184 if (!e
->keep_on_clear
) {
187 e
= init_engine(engine
, e_arg
, b
);
189 } else if (c
== P_UNKNOWN_COMMAND
&& gtp_port
) {
190 /* The gtp command is a weak identity check,
191 * close the connection with a wrong peer. */
195 if (!gtp_port
) break;
196 open_gtp_connection(>p_sock
, gtp_port
);