Moggy Patterns: Add block side cut
[pachi.git] / zzgo.c
blob0ff1e456bfa9b9331c22b266190ab55b276fa965
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 "joseki/base.h"
19 #include "t-unit/test.h"
20 #include "uct/uct.h"
21 #include "distributed/distributed.h"
22 #include "gtp.h"
23 #include "timeinfo.h"
24 #include "random.h"
25 #include "version.h"
26 #include "network.h"
28 int debug_level = 3;
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] [-s RANDOM_SEED] [-t TIME_SETTINGS] [-u TEST_FILENAME]"
75 " [-g [HOST:]GTP_PORT] [-l [HOST:]LOG_PORT] [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;
87 seed = time(NULL) ^ getpid();
89 int opt;
90 while ((opt = getopt(argc, argv, "e:d:g:l:s:t:u:")) != -1) {
91 switch (opt) {
92 case 'e':
93 if (!strcasecmp(optarg, "random")) {
94 engine = E_RANDOM;
95 } else if (!strcasecmp(optarg, "patternscan")) {
96 engine = E_PATTERNSCAN;
97 } else if (!strcasecmp(optarg, "replay")) {
98 engine = E_REPLAY;
99 } else if (!strcasecmp(optarg, "montecarlo")) {
100 engine = E_MONTECARLO;
101 } else if (!strcasecmp(optarg, "uct")) {
102 engine = E_UCT;
103 } else if (!strcasecmp(optarg, "distributed")) {
104 engine = E_DISTRIBUTED;
105 } else if (!strcasecmp(optarg, "joseki")) {
106 engine = E_JOSEKI;
107 } else {
108 fprintf(stderr, "%s: Invalid -e argument %s\n", argv[0], optarg);
109 exit(1);
111 break;
112 case 'd':
113 debug_level = atoi(optarg);
114 break;
115 case 'g':
116 gtp_port = strdup(optarg);
117 break;
118 case 'l':
119 log_port = strdup(optarg);
120 break;
121 case 's':
122 seed = atoi(optarg);
123 break;
124 case 't':
125 /* Time settings to follow; if specified,
126 * GTP time information is ignored. Useful
127 * e.g. when you want to force your bot to
128 * play weaker while giving the opponent
129 * reasonable time to play, or force play
130 * by number of simulations in timed games. */
131 /* Please see timeinfo.h:time_parse()
132 * description for syntax details. */
133 if (!time_parse(&ti_default, optarg)) {
134 fprintf(stderr, "%s: Invalid -t argument %s\n", argv[0], optarg);
135 exit(1);
137 ti_default.ignore_gtp = true;
138 assert(ti_default.period != TT_NULL);
139 break;
140 case 'u':
141 testfile = strdup(optarg);
142 break;
143 default: /* '?' */
144 usage(argv[0]);
145 exit(1);
149 if (log_port)
150 open_log_port(log_port);
152 fast_srandom(seed);
153 if (DEBUGL(0))
154 fprintf(stderr, "Random seed: %d\n", seed);
156 joseki_load();
158 struct board *b = board_init();
159 struct time_info ti[S_MAX];
160 ti[S_BLACK] = ti_default;
161 ti[S_WHITE] = ti_default;
163 char *e_arg = NULL;
164 if (optind < argc)
165 e_arg = argv[optind];
166 struct engine *e = init_engine(engine, e_arg, b);
168 if (testfile) {
169 unittest(testfile);
170 return 0;
173 if (gtp_port) {
174 open_gtp_connection(&gtp_sock, gtp_port);
177 for (;;) {
178 char buf[4096];
179 while (fgets(buf, 4096, stdin)) {
180 if (DEBUGL(1))
181 fprintf(stderr, "IN: %s", buf);
183 enum parse_code c = gtp_parse(b, e, ti, buf);
184 if (c == P_ENGINE_RESET) {
185 ti[S_BLACK] = ti_default;
186 ti[S_WHITE] = ti_default;
187 if (!e->keep_on_clear) {
188 b->es = NULL;
189 done_engine(e);
190 e = init_engine(engine, e_arg, b);
192 } else if (c == P_UNKNOWN_COMMAND && gtp_port) {
193 /* The gtp command is a weak identity check,
194 * close the connection with a wrong peer. */
195 break;
198 if (!gtp_port) break;
199 open_gtp_connection(&gtp_sock, gtp_port);
201 done_engine(e);
202 return 0;