-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathopeningbook.cpp
387 lines (300 loc) · 9 KB
/
openingbook.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
//===========================================================//
//
// Maverick Chess Engine
// Copyright 2013 Steve Maughan
//
//===========================================================//
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <assert.h>
#include <Windows.h>
#include <iostream>
#include "defs.h"
#include "data.h"
#include "procs.h"
void close_book()
{
if (uci.opening_book.f != NULL)
fclose(uci.opening_book.f);
}
void set_own_book(BOOL value)
{
uci.opening_book.use_own_book = value;
}
void set_opening_book(char *book)
{
strncpy(uci.opening_book.filename, book, sizeof uci.opening_book.filename);
if (uci.opening_book.f != NULL) {
fclose(uci.opening_book.f);
}
uci.opening_book.f = fopen(uci.opening_book.filename, "rb");
fseek(uci.opening_book.f, -16, SEEK_END);
uci.opening_book.book_size = ftell(uci.opening_book.f) / 16;
}
int book_count()
{
int count = 0;
CHAR szSearch[MAX_PATH] = "*.bin";
HANDLE hFind = NULL;
WIN32_FIND_DATAA FindFileData;
hFind = FindFirstFileA(szSearch, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE) return count;
do {
count++;
} while (FindNextFileA(hFind, &FindFileData));
FindClose(hFind);
return count;
}
char *book_string()
{
static char s[1024] = { 0 };
static char d[1024] = { 0 };
static char b[1024] = { 0 };
LARGE_INTEGER biggest;
CHAR szSearch[MAX_PATH] = "*.bin";
HANDLE hFind = NULL;
WIN32_FIND_DATAA FindFileData;
LARGE_INTEGER size;
biggest.QuadPart = 0;
hFind = FindFirstFileA(szSearch, &FindFileData);
if (hFind == INVALID_HANDLE_VALUE) return s;
do {
strcat(s, " var ");
strcat(s, FindFileData.cFileName);
size.HighPart = FindFileData.nFileSizeHigh;
size.LowPart = FindFileData.nFileSizeLow;
if (size.QuadPart > biggest.QuadPart) {
biggest.QuadPart = FindFileData.nFileSizeHigh;
strcpy(d, FindFileData.cFileName);
}
} while (FindNextFileA(hFind, &FindFileData));
FindClose(hFind);
//-- Find any Maverick opening books (...and give them preference)
biggest.QuadPart = 0;
strcpy(szSearch, "Maverick*.bin");
hFind = FindFirstFileA(szSearch, &FindFileData);
if (hFind != INVALID_HANDLE_VALUE) {
do {
size.HighPart = FindFileData.nFileSizeHigh;
size.LowPart = FindFileData.nFileSizeLow;
if (size.QuadPart > biggest.QuadPart) {
biggest.QuadPart = FindFileData.nFileSizeHigh;
strcpy(d, FindFileData.cFileName);
}
} while (FindNextFileA(hFind, &FindFileData));
FindClose(hFind);
}
strcpy(b, "option name Opening Book type combo default ");
strcat(b, d);
strcat(b, s);
strcpy(uci.opening_book.filename, d);
uci.opening_book.f = fopen(uci.opening_book.filename, "rb");
fseek(uci.opening_book.f, -16, SEEK_END);
uci.opening_book.book_size = ftell(uci.opening_book.f) / 16;
return b;
}
unsigned long long read_integer(FILE *file, int size)
{
unsigned long long n;
int i;
int b;
n = 0;
for (i = 0; i < size; i++)
{
b = fgetc(file);
n = (n << 8) | b;
}
return n;
}
void read_book_move(int index, struct t_book_move *book_move)
{
fseek(uci.opening_book.f, index * 16, SEEK_SET);
book_move->key = read_integer(uci.opening_book.f, 8);
book_move->move = read_integer(uci.opening_book.f, 2);
book_move->weight = read_integer(uci.opening_book.f, 2);
book_move->n = read_integer(uci.opening_book.f, 2);
book_move->learn = read_integer(uci.opening_book.f, 2);
}
struct t_move_record *decode_move(struct t_board *board, unsigned int move)
{
int from_square = (move >> 6) & 077;
int from_rank = (from_square >> 3) & 0x7;
int from_file = from_square & 0x7;
from_square = (8 * from_rank) + from_file;
int to_square = move & 077;
int to_rank = (to_square >> 3) & 0x7;
int to_file = to_square & 0x7;
to_square = (8 * to_rank) + to_file;
int promote = (move >> 12) & 0x7;
t_chess_piece piece = board->square[from_square];
t_chess_piece captured = BLANK;
if (board->square[to_square] != BLANK && COLOR(piece) != COLOR(board->square[to_square]))
captured = PIECETYPE(board->square[to_square]);
if (promote)
return move_directory[from_square][to_square][piece] + (4 * captured) + promote - 1;
else
return move_directory[from_square][to_square][piece] + captured;
}
t_move_record *probe_book(struct t_board *board)
{
//-- The Key we're looking for!
t_hash key = board->hash;
//-- Used to get the data from the file
struct t_book_move book_move[1];
//-- Not open?
if (uci.opening_book.f == NULL)
return NULL;
//-- List of possible moves
struct t_move_list moves[1];
moves->count = 0;
//-- First entry
int first = 0;
//-- Last entry
int last = uci.opening_book.book_size;
//-- Binary search (which cleverly finds the lowest entry with the same key)
while (first < last) {
int middle = (first + last) / 2;
read_book_move(middle, book_move);
if (key <= book_move->key) {
last = middle;
}
else {
first = middle + 1;
}
}
assert(first == last);
read_book_move(first, book_move);
//-- return NULL value if we cannot find the move
if (book_move->key != key)
return NULL;
//-- local storage
struct t_move_record *move;
//-- Add all suitable moves to the list
int i = 0;
int sum = 0;
do {
move = decode_move(board, book_move->move);
moves->move[i] = move;
moves->value[i] = book_move->weight;
sum += book_move->weight;
i++;
if (first + i < uci.opening_book.book_size)
read_book_move(first + i, book_move);
} while (book_move->key == key && first + i < uci.opening_book.book_size);
moves->count = i;
//-- Generate random move
int random = rand() % sum;
//-- Select the best move
sum = 0;
for (i = 0; i < moves->count; i++) {
sum += moves->value[i];
if (random <= sum)
return moves->move[i];
}
return moves->move[0];
}
int book_move_count(struct t_board *board){
//-- The Key we're looking for!
t_hash key = board->hash;
//-- Used to get the data from the file
struct t_book_move book_move[1];
//-- Not open?
if (uci.opening_book.f == NULL)
return 0;
//-- First entry
int first = 0;
//-- Last entry
int last = uci.opening_book.book_size;
//-- Binary search (which cleverly finds the lowest entry with the same key)
while (first < last) {
int middle = (first + last) / 2;
read_book_move(middle, book_move);
if (key <= book_move->key) {
last = middle;
}
else {
first = middle + 1;
}
}
assert(first == last);
read_book_move(first, book_move);
//-- return 0 value if we cannot find the move
if (book_move->key != key)
return 0;
//-- Count all suitable moves to the list
int i = 0;
do {
i++;
if (first + i < uci.opening_book.book_size)
read_book_move(first + i, book_move);
} while (book_move->key == key && first + i < uci.opening_book.book_size);
//-- Return the move count
return i;
}
void fill_opening_move_list(struct t_board *board, struct t_move_list *move_list){
//-- The Key we're looking for!
t_hash key = board->hash;
//-- Used to get the data from the file
struct t_book_move book_move[1];
//-- Not open?
if (uci.opening_book.f == NULL)
return;
//-- List of possible moves
struct t_move_list moves[1];
moves->count = 0;
//-- First entry
int first = 0;
//-- Last entry
int last = uci.opening_book.book_size;
//-- Binary search (which cleverly finds the lowest entry with the same key)
while (first < last) {
int middle = (first + last) / 2;
read_book_move(middle, book_move);
if (key <= book_move->key) {
last = middle;
}
else {
first = middle + 1;
}
}
assert(first == last);
read_book_move(first, book_move);
//-- return NULL value if we cannot find the move
if (book_move->key != key)
return;
//-- local storage
struct t_move_record *move;
//-- Add all suitable moves to the list
int i = 0;
int sum = 0;
do {
move = decode_move(board, book_move->move);
moves->move[i] = move;
moves->value[i] = book_move->weight;
sum += book_move->weight;
i++;
if (first + i < uci.opening_book.book_size)
read_book_move(first + i, book_move);
} while (book_move->key == key && first + i < uci.opening_book.book_size);
moves->count = i;
move_list->count = 0;
int n = 0;
do{
//-- Generate random move
int random = rand() % sum;
//-- Select the best move
int value_sum = 0;
for (i = 0; i < moves->count; i++) {
value_sum += moves->value[i];
if (random <= value_sum){
if (!is_move_in_list(moves->move[i], move_list)){
move_list->move[move_list->count] = moves->move[i];
move_list->count++;
}
break;
}
}
n++;
}while (move_list->count * 4 < moves->count * 3 && n < moves->count * 5);
}