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"
38 static struct engine
*(*engine_init
[E_MAX
])(char *arg
, struct board
*b
) = {
41 engine_patternscan_init
,
42 engine_montecarlo_init
,
46 static struct engine
*init_engine(enum engine_id engine
, char *e_arg
, struct board
*b
)
48 char *arg
= e_arg
? strdup(e_arg
) : e_arg
;
49 assert(engine
< E_MAX
);
50 struct engine
*e
= engine_init
[engine
](arg
, b
);
55 static void done_engine(struct engine
*e
)
57 if (e
->done
) e
->done(e
);
58 if (e
->data
) free(e
->data
);
62 static void usage(char *name
)
64 fprintf(stderr
, "Pachi version %s\n", PACHI_VERSION
);
65 fprintf(stderr
, "Usage: %s [-e random|replay|patternscan|montecarlo|uct]\n"
66 " [-d DEBUG_LEVEL] [-s RANDOM_SEED] [-t TIME_SETTINGS] [-u TEST_FILENAME]"
67 " [-g [HOST:]GTP_PORT] [-l [HOST:]LOG_PORT] [ENGINE_ARGS]\n", name
);
70 bool engine_reset
= false;
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")) {
99 fprintf(stderr
, "%s: Invalid -e argument %s\n", argv
[0], optarg
);
104 debug_level
= atoi(optarg
);
107 gtp_port
= strdup(optarg
);
110 log_port
= strdup(optarg
);
116 /* Time settings to follow; if specified,
117 * GTP time information is ignored. Useful
118 * e.g. when you want to force your bot to
119 * play weaker while giving the opponent
120 * reasonable time to play, or force play
121 * by number of simulations in timed games. */
122 /* Please see timeinfo.h:time_parse()
123 * description for syntax details. */
124 if (!time_parse(&ti_default
, optarg
)) {
125 fprintf(stderr
, "%s: Invalid -t argument %s\n", argv
[0], optarg
);
128 ti_default
.ignore_gtp
= true;
129 assert(ti_default
.period
!= TT_NULL
);
132 testfile
= strdup(optarg
);
141 open_log_port(log_port
);
145 fprintf(stderr
, "Random seed: %d\n", seed
);
147 struct board
*b
= board_init();
148 struct time_info ti
[S_MAX
];
149 ti
[S_BLACK
] = ti_default
;
150 ti
[S_WHITE
] = ti_default
;
154 e_arg
= argv
[optind
];
155 struct engine
*e
= init_engine(engine
, e_arg
, b
);
163 open_gtp_connection(>p_sock
, gtp_port
);
168 while (fgets(buf
, 4096, stdin
)) {
170 fprintf(stderr
, "IN: %s", buf
);
171 gtp_parse(b
, e
, ti
, buf
);
173 if (!e
->keep_on_clear
) {
176 e
= init_engine(engine
, e_arg
, b
);
177 ti
[S_BLACK
] = ti_default
;
178 ti
[S_WHITE
] = ti_default
;
180 engine_reset
= false;
183 if (!gtp_port
) break;
184 open_gtp_connection(>p_sock
, gtp_port
);