Joseki: Remove global jdict instance
[pachi/nmclean.git] / joseki / base.c
blobe84ad0a761befe94fff87b48465fa563be754304
1 #include <assert.h>
2 #include <stdio.h>
3 #include <stdlib.h>
5 #define DEBUG
6 #include "board.h"
7 #include "debug.h"
8 #include "move.h"
9 #include "joseki/base.h"
12 struct joseki_dict *
13 joseki_init(int bsize)
15 struct joseki_dict *jd = calloc(1, sizeof(*jd));
16 jd->bsize = bsize;
17 jd->patterns = calloc(1 << joseki_hash_bits, sizeof(jd->patterns[0]));
18 return jd;
21 struct joseki_dict *
22 joseki_load(int bsize)
24 FILE *f = fopen("pachijoseki.dat", "r"); // XXX: size-dependent
25 if (!f) {
26 if (DEBUGL(3))
27 perror("pachijoseki.dat");
28 return NULL;
30 struct joseki_dict *jd = joseki_init(bsize);
32 char linebuf[1024];
33 while (fgets(linebuf, 1024, f)) {
34 char *line = linebuf;
36 hash_t h = strtoull(line, &line, 16);
37 while (isspace(*line)) line++;
38 enum stone color = *line++ == 'b' ? S_BLACK : S_WHITE;
39 while (isspace(*line)) line++;
41 /* Get count. */
42 char *cs = strrchr(line, ' '); assert(cs);
43 *cs++ = 0;
44 int count = atoi(cs);
46 coord_t **ccp = &jd->patterns[h].moves[color - 1];
47 assert(!*ccp);
48 *ccp = calloc2(count + 1, sizeof(coord_t));
49 coord_t *cc = *ccp;
50 while (*line) {
51 assert(cc - *ccp < count);
52 coord_t *c = str2coord(line, bsize);
53 *cc++ = *c;
54 coord_done(c);
55 line += strcspn(line, " ");
56 line += strspn(line, " ");
58 *cc = pass;
61 fclose(f);
62 if (DEBUGL(2))
63 fprintf(stderr, "Joseki dictionary for board size %d loaded.\n", bsize - 2);
64 return jd;
67 void
68 joseki_done(struct joseki_dict *jd)
70 free(jd->patterns);
71 free(jd);