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]\n"
74 " [-g [HOST:]GTP_PORT] [-l [HOST:]LOG_PORT] [-f FBOOKFILE] [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
;
85 char *fbookfile
= NULL
;
87 seed
= time(NULL
) ^ getpid();
90 while ((opt
= getopt(argc
, argv
, "e:d:f:g:l:s:t:u:")) != -1) {
93 if (!strcasecmp(optarg
, "random")) {
95 } else if (!strcasecmp(optarg
, "patternscan")) {
96 engine
= E_PATTERNSCAN
;
97 } else if (!strcasecmp(optarg
, "replay")) {
99 } else if (!strcasecmp(optarg
, "montecarlo")) {
100 engine
= E_MONTECARLO
;
101 } else if (!strcasecmp(optarg
, "uct")) {
103 } else if (!strcasecmp(optarg
, "distributed")) {
104 engine
= E_DISTRIBUTED
;
105 } else if (!strcasecmp(optarg
, "joseki")) {
108 fprintf(stderr
, "%s: Invalid -e argument %s\n", argv
[0], optarg
);
113 debug_level
= atoi(optarg
);
116 fbookfile
= strdup(optarg
);
119 gtp_port
= strdup(optarg
);
122 log_port
= strdup(optarg
);
128 /* Time settings to follow; if specified,
129 * GTP time information is ignored. Useful
130 * e.g. when you want to force your bot to
131 * play weaker while giving the opponent
132 * reasonable time to play, or force play
133 * by number of simulations in timed games. */
134 /* Please see timeinfo.h:time_parse()
135 * description for syntax details. */
136 if (!time_parse(&ti_default
, optarg
)) {
137 fprintf(stderr
, "%s: Invalid -t argument %s\n", argv
[0], optarg
);
140 ti_default
.ignore_gtp
= true;
141 assert(ti_default
.period
!= TT_NULL
);
144 testfile
= strdup(optarg
);
153 open_log_port(log_port
);
157 fprintf(stderr
, "Random seed: %d\n", seed
);
159 struct board
*b
= board_init(fbookfile
);
160 struct time_info ti
[S_MAX
];
161 ti
[S_BLACK
] = ti_default
;
162 ti
[S_WHITE
] = ti_default
;
166 e_arg
= argv
[optind
];
167 struct engine
*e
= init_engine(engine
, e_arg
, b
);
175 open_gtp_connection(>p_sock
, gtp_port
);
180 while (fgets(buf
, 4096, stdin
)) {
182 fprintf(stderr
, "IN: %s", buf
);
184 enum parse_code c
= gtp_parse(b
, e
, ti
, buf
);
185 if (c
== P_ENGINE_RESET
) {
186 ti
[S_BLACK
] = ti_default
;
187 ti
[S_WHITE
] = ti_default
;
188 if (!e
->keep_on_clear
) {
191 e
= init_engine(engine
, e_arg
, b
);
193 } else if (c
== P_UNKNOWN_COMMAND
&& gtp_port
) {
194 /* The gtp command is a weak identity check,
195 * close the connection with a wrong peer. */
199 if (!gtp_port
) break;
200 open_gtp_connection(>p_sock
, gtp_port
);