Merge branch 'master' of ssh://repo.or.cz/srv/git/pachi
[pachi.git] / zzgo.c
blobdb077476c4fa9f1315e32aee450a0b19998ce6d4
1 #define DEBUG
2 #include <assert.h>
3 #include <getopt.h>
4 #include <stdio.h>
5 #include <stdlib.h>
6 #include <string.h>
7 #include <time.h>
8 #include <unistd.h>
10 #include "board.h"
11 #include "debug.h"
12 #include "engine.h"
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"
18 #include "uct/uct.h"
19 #include "distributed/distributed.h"
20 #include "gtp.h"
21 #include "timeinfo.h"
22 #include "random.h"
23 #include "version.h"
24 #include "network.h"
26 int debug_level = 3;
27 long verbose_logs = 0;
28 int seed;
31 enum engine_id {
32 E_RANDOM,
33 E_REPLAY,
34 E_PATTERNSCAN,
35 E_MONTECARLO,
36 E_UCT,
37 E_DISTRIBUTED,
38 E_MAX,
41 static struct engine *(*engine_init[E_MAX])(char *arg, struct board *b) = {
42 engine_random_init,
43 engine_replay_init,
44 engine_patternscan_init,
45 engine_montecarlo_init,
46 engine_uct_init,
47 engine_distributed_init,
50 static struct engine *init_engine(enum engine_id engine, char *e_arg, struct board *b)
52 char *arg = e_arg? strdup(e_arg) : e_arg;
53 assert(engine < E_MAX);
54 struct engine *e = engine_init[engine](arg, b);
55 if (arg) free(arg);
56 return e;
59 static void done_engine(struct engine *e)
61 if (e->done) e->done(e);
62 if (e->data) free(e->data);
63 free(e);
66 static void usage(char *name)
68 fprintf(stderr, "Pachi version %s\n", PACHI_VERSION);
69 fprintf(stderr, "Usage: %s [-e random|replay|patternscan|montecarlo|uct|distributed]\n"
70 " [-d DEBUG_LEVEL] [-s RANDOM_SEED] [-t TIME_SETTINGS] [-u TEST_FILENAME]"
71 " [-g [HOST:]GTP_PORT] [-l [HOST:]LOG_PORT] [ENGINE_ARGS]\n", name);
74 int main(int argc, char *argv[])
76 enum engine_id engine = E_UCT;
77 struct time_info ti_default = { .period = TT_NULL };
78 char *testfile = NULL;
79 char *gtp_port = NULL;
80 char *log_port = NULL;
81 int gtp_sock = -1;
83 seed = time(NULL) ^ getpid();
85 int opt;
86 while ((opt = getopt(argc, argv, "e:d:g:l:s:t:u:")) != -1) {
87 switch (opt) {
88 case 'e':
89 if (!strcasecmp(optarg, "random")) {
90 engine = E_RANDOM;
91 } else if (!strcasecmp(optarg, "patternscan")) {
92 engine = E_PATTERNSCAN;
93 } else if (!strcasecmp(optarg, "replay")) {
94 engine = E_REPLAY;
95 } else if (!strcasecmp(optarg, "montecarlo")) {
96 engine = E_MONTECARLO;
97 } else if (!strcasecmp(optarg, "uct")) {
98 engine = E_UCT;
99 } else if (!strcasecmp(optarg, "distributed")) {
100 engine = E_DISTRIBUTED;
101 } else {
102 fprintf(stderr, "%s: Invalid -e argument %s\n", argv[0], optarg);
103 exit(1);
105 break;
106 case 'd':
107 debug_level = atoi(optarg);
108 break;
109 case 'g':
110 gtp_port = strdup(optarg);
111 break;
112 case 'l':
113 log_port = strdup(optarg);
114 break;
115 case 's':
116 seed = atoi(optarg);
117 break;
118 case 't':
119 /* Time settings to follow; if specified,
120 * GTP time information is ignored. Useful
121 * e.g. when you want to force your bot to
122 * play weaker while giving the opponent
123 * reasonable time to play, or force play
124 * by number of simulations in timed games. */
125 /* Please see timeinfo.h:time_parse()
126 * description for syntax details. */
127 if (!time_parse(&ti_default, optarg)) {
128 fprintf(stderr, "%s: Invalid -t argument %s\n", argv[0], optarg);
129 exit(1);
131 ti_default.ignore_gtp = true;
132 assert(ti_default.period != TT_NULL);
133 break;
134 case 'u':
135 testfile = strdup(optarg);
136 break;
137 default: /* '?' */
138 usage(argv[0]);
139 exit(1);
143 if (log_port)
144 open_log_port(log_port);
146 fast_srandom(seed);
147 if (DEBUGL(0))
148 fprintf(stderr, "Random seed: %d\n", seed);
150 struct board *b = board_init();
151 struct time_info ti[S_MAX];
152 ti[S_BLACK] = ti_default;
153 ti[S_WHITE] = ti_default;
155 char *e_arg = NULL;
156 if (optind < argc)
157 e_arg = argv[optind];
158 struct engine *e = init_engine(engine, e_arg, b);
160 if (testfile) {
161 unittest(testfile);
162 return 0;
165 if (gtp_port) {
166 open_gtp_connection(&gtp_sock, gtp_port);
169 for (;;) {
170 char buf[4096];
171 while (fgets(buf, 4096, stdin)) {
172 if (DEBUGL(1))
173 fprintf(stderr, "IN: %s", buf);
175 enum parse_code c = gtp_parse(b, e, ti, buf);
176 if (c == P_ENGINE_RESET) {
177 ti[S_BLACK] = ti_default;
178 ti[S_WHITE] = ti_default;
179 if (!e->keep_on_clear) {
180 b->es = NULL;
181 done_engine(e);
182 e = init_engine(engine, e_arg, b);
184 } else if (c == P_UNKNOWN_COMMAND && gtp_port) {
185 /* The gtp command is a weak identity check,
186 * close the connection with a wrong peer. */
187 break;
190 if (!gtp_port) break;
191 open_gtp_connection(&gtp_sock, gtp_port);
193 done_engine(e);
194 return 0;