13 #include "replay/replay.h"
14 #include "montecarlo/montecarlo.h"
15 #include "random/random.h"
16 #include "patternscan/patternscan.h"
17 #include "patternplay/patternplay.h"
18 #include "joseki/joseki.h"
19 #include "t-unit/test.h"
21 #include "distributed/distributed.h"
30 bool debug_boardprint
= true;
31 long verbose_logs
= 0;
47 static struct engine
*(*engine_init
[E_MAX
])(char *arg
, struct board
*b
) = {
50 engine_patternscan_init
,
51 engine_patternplay_init
,
52 engine_montecarlo_init
,
54 engine_distributed_init
,
58 static struct engine
*init_engine(enum engine_id engine
, char *e_arg
, struct board
*b
)
60 char *arg
= e_arg
? strdup(e_arg
) : e_arg
;
61 assert(engine
< E_MAX
);
62 struct engine
*e
= engine_init
[engine
](arg
, b
);
67 static void done_engine(struct engine
*e
)
69 if (e
->done
) e
->done(e
);
70 if (e
->data
) free(e
->data
);
74 static void usage(char *name
)
76 fprintf(stderr
, "Pachi version %s\n", PACHI_VERSION
);
77 fprintf(stderr
, "Usage: %s [-e random|replay|montecarlo|uct|distributed]\n"
78 " [-d DEBUG_LEVEL] [-D] [-r RULESET] [-s RANDOM_SEED] [-t TIME_SETTINGS] [-u TEST_FILENAME]\n"
79 " [-g [HOST:]GTP_PORT] [-l [HOST:]LOG_PORT] [-f FBOOKFILE] [ENGINE_ARGS]\n", name
);
82 int main(int argc
, char *argv
[])
84 enum engine_id engine
= E_UCT
;
85 struct time_info ti_default
= { .period
= TT_NULL
};
86 char *testfile
= NULL
;
87 char *gtp_port
= NULL
;
88 char *log_port
= NULL
;
90 char *chatfile
= NULL
;
91 char *fbookfile
= NULL
;
94 seed
= time(NULL
) ^ getpid();
97 while ((opt
= getopt(argc
, argv
, "c:e:d:Df:g:l:r:s:t:u:")) != -1) {
100 chatfile
= strdup(optarg
);
103 if (!strcasecmp(optarg
, "random")) {
105 } else if (!strcasecmp(optarg
, "replay")) {
107 } else if (!strcasecmp(optarg
, "montecarlo")) {
108 engine
= E_MONTECARLO
;
109 } else if (!strcasecmp(optarg
, "uct")) {
111 } else if (!strcasecmp(optarg
, "distributed")) {
112 engine
= E_DISTRIBUTED
;
113 } else if (!strcasecmp(optarg
, "patternscan")) {
114 engine
= E_PATTERNSCAN
;
115 } else if (!strcasecmp(optarg
, "patternplay")) {
116 engine
= E_PATTERNPLAY
;
117 } else if (!strcasecmp(optarg
, "joseki")) {
120 fprintf(stderr
, "%s: Invalid -e argument %s\n", argv
[0], optarg
);
125 debug_level
= atoi(optarg
);
128 debug_boardprint
= false;
131 fbookfile
= strdup(optarg
);
134 gtp_port
= strdup(optarg
);
137 log_port
= strdup(optarg
);
140 ruleset
= strdup(optarg
);
146 /* Time settings to follow; if specified,
147 * GTP time information is ignored. Useful
148 * e.g. when you want to force your bot to
149 * play weaker while giving the opponent
150 * reasonable time to play, or force play
151 * by number of simulations in timed games. */
152 /* Please see timeinfo.h:time_parse()
153 * description for syntax details. */
154 if (!time_parse(&ti_default
, optarg
)) {
155 fprintf(stderr
, "%s: Invalid -t argument %s\n", argv
[0], optarg
);
158 ti_default
.ignore_gtp
= true;
159 assert(ti_default
.period
!= TT_NULL
);
162 testfile
= strdup(optarg
);
171 open_log_port(log_port
);
175 fprintf(stderr
, "Random seed: %d\n", seed
);
177 struct board
*b
= board_init(fbookfile
);
179 if (!board_set_rules(b
, ruleset
)) {
180 fprintf(stderr
, "Unknown ruleset: %s\n", ruleset
);
185 struct time_info ti
[S_MAX
];
186 ti
[S_BLACK
] = ti_default
;
187 ti
[S_WHITE
] = ti_default
;
193 e_arg
= argv
[optind
];
194 struct engine
*e
= init_engine(engine
, e_arg
, b
);
202 open_gtp_connection(>p_sock
, gtp_port
);
207 while (fgets(buf
, 4096, stdin
)) {
209 fprintf(stderr
, "IN: %s", buf
);
211 enum parse_code c
= gtp_parse(b
, e
, ti
, buf
);
212 if (c
== P_ENGINE_RESET
) {
213 ti
[S_BLACK
] = ti_default
;
214 ti
[S_WHITE
] = ti_default
;
215 if (!e
->keep_on_clear
) {
218 e
= init_engine(engine
, e_arg
, b
);
220 } else if (c
== P_UNKNOWN_COMMAND
&& gtp_port
) {
221 /* The gtp command is a weak identity check,
222 * close the connection with a wrong peer. */
226 if (!gtp_port
) break;
227 open_gtp_connection(>p_sock
, gtp_port
);