t-regress/TODO: Add some categories of games
[pachi/nmclean.git] / pachi.c
blob4188d589a5e4ccf2a5ceae15c95f78268706ad21
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 "joseki/joseki.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 bool debug_boardprint = true;
28 long verbose_logs = 0;
29 int seed;
32 enum engine_id {
33 E_RANDOM,
34 E_REPLAY,
35 E_MONTECARLO,
36 E_UCT,
37 E_DISTRIBUTED,
38 E_JOSEKI,
39 E_MAX,
42 static struct engine *(*engine_init[E_MAX])(char *arg, struct board *b) = {
43 engine_random_init,
44 engine_replay_init,
45 engine_montecarlo_init,
46 engine_uct_init,
47 engine_distributed_init,
48 engine_joseki_init,
51 static struct engine *init_engine(enum engine_id engine, char *e_arg, struct board *b)
53 char *arg = e_arg? strdup(e_arg) : e_arg;
54 assert(engine < E_MAX);
55 struct engine *e = engine_init[engine](arg, b);
56 if (arg) free(arg);
57 return e;
60 static void done_engine(struct engine *e)
62 if (e->done) e->done(e);
63 if (e->data) free(e->data);
64 free(e);
67 static void usage(char *name)
69 fprintf(stderr, "Pachi version %s\n", PACHI_VERSION);
70 fprintf(stderr, "Usage: %s [-e random|replay|montecarlo|uct|distributed]\n"
71 " [-d DEBUG_LEVEL] [-D] [-s RANDOM_SEED] [-t TIME_SETTINGS] [-u TEST_FILENAME]\n"
72 " [-g [HOST:]GTP_PORT] [-l [HOST:]LOG_PORT] [-f FBOOKFILE] [ENGINE_ARGS]\n", name);
75 int main(int argc, char *argv[])
77 enum engine_id engine = E_UCT;
78 struct time_info ti_default = { .period = TT_NULL };
79 char *testfile = NULL;
80 char *gtp_port = NULL;
81 char *log_port = NULL;
82 int gtp_sock = -1;
83 char *fbookfile = NULL;
85 seed = time(NULL) ^ getpid();
87 int opt;
88 while ((opt = getopt(argc, argv, "e:d:Df:g:l:s:t:u:")) != -1) {
89 switch (opt) {
90 case 'e':
91 if (!strcasecmp(optarg, "random")) {
92 engine = E_RANDOM;
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 if (!strcasecmp(optarg, "joseki")) {
102 engine = E_JOSEKI;
103 } else {
104 fprintf(stderr, "%s: Invalid -e argument %s\n", argv[0], optarg);
105 exit(1);
107 break;
108 case 'd':
109 debug_level = atoi(optarg);
110 break;
111 case 'D':
112 debug_boardprint = false;
113 break;
114 case 'f':
115 fbookfile = strdup(optarg);
116 break;
117 case 'g':
118 gtp_port = strdup(optarg);
119 break;
120 case 'l':
121 log_port = strdup(optarg);
122 break;
123 case 's':
124 seed = atoi(optarg);
125 break;
126 case 't':
127 /* Time settings to follow; if specified,
128 * GTP time information is ignored. Useful
129 * e.g. when you want to force your bot to
130 * play weaker while giving the opponent
131 * reasonable time to play, or force play
132 * by number of simulations in timed games. */
133 /* Please see timeinfo.h:time_parse()
134 * description for syntax details. */
135 if (!time_parse(&ti_default, optarg)) {
136 fprintf(stderr, "%s: Invalid -t argument %s\n", argv[0], optarg);
137 exit(1);
139 ti_default.ignore_gtp = true;
140 assert(ti_default.period != TT_NULL);
141 break;
142 case 'u':
143 testfile = strdup(optarg);
144 break;
145 default: /* '?' */
146 usage(argv[0]);
147 exit(1);
151 if (log_port)
152 open_log_port(log_port);
154 fast_srandom(seed);
155 if (DEBUGL(0))
156 fprintf(stderr, "Random seed: %d\n", seed);
158 struct board *b = board_init(fbookfile);
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;