13 #include "replay/replay.h"
14 #include "montecarlo/montecarlo.h"
15 #include "random/random.h"
16 #include "patternscan/patternscan.h"
17 #include "t-unit/test.h"
19 #include "distributed/distributed.h"
40 static struct engine
*(*engine_init
[E_MAX
])(char *arg
, struct board
*b
) = {
43 engine_patternscan_init
,
44 engine_montecarlo_init
,
46 engine_distributed_init
,
49 static struct engine
*init_engine(enum engine_id engine
, char *e_arg
, struct board
*b
)
51 char *arg
= e_arg
? strdup(e_arg
) : e_arg
;
52 assert(engine
< E_MAX
);
53 struct engine
*e
= engine_init
[engine
](arg
, b
);
58 static void done_engine(struct engine
*e
)
60 if (e
->done
) e
->done(e
);
61 if (e
->data
) free(e
->data
);
65 static void usage(char *name
)
67 fprintf(stderr
, "Pachi version %s\n", PACHI_VERSION
);
68 fprintf(stderr
, "Usage: %s [-e random|replay|patternscan|montecarlo|uct|distributed]\n"
69 " [-d DEBUG_LEVEL] [-s RANDOM_SEED] [-t TIME_SETTINGS] [-u TEST_FILENAME]"
70 " [-g [HOST:]GTP_PORT] [-l [HOST:]LOG_PORT] [ENGINE_ARGS]\n", name
);
73 int main(int argc
, char *argv
[])
75 enum engine_id engine
= E_UCT
;
76 struct time_info ti_default
= { .period
= TT_NULL
};
77 char *testfile
= NULL
;
78 char *gtp_port
= NULL
;
79 char *log_port
= NULL
;
82 seed
= time(NULL
) ^ getpid();
85 while ((opt
= getopt(argc
, argv
, "e:d:g:l:s:t:u:")) != -1) {
88 if (!strcasecmp(optarg
, "random")) {
90 } else if (!strcasecmp(optarg
, "patternscan")) {
91 engine
= E_PATTERNSCAN
;
92 } else if (!strcasecmp(optarg
, "replay")) {
94 } else if (!strcasecmp(optarg
, "montecarlo")) {
95 engine
= E_MONTECARLO
;
96 } else if (!strcasecmp(optarg
, "uct")) {
98 } else if (!strcasecmp(optarg
, "distributed")) {
99 engine
= E_DISTRIBUTED
;
101 fprintf(stderr
, "%s: Invalid -e argument %s\n", argv
[0], optarg
);
106 debug_level
= atoi(optarg
);
109 gtp_port
= strdup(optarg
);
112 log_port
= strdup(optarg
);
118 /* Time settings to follow; if specified,
119 * GTP time information is ignored. Useful
120 * e.g. when you want to force your bot to
121 * play weaker while giving the opponent
122 * reasonable time to play, or force play
123 * by number of simulations in timed games. */
124 /* Please see timeinfo.h:time_parse()
125 * description for syntax details. */
126 if (!time_parse(&ti_default
, optarg
)) {
127 fprintf(stderr
, "%s: Invalid -t argument %s\n", argv
[0], optarg
);
130 ti_default
.ignore_gtp
= true;
131 assert(ti_default
.period
!= TT_NULL
);
134 testfile
= strdup(optarg
);
143 open_log_port(log_port
);
147 fprintf(stderr
, "Random seed: %d\n", seed
);
149 struct board
*b
= board_init();
150 struct time_info ti
[S_MAX
];
151 ti
[S_BLACK
] = ti_default
;
152 ti
[S_WHITE
] = ti_default
;
156 e_arg
= argv
[optind
];
157 struct engine
*e
= init_engine(engine
, e_arg
, b
);
165 open_gtp_connection(>p_sock
, gtp_port
);
170 while (fgets(buf
, 4096, stdin
)) {
172 fprintf(stderr
, "IN: %s", buf
);
174 enum parse_code c
= gtp_parse(b
, e
, ti
, buf
);
175 if (c
== P_ENGINE_RESET
&& !e
->keep_on_clear
) {
178 e
= init_engine(engine
, e_arg
, b
);
179 ti
[S_BLACK
] = ti_default
;
180 ti
[S_WHITE
] = ti_default
;
181 } else if (c
== P_UNKNOWN_COMMAND
&& gtp_port
) {
182 /* The gtp command is a weak identity check,
183 * close the connection with a wrong peer. */
187 if (!gtp_port
) break;
188 open_gtp_connection(>p_sock
, gtp_port
);