Merge pull request #50 from lemonsqueeze/can_countercap
[pachi.git] / pachi.c
blob4038c3e0fbaa50aabc4b831bca4833e4f8c8cb30
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 "patternplay/patternplay.h"
18 #include "joseki/joseki.h"
19 #include "t-unit/test.h"
20 #include "uct/uct.h"
21 #include "distributed/distributed.h"
22 #include "gtp.h"
23 #include "chat.h"
24 #include "timeinfo.h"
25 #include "random.h"
26 #include "version.h"
27 #include "network.h"
28 #include "uct/tree.h"
29 #include "dcnn.h"
31 int debug_level = 3;
32 bool debug_boardprint = true;
33 long verbose_logs = 0;
34 int seed;
37 enum engine_id {
38 E_RANDOM,
39 E_REPLAY,
40 E_PATTERNSCAN,
41 E_PATTERNPLAY,
42 E_MONTECARLO,
43 E_UCT,
44 E_DISTRIBUTED,
45 E_JOSEKI,
46 #ifdef DCNN
47 E_DCNN,
48 #endif
49 E_MAX,
52 static struct engine *(*engine_init[E_MAX])(char *arg, struct board *b) = {
53 engine_random_init,
54 engine_replay_init,
55 engine_patternscan_init,
56 engine_patternplay_init,
57 engine_montecarlo_init,
58 engine_uct_init,
59 engine_distributed_init,
60 engine_joseki_init,
61 #ifdef DCNN
62 engine_dcnn_init,
63 #endif
66 static struct engine *init_engine(enum engine_id engine, char *e_arg, struct board *b)
68 char *arg = e_arg? strdup(e_arg) : e_arg;
69 assert(engine < E_MAX);
70 struct engine *e = engine_init[engine](arg, b);
71 if (arg) free(arg);
72 return e;
75 static void usage(char *name)
77 fprintf(stderr, "Pachi version %s\n", PACHI_VERSION);
78 fprintf(stderr, "Usage: %s [-e random|replay|montecarlo|uct|distributed|dcnn]\n"
79 " [-d DEBUG_LEVEL] [-D] [-r RULESET] [-s RANDOM_SEED] [-t TIME_SETTINGS] [-u TEST_FILENAME]\n"
80 " [-g [HOST:]GTP_PORT] [-l [HOST:]LOG_PORT] [-f FBOOKFILE] [ENGINE_ARGS]\n", name);
83 int main(int argc, char *argv[])
85 enum engine_id engine = E_UCT;
86 struct time_info ti_default = { .period = TT_NULL };
87 char *testfile = NULL;
88 char *gtp_port = NULL;
89 char *log_port = NULL;
90 int gtp_sock = -1;
91 char *chatfile = NULL;
92 char *fbookfile = NULL;
93 char *ruleset = NULL;
95 seed = time(NULL) ^ getpid();
97 int opt;
98 while ((opt = getopt(argc, argv, "c:e:d:Df:g:l:r:s:t:u:")) != -1) {
99 switch (opt) {
100 case 'c':
101 chatfile = strdup(optarg);
102 break;
103 case 'e':
104 if (!strcasecmp(optarg, "random")) {
105 engine = E_RANDOM;
106 } else if (!strcasecmp(optarg, "replay")) {
107 engine = E_REPLAY;
108 } else if (!strcasecmp(optarg, "montecarlo")) {
109 engine = E_MONTECARLO;
110 } else if (!strcasecmp(optarg, "uct")) {
111 engine = E_UCT;
112 } else if (!strcasecmp(optarg, "distributed")) {
113 engine = E_DISTRIBUTED;
114 } else if (!strcasecmp(optarg, "patternscan")) {
115 engine = E_PATTERNSCAN;
116 } else if (!strcasecmp(optarg, "patternplay")) {
117 engine = E_PATTERNPLAY;
118 } else if (!strcasecmp(optarg, "joseki")) {
119 engine = E_JOSEKI;
120 #ifdef DCNN
121 } else if (!strcasecmp(optarg, "dcnn")) {
122 engine = E_DCNN;
123 #endif
124 } else {
125 fprintf(stderr, "%s: Invalid -e argument %s\n", argv[0], optarg);
126 exit(1);
128 break;
129 case 'd':
130 debug_level = atoi(optarg);
131 break;
132 case 'D':
133 debug_boardprint = false;
134 break;
135 case 'f':
136 fbookfile = strdup(optarg);
137 break;
138 case 'g':
139 gtp_port = strdup(optarg);
140 break;
141 case 'l':
142 log_port = strdup(optarg);
143 break;
144 case 'r':
145 ruleset = strdup(optarg);
146 break;
147 case 's':
148 seed = atoi(optarg);
149 break;
150 case 't':
151 /* Time settings to follow; if specified,
152 * GTP time information is ignored. Useful
153 * e.g. when you want to force your bot to
154 * play weaker while giving the opponent
155 * reasonable time to play, or force play
156 * by number of simulations in timed games. */
157 /* Please see timeinfo.h:time_parse()
158 * description for syntax details. */
159 if (!time_parse(&ti_default, optarg)) {
160 fprintf(stderr, "%s: Invalid -t argument %s\n", argv[0], optarg);
161 exit(1);
163 ti_default.ignore_gtp = true;
164 assert(ti_default.period != TT_NULL);
165 break;
166 case 'u':
167 testfile = strdup(optarg);
168 break;
169 default: /* '?' */
170 usage(argv[0]);
171 exit(1);
175 dcnn_quiet_caffe(argc, argv);
176 if (log_port)
177 open_log_port(log_port);
179 fast_srandom(seed);
180 if (DEBUGL(0))
181 fprintf(stderr, "Random seed: %d\n", seed);
183 struct board *b = board_init(fbookfile);
184 if (ruleset) {
185 if (!board_set_rules(b, ruleset)) {
186 fprintf(stderr, "Unknown ruleset: %s\n", ruleset);
187 exit(1);
191 struct time_info ti[S_MAX];
192 ti[S_BLACK] = ti_default;
193 ti[S_WHITE] = ti_default;
195 chat_init(chatfile);
197 char *e_arg = NULL;
198 if (optind < argc)
199 e_arg = argv[optind];
200 struct engine *e = init_engine(engine, e_arg, b);
202 if (testfile) {
203 unittest(testfile);
204 return 0;
207 if (gtp_port) {
208 open_gtp_connection(&gtp_sock, gtp_port);
211 for (;;) {
212 char buf[4096];
213 while (fgets(buf, 4096, stdin)) {
214 if (DEBUGL(1))
215 fprintf(stderr, "IN: %s", buf);
217 enum parse_code c = gtp_parse(b, e, ti, buf);
218 if (c == P_ENGINE_RESET) {
219 ti[S_BLACK] = ti_default;
220 ti[S_WHITE] = ti_default;
221 if (!e->keep_on_clear) {
222 b->es = NULL;
223 engine_done(e);
224 e = init_engine(engine, e_arg, b);
226 } else if (c == P_UNKNOWN_COMMAND && gtp_port) {
227 /* The gtp command is a weak identity check,
228 * close the connection with a wrong peer. */
229 break;
232 if (!gtp_port) break;
233 open_gtp_connection(&gtp_sock, gtp_port);
235 engine_done(e);
236 chat_done();
237 free(testfile);
238 free(gtp_port);
239 free(log_port);
240 free(chatfile);
241 free(fbookfile);
242 free(ruleset);
243 return 0;