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(Option,"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
127 if(BookFile==NULL) return MoveNone;
\r
129 ASSERT(board!=NULL);
\r
130 ASSERT(random==TRUE||random==FALSE);
\r
136 book_moves(list,board);
\r
138 best_move = MoveNone;
\r
140 for(i=0; i<list_size(list); i++){
\r
142 move = list->move[i];
\r
143 score = list->value[i];
\r
145 if (move != MoveNone &&
\r
146 move_is_legal(move,board) &&
\r
147 score>10*option_get_int(Option,"BookTreshold")) {
\r
154 best_score += score;
\r
155 if (my_random_int(best_score) < score) best_move = move;
\r
157 if (score > best_score) {
\r
159 best_score = score;
\r
174 void book_moves(list_t * list, const board_t * board) {
\r
182 char move_string[256];
\r
184 ASSERT(board!=NULL);
\r
185 ASSERT(list!=NULL);
\r
187 if(BookFile==NULL) return;
\r
193 first_pos = find_pos(board->key);
\r
199 for (pos = first_pos; pos < BookSize; pos++) {
\r
201 read_entry(entry,pos);
\r
202 if (entry->key != board->key) break;
\r
204 sum += entry->count;
\r
209 for (pos = first_pos; pos < BookSize; pos++) {
\r
211 read_entry(entry,pos);
\r
212 if (entry->key != board->key) break;
\r
214 move = entry->move;
\r
215 score = (((uint32)entry->count)*((uint32)10000))/sum; // 32 bit safe!
\r
217 if (move != MoveNone && move_is_legal(move,board)) {
\r
218 list_add_ex(list,move,score);
\r
227 void book_disp(const board_t * board) {
\r
229 char move_string[256];
\r
232 int treshold=option_get_int(Option,"BookTreshold");
\r
234 ASSERT(board!=NULL);
\r
236 if(BookFile==NULL) return;
\r
238 book_moves(list,board);
\r
240 for(i=0; i<list_size(list); i++){
\r
241 move_to_san(list->move[i],board,move_string,256);
\r
242 if(list->value[i]>10*treshold){
\r
243 printf(" %6s %5.2f%%\n",move_string,list->value[i]/100.0);
\r
245 printf(" %6s %5.2f%% (below treshold %4.2f%%)\n",
\r
246 move_string,list->value[i]/100.0,treshold/10.0);
\r
249 // this is necessary by the xboard protocol
\r
253 // book_learn_move()
\r
255 void book_learn_move(const board_t * board, int move, int result) {
\r
260 if(BookFile==NULL) return;
\r
262 ASSERT(board!=NULL);
\r
263 ASSERT(move_is_ok(move));
\r
264 ASSERT(result>=-1&&result<=+1);
\r
266 ASSERT(move_is_legal(move,board));
\r
268 for (pos = find_pos(board->key); pos < BookSize; pos++) {
\r
270 read_entry(entry,pos);
\r
271 if (entry->key != board->key) break;
\r
273 if (entry->move == move) {
\r
276 entry->sum += result+1;
\r
278 write_entry(entry,pos);
\r
287 void book_flush() {
\r
289 if(BookFile==NULL) return;
\r
291 if (fflush(BookFile) == EOF) {
\r
292 my_fatal("book_flush(): fflush(): %s\n",strerror(errno));
\r
298 static int find_pos(uint64 key) {
\r
300 int left, right, mid;
\r
303 // binary search (finds the leftmost entry)
\r
306 right = BookSize-1;
\r
308 ASSERT(left<=right);
\r
310 while (left < right) {
\r
312 mid = (left + right) / 2;
\r
313 ASSERT(mid>=left&&mid<right);
\r
315 read_entry(entry,mid);
\r
317 if (key <= entry->key) {
\r
324 ASSERT(left==right);
\r
326 read_entry(entry,left);
\r
328 return (entry->key == key) ? left : BookSize;
\r
333 static void read_entry(entry_t * entry, int n) {
\r
335 ASSERT(entry!=NULL);
\r
336 ASSERT(n>=0&&n<BookSize);
\r
338 if (fseek(BookFile,n*16,SEEK_SET) == -1) {
\r
339 my_fatal("read_entry(): fseek(): %s\n",strerror(errno));
\r
342 entry->key = read_integer(BookFile,8);
\r
343 entry->move = read_integer(BookFile,2);
\r
344 entry->count = read_integer(BookFile,2);
\r
345 entry->n = read_integer(BookFile,2);
\r
346 entry->sum = read_integer(BookFile,2);
\r
351 static void write_entry(const entry_t * entry, int n) {
\r
353 ASSERT(entry!=NULL);
\r
354 ASSERT(n>=0&&n<BookSize);
\r
356 if (fseek(BookFile,n*16,SEEK_SET) == -1) {
\r
357 my_fatal("write_entry(): fseek(): %s\n",strerror(errno));
\r
360 write_integer(BookFile,8,entry->key);
\r
361 write_integer(BookFile,2,entry->move);
\r
362 write_integer(BookFile,2,entry->count);
\r
363 write_integer(BookFile,2,entry->n);
\r
364 write_integer(BookFile,2,entry->sum);
\r
369 static uint64 read_integer(FILE * file, int size) {
\r
375 ASSERT(file!=NULL);
\r
376 ASSERT(size>0&&size<=8);
\r
380 for (i = 0; i < size; i++) {
\r
386 my_fatal("read_integer(): fgetc(): EOF reached\n");
\r
388 my_fatal("read_integer(): fgetc(): %s\n",strerror(errno));
\r
392 ASSERT(b>=0&&b<256);
\r
401 static void write_integer(FILE * file, int size, uint64 n) {
\r
406 ASSERT(file!=NULL);
\r
407 ASSERT(size>0&&size<=8);
\r
408 ASSERT(size==8||n>>(size*8)==0);
\r
410 for (i = size-1; i >= 0; i--) {
\r
412 b = (n >> (i*8)) & 0xFF;
\r
413 ASSERT(b>=0&&b<256);
\r
415 if (fputc(b,file) == EOF) {
\r
416 my_fatal("write_integer(): fputc(): %s\n",strerror(errno));
\r