14 #include "move_legal.h"
31 static FILE * BookFile;
36 static int find_pos (uint64 key);
38 static void read_entry (entry_t * entry, int n);
39 static void write_entry (const entry_t * entry, int n);
41 static uint64 read_integer (FILE * file, int size);
42 static void write_integer (FILE * file, int size, uint64 n);
55 return BookFile!=NULL;
60 void book_open(const char file_name[]) {
62 ASSERT(file_name!=NULL);
63 if(FALSE && option_get_bool(Option,"BookLearn")){
64 BookFile = fopen(file_name,"rb+");
66 BookFile = fopen(file_name,"rb");
69 // if (BookFile == NULL) my_fatal("book_open(): can't open file \"%s\": %s\n",file_name,strerror(errno));
70 if (BookFile == NULL) return;
72 if (fseek(BookFile,0,SEEK_END) == -1) {
73 my_fatal("book_open(): fseek(): %s\n",strerror(errno));
76 BookSize = ftell(BookFile) / 16;
77 // if (BookSize == 0) my_fatal("book_open(): empty file\n");
88 if(BookFile==NULL) return;
90 if (fclose(BookFile) == EOF) {
91 my_fatal("book_close(): fclose(): %s\n",strerror(errno));
97 bool is_in_book(const board_t * board) {
102 if(BookFile==NULL) return FALSE;
106 for (pos = find_pos(board->key); pos < BookSize; pos++) {
107 read_entry(entry,pos);
108 if (entry->key == board->key) return TRUE;
116 int book_move(const board_t * board, bool random) {
127 if(BookFile==NULL) return MoveNone;
130 ASSERT(random==TRUE||random==FALSE);
136 book_moves(list,board);
138 best_move = MoveNone;
140 for(i=0; i<list_size(list); i++){
142 move = list->move[i];
143 score = list->value[i];
145 if (move != MoveNone &&
146 move_is_legal(move,board) &&
147 score>10*option_get_int(Option,"BookTreshold")) {
155 if (my_random_int(best_score) < score) best_move = move;
157 if (score > best_score) {
174 void book_moves(list_t * list, const board_t * board) {
182 char move_string[256];
187 if(BookFile==NULL) return;
193 first_pos = find_pos(board->key);
199 for (pos = first_pos; pos < BookSize; pos++) {
201 read_entry(entry,pos);
202 if (entry->key != board->key) break;
209 for (pos = first_pos; pos < BookSize; pos++) {
211 read_entry(entry,pos);
212 if (entry->key != board->key) break;
215 score = (((uint32)entry->count)*((uint32)10000))/sum; // 32 bit safe!
217 if (move != MoveNone && move_is_legal(move,board)) {
218 list_add_ex(list,move,score);
227 void book_disp(const board_t * board) {
229 char move_string[256];
232 int treshold=option_get_int(Option,"BookTreshold");
236 if(BookFile==NULL) return;
238 book_moves(list,board);
240 for(i=0; i<list_size(list); i++){
241 move_to_san(list->move[i],board,move_string,256);
242 if(list->value[i]>10*treshold){
243 printf(" %6s %5.2f%%\n",move_string,list->value[i]/100.0);
245 printf(" %6s %5.2f%% (below treshold %4.2f%%)\n",
246 move_string,list->value[i]/100.0,treshold/10.0);
249 // this is necessary by the xboard protocol
255 void book_learn_move(const board_t * board, int move, int result) {
260 if(BookFile==NULL) return;
263 ASSERT(move_is_ok(move));
264 ASSERT(result>=-1&&result<=+1);
266 ASSERT(move_is_legal(move,board));
268 for (pos = find_pos(board->key); pos < BookSize; pos++) {
270 read_entry(entry,pos);
271 if (entry->key != board->key) break;
273 if (entry->move == move) {
276 entry->sum += result+1;
278 write_entry(entry,pos);
289 if(BookFile==NULL) return;
291 if (fflush(BookFile) == EOF) {
292 my_fatal("book_flush(): fflush(): %s\n",strerror(errno));
298 static int find_pos(uint64 key) {
300 int left, right, mid;
303 // binary search (finds the leftmost entry)
310 while (left < right) {
312 mid = (left + right) / 2;
313 ASSERT(mid>=left&&mid<right);
315 read_entry(entry,mid);
317 if (key <= entry->key) {
326 read_entry(entry,left);
328 return (entry->key == key) ? left : BookSize;
333 static void read_entry(entry_t * entry, int n) {
336 ASSERT(n>=0&&n<BookSize);
338 if (fseek(BookFile,n*16,SEEK_SET) == -1) {
339 my_fatal("read_entry(): fseek(): %s\n",strerror(errno));
342 entry->key = read_integer(BookFile,8);
343 entry->move = read_integer(BookFile,2);
344 entry->count = read_integer(BookFile,2);
345 entry->n = read_integer(BookFile,2);
346 entry->sum = read_integer(BookFile,2);
351 static void write_entry(const entry_t * entry, int n) {
354 ASSERT(n>=0&&n<BookSize);
356 if (fseek(BookFile,n*16,SEEK_SET) == -1) {
357 my_fatal("write_entry(): fseek(): %s\n",strerror(errno));
360 write_integer(BookFile,8,entry->key);
361 write_integer(BookFile,2,entry->move);
362 write_integer(BookFile,2,entry->count);
363 write_integer(BookFile,2,entry->n);
364 write_integer(BookFile,2,entry->sum);
369 static uint64 read_integer(FILE * file, int size) {
376 ASSERT(size>0&&size<=8);
380 for (i = 0; i < size; i++) {
386 my_fatal("read_integer(): fgetc(): EOF reached\n");
388 my_fatal("read_integer(): fgetc(): %s\n",strerror(errno));
401 static void write_integer(FILE * file, int size, uint64 n) {
407 ASSERT(size>0&&size<=8);
408 ASSERT(size==8||n>>(size*8)==0);
410 for (i = size-1; i >= 0; i--) {
412 b = (n >> (i*8)) & 0xFF;
415 if (fputc(b,file) == EOF) {
416 my_fatal("write_integer(): fputc(): %s\n",strerror(errno));