Merge branch 'master' of git+ssh://repo.or.cz/srv/git/pachi
[pachi/ann.git] / zzgo.c
blob3ce5bf5f38419b645b71e294e13a153369d5c7d3
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 "joseki/joseki.h"
18 #include "t-unit/test.h"
19 #include "uct/uct.h"
20 #include "distributed/distributed.h"
21 #include "gtp.h"
22 #include "timeinfo.h"
23 #include "random.h"
24 #include "version.h"
25 #include "network.h"
27 int debug_level = 3;
28 bool debug_boardprint = true;
29 long verbose_logs = 0;
30 int seed;
33 enum engine_id {
34 E_RANDOM,
35 E_REPLAY,
36 E_PATTERNSCAN,
37 E_MONTECARLO,
38 E_UCT,
39 E_DISTRIBUTED,
40 E_JOSEKI,
41 E_MAX,
44 static struct engine *(*engine_init[E_MAX])(char *arg, struct board *b) = {
45 engine_random_init,
46 engine_replay_init,
47 engine_patternscan_init,
48 engine_montecarlo_init,
49 engine_uct_init,
50 engine_distributed_init,
51 engine_joseki_init,
54 static struct engine *init_engine(enum engine_id engine, char *e_arg, struct board *b)
56 char *arg = e_arg? strdup(e_arg) : e_arg;
57 assert(engine < E_MAX);
58 struct engine *e = engine_init[engine](arg, b);
59 if (arg) free(arg);
60 return e;
63 static void done_engine(struct engine *e)
65 if (e->done) e->done(e);
66 if (e->data) free(e->data);
67 free(e);
70 static void usage(char *name)
72 fprintf(stderr, "Pachi version %s\n", PACHI_VERSION);
73 fprintf(stderr, "Usage: %s [-e random|replay|patternscan|montecarlo|uct|distributed]\n"
74 " [-d DEBUG_LEVEL] [-D] [-s RANDOM_SEED] [-t TIME_SETTINGS] [-u TEST_FILENAME]\n"
75 " [-g [HOST:]GTP_PORT] [-l [HOST:]LOG_PORT] [-f FBOOKFILE] [ENGINE_ARGS]\n", name);
78 int main(int argc, char *argv[])
80 enum engine_id engine = E_UCT;
81 struct time_info ti_default = { .period = TT_NULL };
82 char *testfile = NULL;
83 char *gtp_port = NULL;
84 char *log_port = NULL;
85 int gtp_sock = -1;
86 char *fbookfile = NULL;
88 seed = time(NULL) ^ getpid();
90 int opt;
91 while ((opt = getopt(argc, argv, "e:d:Df:g:l:s:t:u:")) != -1) {
92 switch (opt) {
93 case 'e':
94 if (!strcasecmp(optarg, "random")) {
95 engine = E_RANDOM;
96 } else if (!strcasecmp(optarg, "patternscan")) {
97 engine = E_PATTERNSCAN;
98 } else if (!strcasecmp(optarg, "replay")) {
99 engine = E_REPLAY;
100 } else if (!strcasecmp(optarg, "montecarlo")) {
101 engine = E_MONTECARLO;
102 } else if (!strcasecmp(optarg, "uct")) {
103 engine = E_UCT;
104 } else if (!strcasecmp(optarg, "distributed")) {
105 engine = E_DISTRIBUTED;
106 } else if (!strcasecmp(optarg, "joseki")) {
107 engine = E_JOSEKI;
108 } else {
109 fprintf(stderr, "%s: Invalid -e argument %s\n", argv[0], optarg);
110 exit(1);
112 break;
113 case 'd':
114 debug_level = atoi(optarg);
115 break;
116 case 'D':
117 debug_boardprint = false;
118 break;
119 case 'f':
120 fbookfile = strdup(optarg);
121 break;
122 case 'g':
123 gtp_port = strdup(optarg);
124 break;
125 case 'l':
126 log_port = strdup(optarg);
127 break;
128 case 's':
129 seed = atoi(optarg);
130 break;
131 case 't':
132 /* Time settings to follow; if specified,
133 * GTP time information is ignored. Useful
134 * e.g. when you want to force your bot to
135 * play weaker while giving the opponent
136 * reasonable time to play, or force play
137 * by number of simulations in timed games. */
138 /* Please see timeinfo.h:time_parse()
139 * description for syntax details. */
140 if (!time_parse(&ti_default, optarg)) {
141 fprintf(stderr, "%s: Invalid -t argument %s\n", argv[0], optarg);
142 exit(1);
144 ti_default.ignore_gtp = true;
145 assert(ti_default.period != TT_NULL);
146 break;
147 case 'u':
148 testfile = strdup(optarg);
149 break;
150 default: /* '?' */
151 usage(argv[0]);
152 exit(1);
156 if (log_port)
157 open_log_port(log_port);
159 fast_srandom(seed);
160 if (DEBUGL(0))
161 fprintf(stderr, "Random seed: %d\n", seed);
163 struct board *b = board_init(fbookfile);
164 struct time_info ti[S_MAX];
165 ti[S_BLACK] = ti_default;
166 ti[S_WHITE] = ti_default;
168 char *e_arg = NULL;
169 if (optind < argc)
170 e_arg = argv[optind];
171 struct engine *e = init_engine(engine, e_arg, b);
173 if (testfile) {
174 unittest(testfile);
175 return 0;
178 if (gtp_port) {
179 open_gtp_connection(&gtp_sock, gtp_port);
182 for (;;) {
183 char buf[4096];
184 while (fgets(buf, 4096, stdin)) {
185 if (DEBUGL(1))
186 fprintf(stderr, "IN: %s", buf);
188 enum parse_code c = gtp_parse(b, e, ti, buf);
189 if (c == P_ENGINE_RESET) {
190 ti[S_BLACK] = ti_default;
191 ti[S_WHITE] = ti_default;
192 if (!e->keep_on_clear) {
193 b->es = NULL;
194 done_engine(e);
195 e = init_engine(engine, e_arg, b);
197 } else if (c == P_UNKNOWN_COMMAND && gtp_port) {
198 /* The gtp command is a weak identity check,
199 * close the connection with a wrong peer. */
200 break;
203 if (!gtp_port) break;
204 open_gtp_connection(&gtp_sock, gtp_port);
206 done_engine(e);
207 return 0;