can_countercapture(): Export interface
[pachi/pachi-r6144.git] / joseki / base.c
blob2cc6ef8899b993a14ee9b2b76b025e7687bf8cd4
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 char fname[1024];
25 snprintf(fname, 1024, "joseki%d.pdict", bsize - 2);
26 FILE *f = fopen(fname, "r");
27 if (!f) {
28 if (DEBUGL(3))
29 perror(fname);
30 return NULL;
32 struct joseki_dict *jd = joseki_init(bsize);
34 char linebuf[1024];
35 while (fgets(linebuf, 1024, f)) {
36 char *line = linebuf;
38 while (isspace(*line)) line++;
39 if (*line == '#')
40 continue;
41 hash_t h = strtoull(line, &line, 16);
42 while (isspace(*line)) line++;
43 enum stone color = *line++ == 'b' ? S_BLACK : S_WHITE;
44 while (isspace(*line)) line++;
46 /* Get count. */
47 char *cs = strrchr(line, ' '); assert(cs);
48 *cs++ = 0;
49 int count = atoi(cs);
51 coord_t **ccp = &jd->patterns[h].moves[color - 1];
52 assert(!*ccp);
53 *ccp = calloc2(count + 1, sizeof(coord_t));
54 coord_t *cc = *ccp;
55 while (*line) {
56 assert(cc - *ccp < count);
57 coord_t *c = str2coord(line, bsize);
58 *cc++ = *c;
59 coord_done(c);
60 line += strcspn(line, " ");
61 line += strspn(line, " ");
63 *cc = pass;
66 fclose(f);
67 if (DEBUGL(2))
68 fprintf(stderr, "Joseki dictionary for board size %d loaded.\n", bsize - 2);
69 return jd;
72 void
73 joseki_done(struct joseki_dict *jd)
75 if (!jd) return;
76 free(jd->patterns);
77 free(jd);