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] [-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
;
93 seed
= time(NULL
) ^ getpid();
96 while ((opt
= getopt(argc
, argv
, "c:e:d:Df:g:l:s:t:u:")) != -1) {
99 chatfile
= strdup(optarg
);
102 if (!strcasecmp(optarg
, "random")) {
104 } else if (!strcasecmp(optarg
, "replay")) {
106 } else if (!strcasecmp(optarg
, "montecarlo")) {
107 engine
= E_MONTECARLO
;
108 } else if (!strcasecmp(optarg
, "uct")) {
110 } else if (!strcasecmp(optarg
, "distributed")) {
111 engine
= E_DISTRIBUTED
;
112 } else if (!strcasecmp(optarg
, "patternscan")) {
113 engine
= E_PATTERNSCAN
;
114 } else if (!strcasecmp(optarg
, "patternplay")) {
115 engine
= E_PATTERNPLAY
;
116 } else if (!strcasecmp(optarg
, "joseki")) {
119 fprintf(stderr
, "%s: Invalid -e argument %s\n", argv
[0], optarg
);
124 debug_level
= atoi(optarg
);
127 debug_boardprint
= false;
130 fbookfile
= strdup(optarg
);
133 gtp_port
= strdup(optarg
);
136 log_port
= strdup(optarg
);
142 /* Time settings to follow; if specified,
143 * GTP time information is ignored. Useful
144 * e.g. when you want to force your bot to
145 * play weaker while giving the opponent
146 * reasonable time to play, or force play
147 * by number of simulations in timed games. */
148 /* Please see timeinfo.h:time_parse()
149 * description for syntax details. */
150 if (!time_parse(&ti_default
, optarg
)) {
151 fprintf(stderr
, "%s: Invalid -t argument %s\n", argv
[0], optarg
);
154 ti_default
.ignore_gtp
= true;
155 assert(ti_default
.period
!= TT_NULL
);
158 testfile
= strdup(optarg
);
167 open_log_port(log_port
);
171 fprintf(stderr
, "Random seed: %d\n", seed
);
173 struct board
*b
= board_init(fbookfile
);
174 struct time_info ti
[S_MAX
];
175 ti
[S_BLACK
] = ti_default
;
176 ti
[S_WHITE
] = ti_default
;
182 e_arg
= argv
[optind
];
183 struct engine
*e
= init_engine(engine
, e_arg
, b
);
191 open_gtp_connection(>p_sock
, gtp_port
);
196 while (fgets(buf
, 4096, stdin
)) {
198 fprintf(stderr
, "IN: %s", buf
);
200 enum parse_code c
= gtp_parse(b
, e
, ti
, buf
);
201 if (c
== P_ENGINE_RESET
) {
202 ti
[S_BLACK
] = ti_default
;
203 ti
[S_WHITE
] = ti_default
;
204 if (!e
->keep_on_clear
) {
207 e
= init_engine(engine
, e_arg
, b
);
209 } else if (c
== P_UNKNOWN_COMMAND
&& gtp_port
) {
210 /* The gtp command is a weak identity check,
211 * close the connection with a wrong peer. */
215 if (!gtp_port
) break;
216 open_gtp_connection(>p_sock
, gtp_port
);