2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or
5 * (at your option) any later version.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19 * based on Polyglot code
40 static FILE * book_file
;
43 static int find_pos(uint64_t key
);
44 static void read_entry(struct entry_t
*entry
, int n
);
45 static uint64_t read_integer(FILE *file
, int size
);
52 r
= (double)(rand()) / ((double)(RAND_MAX
) + 1.0);
54 return (int)(floor(r
* (double)(n
)));
65 book_open(const char file_name
[])
67 book_file
= fopen(file_name
, "rb");
69 if (book_file
!= NULL
) {
70 if (fseek(book_file
, 0, SEEK_END
) == -1)
71 fprintf(stderr
, "book_open(): fseek(): %s\n", strerror(errno
));
73 book_size
= ftell(book_file
) / 16;
75 fprintf(stderr
, "book_open(): ftell(): %s\n", strerror(errno
));
77 fprintf(stderr
, "Unable open book\n");
83 if (book_file
!= NULL
&& fclose(book_file
) == EOF
)
84 fprintf(stderr
, "book_close(): fclose(): %s\n", strerror(errno
));
92 int best_move
, best_score
;
93 struct entry_t entry
[1];
96 if (book_file
!= NULL
&& book_size
!= 0) {
97 /* draw a move according to a fixed probability distribution */
101 for (pos
= find_pos(brd
.hash_key
); pos
< book_size
; pos
++) {
102 read_entry(entry
, pos
);
104 if (entry
->key
!= brd
.hash_key
)
108 score
= entry
->count
;
111 if (my_random(best_score
) < score
)
115 if (best_move
!= 0) {
116 /* convert polyglot move to our format */
117 from
= SET_FROM(((best_move
>> 9) & 7) * 8 + \
118 ((best_move
>> 6) & 7));
119 to
= SET_TO(((best_move
>> 3) & 7) * 8 + (best_move
& 7));
121 if (square64
[from
] == KING
) {
122 if (GET_FROM(from
) == E1
&& GET_TO(to
) == H1
)
124 else if (GET_FROM(from
) == E1
&& GET_TO(to
) == A1
)
126 else if (GET_FROM(from
) == E8
&& GET_TO(to
) == H8
)
128 else if (GET_FROM(from
) == E8
&& GET_TO(to
) == A8
)
137 for (i
= 0; i
< moves
.count
; i
++) {
138 if (!make_move(moves
.move
[i
], TRUE
))
140 if ((moves
.move
[i
] & 0xfff) == (from
+ to
))
141 return moves
.move
[i
];
150 find_pos(uint64_t key
)
152 int left
, right
, mid
;
153 struct entry_t entry
[1];
155 /* binary search (finds the leftmost entry) */
157 right
= book_size
- 1;
159 while (left
< right
) {
160 mid
= (left
+ right
) / 2;
161 read_entry(entry
, mid
);
163 if (key
<= entry
->key
)
168 read_entry(entry
, left
);
170 return (entry
->key
== key
) ? left
: book_size
;
174 read_entry(struct entry_t
*entry
, int n
)
176 if (fseek(book_file
,n
*16,SEEK_SET
) == -1)
177 fprintf(stderr
, "read_entry(): fseek(): %s\n",strerror(errno
));
179 entry
->key
= read_integer(book_file
, 8);
180 entry
->move
= read_integer(book_file
, 2);
181 entry
->count
= read_integer(book_file
, 2);
182 entry
->n
= read_integer(book_file
, 2);
183 entry
->sum
= read_integer(book_file
, 2);
187 read_integer(FILE *file
, int size
)
193 for (i
= 0; i
< size
; i
++) {
198 fprintf(stderr
, "read_integer(): fgetc(): EOF reached\n");
200 fprintf(stderr
, "read_integer(): fgetc(): %s\n", \