15 coord_transform(struct board
*b
, coord_t coord
, int i
)
17 #define HASH_VMIRROR 1
18 #define HASH_HMIRROR 2
20 if (i
& HASH_VMIRROR
) {
21 coord
= coord_xy(b
, coord_x(coord
, b
), board_size(b
) - 1 - coord_y(coord
, b
));
23 if (i
& HASH_HMIRROR
) {
24 coord
= coord_xy(b
, board_size(b
) - 1 - coord_x(coord
, b
), coord_y(coord
, b
));
26 if (i
& HASH_XYFLIP
) {
27 coord
= coord_xy(b
, coord_y(coord
, b
), coord_x(coord
, b
));
32 /* Check if we can make a move along the fbook right away.
33 * Otherwise return pass. */
35 fbook_check(struct board
*board
)
37 if (!board
->fbook
) return pass
;
39 hash_t hi
= board
->hash
;
41 while (!is_pass(board
->fbook
->moves
[hi
& fbook_hash_mask
])) {
42 if (board
->fbook
->hashes
[hi
& fbook_hash_mask
] == board
->hash
) {
43 cf
= board
->fbook
->moves
[hi
& fbook_hash_mask
];
50 fprintf(stderr
, "fbook match %"PRIhash
":%"PRIhash
"\n", board
->hash
, board
->hash
& fbook_hash_mask
);
52 /* No match, also prevent further fbook usage
53 * until the next clear_board. */
55 fprintf(stderr
, "fbook out %"PRIhash
":%"PRIhash
"\n", board
->hash
, board
->hash
& fbook_hash_mask
);
56 fbook_done(board
->fbook
);
62 static struct fbook
*fbcache
;
65 fbook_init(char *filename
, struct board
*b
)
67 if (fbcache
&& fbcache
->bsize
== board_size(b
)
68 && fbcache
->handicap
== b
->handicap
)
71 FILE *f
= fopen(filename
, "r");
77 struct fbook
*fbook
= calloc(1, sizeof(*fbook
));
78 fbook
->bsize
= board_size(b
);
79 fbook
->handicap
= b
->handicap
;
80 /* We do not set handicap=1 in case of too low komi on purpose;
81 * we want to go with the no-handicap fbook for now. */
82 for (int i
= 0; i
< 1<<fbook_hash_bits
; i
++)
83 fbook
->moves
[i
] = pass
;
86 fprintf(stderr
, "Loading opening fbook %s...\n", filename
);
88 /* Scratch board where we lay out the sequence;
89 * one for each transposition. */
91 for (int i
= 0; i
< 8; i
++) {
92 bs
[i
] = board_init(NULL
);
93 board_resize(bs
[i
], fbook
->bsize
- 2);
97 while (fgets(linebuf
, sizeof(linebuf
), f
)) {
99 linebuf
[strlen(linebuf
) - 1] = 0; // chop
101 /* Format of line is:
102 * BSIZE COORD COORD COORD... | COORD
103 * BSIZE/HANDI COORD COORD COORD... | COORD */
104 int bsize
= strtol(line
, &line
, 10);
105 if (bsize
!= fbook
->bsize
- 2)
110 handi
= strtol(line
, &line
, 10);
112 if (handi
!= fbook
->handicap
)
114 while (isspace(*line
)) line
++;
116 for (int i
= 0; i
< 8; i
++) {
118 bs
[i
]->last_move
.color
= S_WHITE
;
121 while (*line
!= '|') {
122 coord_t
*c
= str2coord(line
, fbook
->bsize
);
124 for (int i
= 0; i
< 8; i
++) {
125 coord_t coord
= coord_transform(b
, *c
, i
);
126 struct move m
= { .coord
= coord
, .color
= stone_other(bs
[i
]->last_move
.color
) };
127 int ret
= board_play(bs
[i
], &m
);
132 while (!isspace(*line
)) line
++;
133 while (isspace(*line
)) line
++;
137 while (isspace(*line
)) line
++;
139 /* In case of multiple candidates, pick one with
140 * exponentially decreasing likelihood. */
141 while (strchr(line
, ' ') && fast_random(2)) {
142 line
= strchr(line
, ' ');
143 while (isspace(*line
)) line
++;
144 // fprintf(stderr, "<%s> skip to %s\n", linebuf, line);
147 coord_t
*c
= str2coord(line
, fbook
->bsize
);
148 for (int i
= 0; i
< 8; i
++) {
149 coord_t coord
= coord_transform(b
, *c
, i
);
151 char conflict
= is_pass(fbook
->moves
[bs
[i
]->hash
& fbook_hash_mask
]) ? '+' : 'C';
153 for (int j
= 0; j
< i
; j
++)
154 if (bs
[i
]->hash
== bs
[j
]->hash
)
156 if (conflict
== 'C') {
157 hash_t hi
= bs
[i
]->hash
;
158 while (!is_pass(fbook
->moves
[hi
& fbook_hash_mask
]) && fbook
->hashes
[hi
& fbook_hash_mask
] != bs
[i
]->hash
)
160 if (fbook
->hashes
[hi
& fbook_hash_mask
] == bs
[i
]->hash
)
163 fprintf(stderr
, "%c %"PRIhash
":%"PRIhash
" (<%d> %s)\n", conflict
,
164 bs
[i
]->hash
& fbook_hash_mask
, bs
[i
]->hash
, i
, linebuf
);
166 hash_t hi
= bs
[i
]->hash
;
167 while (!is_pass(fbook
->moves
[hi
& fbook_hash_mask
]) && fbook
->hashes
[hi
& fbook_hash_mask
] != bs
[i
]->hash
)
169 fbook
->moves
[hi
& fbook_hash_mask
] = coord
;
170 fbook
->hashes
[hi
& fbook_hash_mask
] = bs
[i
]->hash
;
176 for (int i
= 0; i
< 8; i
++) {
182 if (!fbook
->movecnt
) {
183 /* Empty book is not worth the hassle. */
188 struct fbook
*fbold
= fbcache
;
196 void fbook_done(struct fbook
*fbook
)
198 if (fbook
!= fbcache
)