Add board_estimated_moves_left() and MIN_MOVES_LEFT, will be needed for time control.
[pachi/derm.git] / zzgo.c
blobda587553846773200013d58574b041fdacb5a592
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 struct time_info ti_default = { .period = TT_NULL };
68 char *testfile = NULL;
70 seed = time(NULL) ^ getpid();
72 int opt;
73 while ((opt = getopt(argc, argv, "e:d:s:t:u:")) != -1) {
74 switch (opt) {
75 case 'e':
76 if (!strcasecmp(optarg, "random")) {
77 engine = E_RANDOM;
78 } else if (!strcasecmp(optarg, "patternscan")) {
79 engine = E_PATTERNSCAN;
80 } else if (!strcasecmp(optarg, "replay")) {
81 engine = E_REPLAY;
82 } else if (!strcasecmp(optarg, "montecarlo")) {
83 engine = E_MONTECARLO;
84 } else if (!strcasecmp(optarg, "uct")) {
85 engine = E_UCT;
86 } else {
87 fprintf(stderr, "%s: Invalid -e argument %s\n", argv[0], optarg);
88 exit(1);
90 break;
91 case 'd':
92 debug_level = atoi(optarg);
93 break;
94 case 's':
95 seed = atoi(optarg);
96 break;
97 case 't':
98 /* Time settings to follow by default (if no
99 * time_left GTP commands are received). Please
100 * see timeinfo.h:time_parse() description for
101 * syntax details. */
102 if (!time_parse(&ti_default, optarg)) {
103 fprintf(stderr, "%s: Invalid -t argument %s\n", argv[0], optarg);
104 exit(1);
106 break;
107 case 'u':
108 testfile = strdup(optarg);
109 break;
110 default: /* '?' */
111 fprintf(stderr, "Pachi version %s\n", PACHI_VERSION);
112 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",
113 argv[0]);
114 exit(1);
118 fast_srandom(seed);
119 fprintf(stderr, "Random seed: %d\n", seed);
121 struct board *b = board_init();
122 struct time_info ti = ti_default;
124 char *e_arg = NULL;
125 if (optind < argc)
126 e_arg = argv[optind];
127 struct engine *e = init_engine(engine, e_arg, b);
129 if (testfile) {
130 unittest(testfile);
131 return 0;
134 char buf[4096];
135 while (fgets(buf, 4096, stdin)) {
136 if (DEBUGL(1))
137 fprintf(stderr, "IN: %s", buf);
138 gtp_parse(b, e, &ti, buf);
139 if (engine_reset) {
140 if (!e->keep_on_clear) {
141 b->es = NULL;
142 done_engine(e);
143 e = init_engine(engine, e_arg, b);
144 ti = ti_default;
146 engine_reset = false;
149 done_engine(e);
150 return 0;