Spatials: Fix constructor execution order - we need ptcords first, pthashes later
[pachi.git] / zzgo.c
blobbe216809562327b4fa514004a9ccfc164459e042
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>
9 #include "board.h"
10 #include "debug.h"
11 #include "engine.h"
12 #include "replay/replay.h"
13 #include "montecarlo/montecarlo.h"
14 #include "random/random.h"
15 #include "patternscan/patternscan.h"
16 #include "t-unit/test.h"
17 #include "uct/uct.h"
18 #include "gtp.h"
19 #include "random.h"
20 #include "version.h"
22 int debug_level = 1;
23 int seed;
26 enum engine_id {
27 E_RANDOM,
28 E_REPLAY,
29 E_PATTERNSCAN,
30 E_MONTECARLO,
31 E_UCT,
32 E_MAX,
35 static struct engine *(*engine_init[E_MAX])(char *arg, struct board *b) = {
36 engine_random_init,
37 engine_replay_init,
38 engine_patternscan_init,
39 engine_montecarlo_init,
40 engine_uct_init,
43 static struct engine *init_engine(enum engine_id engine, char *e_arg, struct board *b)
45 char *arg = e_arg? strdup(e_arg) : e_arg;
46 assert(engine < E_MAX);
47 struct engine *e = engine_init[engine](arg, b);
48 if (arg) free(arg);
49 return e;
52 static void done_engine(struct engine *e)
54 if (e->done) e->done(e);
55 if (e->data) free(e->data);
56 free(e);
59 bool engine_reset = false;
62 int main(int argc, char *argv[])
64 enum engine_id engine = E_UCT;
65 char *testfile = NULL;
67 seed = time(NULL);
69 int opt;
70 while ((opt = getopt(argc, argv, "e:d:s:t:")) != -1) {
71 switch (opt) {
72 case 'e':
73 if (!strcasecmp(optarg, "random")) {
74 engine = E_RANDOM;
75 } else if (!strcasecmp(optarg, "patternscan")) {
76 engine = E_PATTERNSCAN;
77 } else if (!strcasecmp(optarg, "replay")) {
78 engine = E_REPLAY;
79 } else if (!strcasecmp(optarg, "montecarlo")) {
80 engine = E_MONTECARLO;
81 } else if (!strcasecmp(optarg, "uct")) {
82 engine = E_UCT;
83 } else {
84 fprintf(stderr, "%s: Invalid -e argument %s\n", argv[0], optarg);
85 exit(1);
87 break;
88 case 'd':
89 debug_level = atoi(optarg);
90 break;
91 case 's':
92 seed = atoi(optarg);
93 break;
94 case 't':
95 testfile = strdup(optarg);
96 break;
97 default: /* '?' */
98 fprintf(stderr, "Pachi version %s\n", PACHI_VERSION);
99 fprintf(stderr, "Usage: %s [-e random|replay|patternscan|montecarlo|uct] [-d DEBUG_LEVEL] [-s RANDOM_SEED] [-t FILENAME] [ENGINE_ARGS]\n",
100 argv[0]);
101 exit(1);
105 fast_srandom(seed);
106 fprintf(stderr, "Random seed: %d\n", seed);
108 struct board *b = board_init();
110 char *e_arg = NULL;
111 if (optind < argc)
112 e_arg = argv[optind];
113 struct engine *e = init_engine(engine, e_arg, b);
115 if (testfile) {
116 unittest(testfile);
117 return 0;
120 char buf[4096];
121 while (fgets(buf, 4096, stdin)) {
122 if (DEBUGL(1))
123 fprintf(stderr, "IN: %s", buf);
124 gtp_parse(b, e, buf);
125 if (engine_reset) {
126 if (!e->keep_on_clear) {
127 b->es = NULL;
128 done_engine(e);
129 e = init_engine(engine, e_arg, b);
131 engine_reset = false;
134 done_engine(e);
135 return 0;