-
Notifications
You must be signed in to change notification settings - Fork 0
/
book.c
407 lines (299 loc) · 8.99 KB
/
book.c
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
#include "book.h"
#include "types.h"
#include "board.h"
#include "moves.h"
#include "string.h"
#include "constants.h"
#include "diag.h"
#include <time.h>
#include <stdlib.h>
#include "bcm2835.h"
static FILE *bk = NULL;
U32 numEntries = 0;
static U32 findFirstKeyMatch( U64 val);
static U32 compareKey(U32 lower, U32 upper);
static void readPositionRecord(U32 offset, candidate_t *c);
static U64 reverseBytes( U64 input);
static void correctCastling(board_t *b, move_t *mv);
bookErr_t openBook( char *file )
{
U32 numUnique = 0;
U8 id[8];
U8 record[16];
FILE *temp = NULL;
bookErr_t retVal = BOOK_NO_ERROR;
// Seed random number once only when book is opened.
srand(bcm2835_st_read());
char filename[100];
// If a book is already opened, keep track of it just in case new can't be opened
if(bk != NULL) temp = bk;
// Create name in books folder
sprintf(filename, "/home/pi/chess/books/%s", file);
DPRINT("Attempting to open %s\n", filename);
// Point to this newly opened book
bk = fopen(filename, "rb");
// If we failed to open...
if(bk == NULL)
{
DPRINT("Could not open file\n");
// restore pointer
bk = temp;
return BOOK_FILE_NOT_FOUND;
}
// If we had kept a temporary
if(temp != NULL)
{
fclose(temp);
retVal = BOOK_REPLACED;
}
// Get the number of entries by examining the size of the file
fseek(bk, 0, SEEK_END);
numEntries = ftell(bk) / 16;
rewind(bk);
// GATHER STATS...
// Set the id to something that won't match the first entry...
memset(id, 0xFF, 8);
// Scan for differences between adjacent entries...
while( fread(record, 1, 16, bk) == 16)
{
if(memcmp(id, record, 8)) numUnique++;
memcpy(id, record, 8);
}
DPRINT("%d records, %d unique positions\n", numEntries, numUnique);
return retVal;
}
bool_t isBookOpen( void )
{
if(bk == NULL) return FALSE;
else return TRUE;
}
bookErr_t closeBook( void )
{
if(bk == NULL) return BOOK_ALREADY_CLOSED;
fclose(bk);
bk = NULL;
return BOOK_NO_ERROR;
}
bookErr_t listBookMoves( board_t *b )
{
U32 firstMatch;
U32 original;
U32 totalWeight = 0;
candidate_t c;
if(!isBookOpen()) return BOOK_NOT_OPEN;
original = firstMatch = findFirstKeyMatch( b->hash );
if(firstMatch == 0xFFFFFFFF) return BOOK_POSITION_NOT_FOUND;
// Get total of all the weights for all available moves
while(1)
{
readPositionRecord(firstMatch++, &c);
// If we've stepped outside the range of matches for this id, we're done..
if(c.hash != b->hash) break;
// Accumulate.
totalWeight += c.weight;
}
// now scan and print data for each entry...
while(1)
{
// Extract the data
readPositionRecord(original++, &c);
// make sure we're still in the block that matches this id
if(c.hash != b->hash) break;
// Need to correct polygot format of king "capturing" rook on castling moves
correctCastling(b, &c.mv);
// Show this move and its weight relative to the total.
DPRINT("%4.1f%% %s\n", (100.0 * (float)c.weight)/(float)totalWeight, moveToSAN(c.mv, b));
}
return BOOK_NO_ERROR;
}
bookErr_t getBestMove ( board_t *b, move_t *mv )
{
U32 firstMatch;
U32 bestPos;
U16 best = 0;
candidate_t c;
if(!isBookOpen()) return BOOK_NOT_OPEN;
firstMatch = findFirstKeyMatch( b->hash );
if(firstMatch == 0xFFFFFFFF) return BOOK_POSITION_NOT_FOUND;
while(1)
{
// Read the new record
readPositionRecord(firstMatch++, &c);
// If we have stepped outside of the range of matches, get out
if(c.hash != b->hash) break;
// If we have a new best, update best and remember our location
if(c.weight > best)
{
best = c.weight;
bestPos = firstMatch-1;
}
}
// Grab the data back out for the best
readPositionRecord(bestPos, &c);
// Need to correct polygot format of king "capturing" rook on castling moves
correctCastling(b, &c.mv);
// prepare the return value...
memcpy(mv, &c.mv, sizeof(move_t));
return BOOK_NO_ERROR;
}
bookErr_t getRandMove ( board_t *b, move_t *mv )
{
U32 firstMatch;
U32 original;
U32 totalWeight = 0;
candidate_t c;
// Generate a random number
int r = rand();
// Check error conditions first...
if(!isBookOpen()) return BOOK_NOT_OPEN;
original = firstMatch = findFirstKeyMatch( b->hash );
if(firstMatch == 0xFFFFFFFF) return BOOK_POSITION_NOT_FOUND;
while(1)
{
// Get the data
readPositionRecord(firstMatch++, &c);
// Make sure we are still in range...
if(c.hash != b->hash) break;
// Accumulate total weight.
totalWeight += c.weight;
}
// Create a random number from 1 to total weight
r %= totalWeight;
r++;
// Scan until our acculuated weight lands in a "bin"
while(1)
{
readPositionRecord(original++, &c);
if(c.weight >= r) break;
r -= c.weight;
}
// Need to correct polygot format of king "capturing" rook on castling moves
correctCastling(b, &c.mv);
memcpy(mv, &c.mv, sizeof(move_t));
return BOOK_NO_ERROR;
}
static U32 mid;
static U64 key;
static U64 thisKey;
// Entry into binary sort
static U32 findFirstKeyMatch( U64 val)
{
key = val;
return compareKey(0, numEntries - 1);
}
// Resursive sort function
static U32 compareKey(U32 lower, U32 upper)
{
mid = (lower + upper) / 2;
// Pull out the first 8 bytes (the hash value);
fseek(bk, mid * RECORD_SIZE + KEY_OFFSET , SEEK_SET);
fread(&thisKey, 1, 8 , bk);
// Stored MSB/LSB, so reverse the bytes...
thisKey = reverseBytes(thisKey);
// Do we have a match?
if(thisKey == key)
{
// YES! Work backwards to find the first
while(mid--)
{
fseek(bk, mid * RECORD_SIZE + KEY_OFFSET, SEEK_SET);
fread(&thisKey, 1, 8 , bk);
// reverse the byte ordering
thisKey = reverseBytes(thisKey);
// if we backed up too far, return the previous location
if(thisKey != key) return mid + 1;
}
return 0;
}
// If no hit, either search upper or lower half accordingly
else if (thisKey > key)
{
// can't narrow any further - no match available.
if(lower == mid) return 0xFFFFFFFF;
// Recurse, moving in closer
else return compareKey( lower, mid-1 );
}
else
{
// can't narrow any further - no match available.
if(upper == mid) return 0xFFFFFFFF;
// Recurse, moving in closer
else return compareKey( mid+1, upper );
}
}
// Reverse byte order of a U64
static U64 reverseBytes( U64 input)
{
int i;
U64 retValue = 0;
for(i=0;i<8;i++)
{
retValue <<= 8;
retValue |= *((U8 *)(&input)+i);
}
return retValue;
}
// Extract data from record in binary
static void readPositionRecord(U32 offset, candidate_t *c)
{
U8 toFile;
U8 toRow;
U8 fromFile;
U8 fromRow;
U8 promotion;
U8 bytes[8];
// move to desired location
fseek(bk, offset * RECORD_SIZE , SEEK_SET);
// Get the hash
fread(bytes, 1, 8 , bk);
c->hash = reverseBytes( *((U64 *)(&bytes)));
// The next two bytes are the move information...
fread(bytes, 1, 2 , bk);
// Pick apart the bits....
toFile = bytes[1] & 0x07;
toRow = (bytes[1] & 0x38) >> 3;
fromFile = ((bytes[1] & 0xC0) >> 6) | ( (bytes[0] & 0x01) << 2);
fromRow = (bytes[0] & 0x0E) >> 1;
promotion = (bytes[0] & 0x70) >> 4;
c->mv.to = toFile + (7 - toRow) * 8;
c->mv.from = fromFile + (7 - fromRow) * 8;
if(promotion == 0)
{
c->mv.promote = PIECE_NONE;
}
else
{
c->mv.promote = (piece_t)promotion + KNIGHT;
}
// Get the weight of this move
fread(bytes, 1, 2, bk);
c->weight = bytes[0] * 256 + bytes[1];
// IGNORE THE LEARN DATA...
}
// polyglot format uses an unusal notation for castling. Indicates a king to move to rook's square.
// we use king moving left or right two spaces, so make the adjustment if necessary...
static void correctCastling(board_t *b, move_t *mv)
{
// If from square is white king's square...
if( mv->from == E1)
{
// ... and white king is still there (AND white is on move)...
if( b->pieces[KING] & b->colors[b->toMove] & squareMask[E1] )
{
if ( mv->to == H1)
mv->to = G1;
else if (mv->to == A1)
mv->to = C1;
}
}
else if( mv->from == E8)
{
if( b->pieces[KING] & b->colors[b->toMove] & squareMask[E8] )
{
if( mv->to == H8)
mv->to = G8;
else if(mv->to == A8)
mv->to = C8;
}
}
}