Moggy: Remove old, dusty and ineffective assess_local
[pachi.git] / joseki / base.c
bloba48d796880db9303ab6a982cab42c10518217512
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 hash_t h = strtoull(line, &line, 16);
39 while (isspace(*line)) line++;
40 enum stone color = *line++ == 'b' ? S_BLACK : S_WHITE;
41 while (isspace(*line)) line++;
43 /* Get count. */
44 char *cs = strrchr(line, ' '); assert(cs);
45 *cs++ = 0;
46 int count = atoi(cs);
48 coord_t **ccp = &jd->patterns[h].moves[color - 1];
49 assert(!*ccp);
50 *ccp = calloc2(count + 1, sizeof(coord_t));
51 coord_t *cc = *ccp;
52 while (*line) {
53 assert(cc - *ccp < count);
54 coord_t *c = str2coord(line, bsize);
55 *cc++ = *c;
56 coord_done(c);
57 line += strcspn(line, " ");
58 line += strspn(line, " ");
60 *cc = pass;
63 fclose(f);
64 if (DEBUGL(2))
65 fprintf(stderr, "Joseki dictionary for board size %d loaded.\n", bsize - 2);
66 return jd;
69 void
70 joseki_done(struct joseki_dict *jd)
72 if (!jd) return;
73 free(jd->patterns);
74 free(jd);