Add support for time_left, time_setting and kgs-time_settings gtp commands.
[pachi/derm.git] / zzgo.c
blobeb93050c34aa7ff357c8d4ee849f534d8f6e3997
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 "t-unit/test.h"
18 #include "uct/uct.h"
19 #include "gtp.h"
20 #include "timeinfo.h"
21 #include "random.h"
22 #include "version.h"
24 int debug_level = 1;
25 int seed;
28 enum engine_id {
29 E_RANDOM,
30 E_REPLAY,
31 E_PATTERNSCAN,
32 E_MONTECARLO,
33 E_UCT,
34 E_MAX,
37 static struct engine *(*engine_init[E_MAX])(char *arg, struct board *b) = {
38 engine_random_init,
39 engine_replay_init,
40 engine_patternscan_init,
41 engine_montecarlo_init,
42 engine_uct_init,
45 static struct engine *init_engine(enum engine_id engine, char *e_arg, struct board *b)
47 char *arg = e_arg? strdup(e_arg) : e_arg;
48 assert(engine < E_MAX);
49 struct engine *e = engine_init[engine](arg, b);
50 if (arg) free(arg);
51 return e;
54 static void done_engine(struct engine *e)
56 if (e->done) e->done(e);
57 if (e->data) free(e->data);
58 free(e);
61 bool engine_reset = false;
64 int main(int argc, char *argv[])
66 enum engine_id engine = E_UCT;
67 /* time_info for none(ignored), black, white: */
68 struct time_info ti_default[] = { { .period = TT_NULL }, { .period = TT_NULL }, { .period = TT_NULL }};
69 char *testfile = NULL;
71 seed = time(NULL) ^ getpid();
73 int opt;
74 while ((opt = getopt(argc, argv, "e:d:s:t:u:")) != -1) {
75 switch (opt) {
76 case 'e':
77 if (!strcasecmp(optarg, "random")) {
78 engine = E_RANDOM;
79 } else if (!strcasecmp(optarg, "patternscan")) {
80 engine = E_PATTERNSCAN;
81 } else if (!strcasecmp(optarg, "replay")) {
82 engine = E_REPLAY;
83 } else if (!strcasecmp(optarg, "montecarlo")) {
84 engine = E_MONTECARLO;
85 } else if (!strcasecmp(optarg, "uct")) {
86 engine = E_UCT;
87 } else {
88 fprintf(stderr, "%s: Invalid -e argument %s\n", argv[0], optarg);
89 exit(1);
91 break;
92 case 'd':
93 debug_level = atoi(optarg);
94 break;
95 case 's':
96 seed = atoi(optarg);
97 break;
98 case 't':
99 /* Time settings to follow by default (if no
100 * time_left GTP commands are received). Please
101 * see timeinfo.h:time_parse() description for
102 * syntax details. */
103 if (!time_parse(&ti_default[S_BLACK], optarg)) {
104 fprintf(stderr, "%s: Invalid -t argument %s\n", argv[0], optarg);
105 exit(1);
107 ti_default[S_WHITE] = ti_default[S_BLACK];
108 break;
109 case 'u':
110 testfile = strdup(optarg);
111 break;
112 default: /* '?' */
113 fprintf(stderr, "Pachi version %s\n", PACHI_VERSION);
114 fprintf(stderr, "Usage: %s [-e random|replay|patternscan|montecarlo|uct] [-d DEBUG_LEVEL] [-s RANDOM_SEED] [-t TIME_SETTINGS] [-u TEST_FILENAME] [ENGINE_ARGS]\n",
115 argv[0]);
116 exit(1);
120 fast_srandom(seed);
121 fprintf(stderr, "Random seed: %d\n", seed);
123 struct board *b = board_init();
124 struct time_info ti[S_WHITE+1];
125 ti[S_BLACK] = ti_default[S_BLACK];
126 ti[S_WHITE] = ti_default[S_WHITE];
128 char *e_arg = NULL;
129 if (optind < argc)
130 e_arg = argv[optind];
131 struct engine *e = init_engine(engine, e_arg, b);
133 if (testfile) {
134 unittest(testfile);
135 return 0;
138 char buf[4096];
139 while (fgets(buf, 4096, stdin)) {
140 if (DEBUGL(1))
141 fprintf(stderr, "IN: %s", buf);
142 gtp_parse(b, e, ti, buf);
143 if (engine_reset) {
144 if (!e->keep_on_clear) {
145 b->es = NULL;
146 done_engine(e);
147 e = init_engine(engine, e_arg, b);
148 ti[S_BLACK] = ti_default[S_BLACK];
149 ti[S_WHITE] = ti_default[S_WHITE];
151 engine_reset = false;
154 done_engine(e);
155 return 0;