14 #include "move_legal.h"
\r
31 static FILE * BookFile;
\r
32 static int BookSize;
\r
36 static int find_pos (uint64 key);
\r
38 static void read_entry (entry_t * entry, int n);
\r
39 static void write_entry (const entry_t * entry, int n);
\r
41 static uint64 read_integer (FILE * file, int size);
\r
42 static void write_integer (FILE * file, int size, uint64 n);
\r
54 bool book_is_open(){
\r
55 return BookFile!=NULL;
\r
60 void book_open(const char file_name[]) {
\r
62 ASSERT(file_name!=NULL);
\r
63 if(option_get_bool("BookLearn")){
\r
64 BookFile = fopen(file_name,"rb+");
\r
66 BookFile = fopen(file_name,"rb");
\r
69 // if (BookFile == NULL) my_fatal("book_open(): can't open file \"%s\": %s\n",file_name,strerror(errno));
\r
70 if (BookFile == NULL) return;
\r
72 if (fseek(BookFile,0,SEEK_END) == -1) {
\r
73 my_fatal("book_open(): fseek(): %s\n",strerror(errno));
\r
76 BookSize = ftell(BookFile) / 16;
\r
77 // if (BookSize == 0) my_fatal("book_open(): empty file\n");
\r
78 if (BookSize == 0) {
\r
88 if(BookFile==NULL) return;
\r
90 if (fclose(BookFile) == EOF) {
\r
91 my_fatal("book_close(): fclose(): %s\n",strerror(errno));
\r
97 bool is_in_book(const board_t * board) {
\r
102 if(BookFile==NULL) return false;
\r
104 ASSERT(board!=NULL);
\r
106 for (pos = find_pos(board->key); pos < BookSize; pos++) {
\r
107 read_entry(entry,pos);
\r
108 if (entry->key == board->key) return true;
\r
116 int book_move(const board_t * board, bool random) {
\r
125 if(BookFile==NULL) return MoveNone;
\r
127 ASSERT(board!=NULL);
\r
128 ASSERT(random==true||random==false);
\r
131 best_move = MoveNone;
\r
133 for (pos = find_pos(board->key); pos < BookSize; pos++) {
\r
135 read_entry(entry,pos);
\r
136 if (entry->key != board->key) break;
\r
138 move = entry->move;
\r
139 score = entry->count;
\r
141 if (move != MoveNone && move_is_legal(move,board)) {
\r
148 best_score += score;
\r
149 if (my_random_int(best_score) < score) best_move = move;
\r
151 if (score > best_score) {
\r
153 best_score = score;
\r
168 void book_disp(const board_t * board) {
\r
176 char move_string[256];
\r
178 ASSERT(board!=NULL);
\r
180 if(BookFile==NULL) return;
\r
182 first_pos = find_pos(board->key);
\r
188 for (pos = first_pos; pos < BookSize; pos++) {
\r
190 read_entry(entry,pos);
\r
191 if (entry->key != board->key) break;
\r
193 sum += entry->count;
\r
198 for (pos = first_pos; pos < BookSize; pos++) {
\r
200 read_entry(entry,pos);
\r
201 if (entry->key != board->key) break;
\r
203 move = entry->move;
\r
204 score = entry->count;
\r
206 if (score > 0 && move != MoveNone && move_is_legal(move,board)) {
\r
207 move_to_san(move,board,move_string,256);
\r
208 printf(" %s (%.0f%%)\n",move_string,(double(score)/double(sum))*100.0);
\r
215 // book_learn_move()
\r
217 void book_learn_move(const board_t * board, int move, int result) {
\r
222 if(BookFile==NULL) return;
\r
224 ASSERT(board!=NULL);
\r
225 ASSERT(move_is_ok(move));
\r
226 ASSERT(result>=-1&&result<=+1);
\r
228 ASSERT(move_is_legal(move,board));
\r
230 for (pos = find_pos(board->key); pos < BookSize; pos++) {
\r
232 read_entry(entry,pos);
\r
233 if (entry->key != board->key) break;
\r
235 if (entry->move == move) {
\r
238 entry->sum += result+1;
\r
240 write_entry(entry,pos);
\r
249 void book_flush() {
\r
251 if(BookFile==NULL) return;
\r
253 if (fflush(BookFile) == EOF) {
\r
254 my_fatal("book_flush(): fflush(): %s\n",strerror(errno));
\r
260 static int find_pos(uint64 key) {
\r
262 int left, right, mid;
\r
265 // binary search (finds the leftmost entry)
\r
268 right = BookSize-1;
\r
270 ASSERT(left<=right);
\r
272 while (left < right) {
\r
274 mid = (left + right) / 2;
\r
275 ASSERT(mid>=left&&mid<right);
\r
277 read_entry(entry,mid);
\r
279 if (key <= entry->key) {
\r
286 ASSERT(left==right);
\r
288 read_entry(entry,left);
\r
290 return (entry->key == key) ? left : BookSize;
\r
295 static void read_entry(entry_t * entry, int n) {
\r
297 ASSERT(entry!=NULL);
\r
298 ASSERT(n>=0&&n<BookSize);
\r
300 if (fseek(BookFile,n*16,SEEK_SET) == -1) {
\r
301 my_fatal("read_entry(): fseek(): %s\n",strerror(errno));
\r
304 entry->key = read_integer(BookFile,8);
\r
305 entry->move = read_integer(BookFile,2);
\r
306 entry->count = read_integer(BookFile,2);
\r
307 entry->n = read_integer(BookFile,2);
\r
308 entry->sum = read_integer(BookFile,2);
\r
313 static void write_entry(const entry_t * entry, int n) {
\r
315 ASSERT(entry!=NULL);
\r
316 ASSERT(n>=0&&n<BookSize);
\r
318 if (fseek(BookFile,n*16,SEEK_SET) == -1) {
\r
319 my_fatal("write_entry(): fseek(): %s\n",strerror(errno));
\r
322 write_integer(BookFile,8,entry->key);
\r
323 write_integer(BookFile,2,entry->move);
\r
324 write_integer(BookFile,2,entry->count);
\r
325 write_integer(BookFile,2,entry->n);
\r
326 write_integer(BookFile,2,entry->sum);
\r
331 static uint64 read_integer(FILE * file, int size) {
\r
337 ASSERT(file!=NULL);
\r
338 ASSERT(size>0&&size<=8);
\r
342 for (i = 0; i < size; i++) {
\r
348 my_fatal("read_integer(): fgetc(): EOF reached\n");
\r
350 my_fatal("read_integer(): fgetc(): %s\n",strerror(errno));
\r
354 ASSERT(b>=0&&b<256);
\r
363 static void write_integer(FILE * file, int size, uint64 n) {
\r
368 ASSERT(file!=NULL);
\r
369 ASSERT(size>0&&size<=8);
\r
370 ASSERT(size==8||n>>(size*8)==0);
\r
372 for (i = size-1; i >= 0; i--) {
\r
374 b = (n >> (i*8)) & 0xFF;
\r
375 ASSERT(b>=0&&b<256);
\r
377 if (fputc(b,file) == EOF) {
\r
378 my_fatal("write_integer(): fputc(): %s\n",strerror(errno));
\r