Merge branch 'master' of git+ssh://repo.or.cz/srv/git/pachi
[pachi/json.git] / zzgo.c
blobb8ce54ff919db0a0b5c54f9fb092d021b205284c
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 long verbose_logs = 0;
29 int seed;
32 enum engine_id {
33 E_RANDOM,
34 E_REPLAY,
35 E_PATTERNSCAN,
36 E_MONTECARLO,
37 E_UCT,
38 E_DISTRIBUTED,
39 E_JOSEKI,
40 E_MAX,
43 static struct engine *(*engine_init[E_MAX])(char *arg, struct board *b) = {
44 engine_random_init,
45 engine_replay_init,
46 engine_patternscan_init,
47 engine_montecarlo_init,
48 engine_uct_init,
49 engine_distributed_init,
50 engine_joseki_init,
53 static struct engine *init_engine(enum engine_id engine, char *e_arg, struct board *b)
55 char *arg = e_arg? strdup(e_arg) : e_arg;
56 assert(engine < E_MAX);
57 struct engine *e = engine_init[engine](arg, b);
58 if (arg) free(arg);
59 return e;
62 static void done_engine(struct engine *e)
64 if (e->done) e->done(e);
65 if (e->data) free(e->data);
66 free(e);
69 static void usage(char *name)
71 fprintf(stderr, "Pachi version %s\n", PACHI_VERSION);
72 fprintf(stderr, "Usage: %s [-e random|replay|patternscan|montecarlo|uct|distributed]\n"
73 " [-d DEBUG_LEVEL] [-s RANDOM_SEED] [-t TIME_SETTINGS] [-u TEST_FILENAME]"
74 " [-g [HOST:]GTP_PORT] [-l [HOST:]LOG_PORT] [ENGINE_ARGS]\n", name);
77 int main(int argc, char *argv[])
79 enum engine_id engine = E_UCT;
80 struct time_info ti_default = { .period = TT_NULL };
81 char *testfile = NULL;
82 char *gtp_port = NULL;
83 char *log_port = NULL;
84 int gtp_sock = -1;
86 seed = time(NULL) ^ getpid();
88 int opt;
89 while ((opt = getopt(argc, argv, "e:d:g:l:s:t:u:")) != -1) {
90 switch (opt) {
91 case 'e':
92 if (!strcasecmp(optarg, "random")) {
93 engine = E_RANDOM;
94 } else if (!strcasecmp(optarg, "patternscan")) {
95 engine = E_PATTERNSCAN;
96 } else if (!strcasecmp(optarg, "replay")) {
97 engine = E_REPLAY;
98 } else if (!strcasecmp(optarg, "montecarlo")) {
99 engine = E_MONTECARLO;
100 } else if (!strcasecmp(optarg, "uct")) {
101 engine = E_UCT;
102 } else if (!strcasecmp(optarg, "distributed")) {
103 engine = E_DISTRIBUTED;
104 } else if (!strcasecmp(optarg, "joseki")) {
105 engine = E_JOSEKI;
106 } else {
107 fprintf(stderr, "%s: Invalid -e argument %s\n", argv[0], optarg);
108 exit(1);
110 break;
111 case 'd':
112 debug_level = atoi(optarg);
113 break;
114 case 'g':
115 gtp_port = strdup(optarg);
116 break;
117 case 'l':
118 log_port = strdup(optarg);
119 break;
120 case 's':
121 seed = atoi(optarg);
122 break;
123 case 't':
124 /* Time settings to follow; if specified,
125 * GTP time information is ignored. Useful
126 * e.g. when you want to force your bot to
127 * play weaker while giving the opponent
128 * reasonable time to play, or force play
129 * by number of simulations in timed games. */
130 /* Please see timeinfo.h:time_parse()
131 * description for syntax details. */
132 if (!time_parse(&ti_default, optarg)) {
133 fprintf(stderr, "%s: Invalid -t argument %s\n", argv[0], optarg);
134 exit(1);
136 ti_default.ignore_gtp = true;
137 assert(ti_default.period != TT_NULL);
138 break;
139 case 'u':
140 testfile = strdup(optarg);
141 break;
142 default: /* '?' */
143 usage(argv[0]);
144 exit(1);
148 if (log_port)
149 open_log_port(log_port);
151 fast_srandom(seed);
152 if (DEBUGL(0))
153 fprintf(stderr, "Random seed: %d\n", seed);
155 struct board *b = board_init();
156 struct time_info ti[S_MAX];
157 ti[S_BLACK] = ti_default;
158 ti[S_WHITE] = ti_default;
160 char *e_arg = NULL;
161 if (optind < argc)
162 e_arg = argv[optind];
163 struct engine *e = init_engine(engine, e_arg, b);
165 if (testfile) {
166 unittest(testfile);
167 return 0;
170 if (gtp_port) {
171 open_gtp_connection(&gtp_sock, gtp_port);
174 for (;;) {
175 char buf[4096];
176 while (fgets(buf, 4096, stdin)) {
177 if (DEBUGL(1))
178 fprintf(stderr, "IN: %s", buf);
180 enum parse_code c = gtp_parse(b, e, ti, buf);
181 if (c == P_ENGINE_RESET) {
182 ti[S_BLACK] = ti_default;
183 ti[S_WHITE] = ti_default;
184 if (!e->keep_on_clear) {
185 b->es = NULL;
186 done_engine(e);
187 e = init_engine(engine, e_arg, b);
189 } else if (c == P_UNKNOWN_COMMAND && gtp_port) {
190 /* The gtp command is a weak identity check,
191 * close the connection with a wrong peer. */
192 break;
195 if (!gtp_port) break;
196 open_gtp_connection(&gtp_sock, gtp_port);
198 done_engine(e);
199 return 0;