13 coord_transform(struct board
*b
, coord_t coord
, int i
)
15 #define HASH_VMIRROR 1
16 #define HASH_HMIRROR 2
18 if (i
& HASH_VMIRROR
) {
19 coord
= coord_xy(b
, coord_x(coord
, b
), board_size(b
) - 1 - coord_y(coord
, b
));
21 if (i
& HASH_HMIRROR
) {
22 coord
= coord_xy(b
, board_size(b
) - 1 - coord_x(coord
, b
), coord_y(coord
, b
));
24 if (i
& HASH_XYFLIP
) {
25 coord
= coord_xy(b
, coord_y(coord
, b
), coord_x(coord
, b
));
30 /* Check if we can make a move along the fbook right away.
31 * Otherwise return pass. */
33 fbook_check(struct board
*board
)
35 if (!board
->fbook
) return pass
;
37 hash_t hi
= board
->hash
;
39 while (!is_pass(board
->fbook
->moves
[hi
& fbook_hash_mask
])) {
40 if (board
->fbook
->hashes
[hi
& fbook_hash_mask
] == board
->hash
) {
41 cf
= board
->fbook
->moves
[hi
& fbook_hash_mask
];
48 fprintf(stderr
, "fbook match %"PRIhash
":%"PRIhash
"\n", board
->hash
, board
->hash
& fbook_hash_mask
);
50 /* No match, also prevent further fbook usage
51 * until the next clear_board. */
53 fprintf(stderr
, "fbook out %"PRIhash
":%"PRIhash
"\n", board
->hash
, board
->hash
& fbook_hash_mask
);
54 fbook_done(board
->fbook
);
61 fbook_init(char *filename
, struct board
*b
)
63 FILE *f
= fopen(filename
, "r");
69 struct fbook
*fbook
= calloc(1, sizeof(*fbook
));
70 fbook
->bsize
= board_size(b
);
71 fbook
->handicap
= b
->handicap
;
72 for (int i
= 0; i
< 1<<fbook_hash_bits
; i
++)
73 fbook
->moves
[i
] = pass
;
76 fprintf(stderr
, "Loading opening fbook %s...\n", filename
);
78 /* Scratch board where we lay out the sequence;
79 * one for each transposition. */
81 for (int i
= 0; i
< 8; i
++) {
82 bs
[i
] = board_init(NULL
);
83 board_resize(bs
[i
], fbook
->bsize
- 2);
87 while (fgets(linebuf
, sizeof(linebuf
), f
)) {
89 linebuf
[strlen(linebuf
) - 1] = 0; // chop
92 * BSIZE COORD COORD COORD... | COORD
93 * We descend up to |, then add the new node
94 * with value minimax(1000), forcing UCT to
95 * always pick that node immediately. */
96 int bsize
= strtol(line
, &line
, 10);
97 if (bsize
!= fbook
->bsize
- 2)
99 while (isspace(*line
)) line
++;
101 for (int i
= 0; i
< 8; i
++) {
103 bs
[i
]->last_move
.color
= S_WHITE
;
106 while (*line
!= '|') {
107 coord_t
*c
= str2coord(line
, fbook
->bsize
);
109 for (int i
= 0; i
< 8; i
++) {
110 coord_t coord
= coord_transform(b
, *c
, i
);
111 struct move m
= { .coord
= coord
, .color
= stone_other(bs
[i
]->last_move
.color
) };
112 int ret
= board_play(bs
[i
], &m
);
117 while (!isspace(*line
)) line
++;
118 while (isspace(*line
)) line
++;
122 while (isspace(*line
)) line
++;
124 /* In case of multiple candidates, pick one with
125 * exponentially decreasing likelihood. */
126 while (strchr(line
, ' ') && fast_random(2)) {
127 line
= strchr(line
, ' ');
128 while (isspace(*line
)) line
++;
129 // fprintf(stderr, "<%s> skip to %s\n", linebuf, line);
132 coord_t
*c
= str2coord(line
, fbook
->bsize
);
133 for (int i
= 0; i
< 8; i
++) {
134 coord_t coord
= coord_transform(b
, *c
, i
);
136 char conflict
= is_pass(fbook
->moves
[bs
[i
]->hash
& fbook_hash_mask
]) ? '+' : 'C';
138 for (int j
= 0; j
< i
; j
++)
139 if (bs
[i
]->hash
== bs
[j
]->hash
)
141 if (conflict
== 'C') {
142 hash_t hi
= bs
[i
]->hash
;
143 while (!is_pass(fbook
->moves
[hi
& fbook_hash_mask
]) && fbook
->hashes
[hi
& fbook_hash_mask
] != bs
[i
]->hash
)
145 if (fbook
->hashes
[hi
& fbook_hash_mask
] == bs
[i
]->hash
)
148 fprintf(stderr
, "%c %"PRIhash
":%"PRIhash
" (<%d> %s)\n", conflict
,
149 bs
[i
]->hash
& fbook_hash_mask
, bs
[i
]->hash
, i
, linebuf
);
151 hash_t hi
= bs
[i
]->hash
;
152 while (!is_pass(fbook
->moves
[hi
& fbook_hash_mask
]) && fbook
->hashes
[hi
& fbook_hash_mask
] != bs
[i
]->hash
)
154 fbook
->moves
[hi
& fbook_hash_mask
] = coord
;
155 fbook
->hashes
[hi
& fbook_hash_mask
] = bs
[i
]->hash
;
160 for (int i
= 0; i
< 8; i
++) {
169 void fbook_done(struct fbook
*fbook
)