playout_elo_choose(): Pass when no moves remain in the probdist
[pachi/t.git] / zzgo.c
blob611535c3458ed19d65288e79daefc47ace9fa18c
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 = 3;
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; if specified,
99 * GTP time information is ignored. Useful
100 * e.g. when you want to force your bot to
101 * play weaker while giving the opponent
102 * reasonable time to play, or force play
103 * by number of simulations in timed games. */
104 /* Please see timeinfo.h:time_parse()
105 * description for syntax details. */
106 if (!time_parse(&ti_default, optarg)) {
107 fprintf(stderr, "%s: Invalid -t argument %s\n", argv[0], optarg);
108 exit(1);
110 ti_default.ignore_gtp = true;
111 assert(ti_default.period != TT_NULL);
112 break;
113 case 'u':
114 testfile = strdup(optarg);
115 break;
116 default: /* '?' */
117 fprintf(stderr, "Pachi version %s\n", PACHI_VERSION);
118 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",
119 argv[0]);
120 exit(1);
124 fast_srandom(seed);
125 if (DEBUGL(0))
126 fprintf(stderr, "Random seed: %d\n", seed);
128 struct board *b = board_init();
129 struct time_info ti[S_MAX];
130 ti[S_BLACK] = ti_default;
131 ti[S_WHITE] = ti_default;
133 char *e_arg = NULL;
134 if (optind < argc)
135 e_arg = argv[optind];
136 struct engine *e = init_engine(engine, e_arg, b);
138 if (testfile) {
139 unittest(testfile);
140 return 0;
143 char buf[4096];
144 while (fgets(buf, 4096, stdin)) {
145 if (DEBUGL(1))
146 fprintf(stderr, "IN: %s", buf);
147 gtp_parse(b, e, ti, buf);
148 if (engine_reset) {
149 if (!e->keep_on_clear) {
150 b->es = NULL;
151 done_engine(e);
152 e = init_engine(engine, e_arg, b);
153 ti[S_BLACK] = ti_default;
154 ti[S_WHITE] = ti_default;
156 engine_reset = false;
159 done_engine(e);
160 return 0;