-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpolybook.c
More file actions
235 lines (196 loc) · 5.93 KB
/
polybook.c
File metadata and controls
235 lines (196 loc) · 5.93 KB
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
//
// Created by Eric Silverman on 14/10/2018.
//
#include "defs.h"
#include "polykeys.h"
#include "string.h"
typedef struct {
U64 key;
unsigned short move;
unsigned short weight;
unsigned int learn;
} S_POLY_BOOK_ENTRY;
long NumEntries = 0;
char bookName[50] = "bookfish.bin";
S_POLY_BOOK_ENTRY *entries;
const int PolyKindOfPiece[13] = {
-1, 1, 3, 5, 7, 9, 11, 0, 2, 4, 6, 8, 10
};
void InitPolyBook() {
EngineOptions->UseBook = FALSE;
//strcpy(EngineOptions->BookName,"bookfish.bin");
FILE *pFile = fopen(EngineOptions->BookName,"rb");
if(pFile == NULL) {
printf("Book File Not Read\n\n");
} else {
fseek(pFile,0,SEEK_END);
long position = ftell(pFile);
if(position < sizeof(S_POLY_BOOK_ENTRY)) {
printf("No Entries Found!\n");
return;
}
NumEntries = position / sizeof(S_POLY_BOOK_ENTRY);
printf("%ld Entries found in opening book\n", NumEntries);
entries = (S_POLY_BOOK_ENTRY*)malloc(NumEntries * sizeof(S_POLY_BOOK_ENTRY));
rewind(pFile);
size_t returnValue;
returnValue = fread(entries, sizeof(S_POLY_BOOK_ENTRY), NumEntries, pFile);
printf("%ld Entries read in from opening book %s\n\n", returnValue, EngineOptions->BookName);
if(NumEntries > 0) {
EngineOptions->UseBook = TRUE;
}
}
}
void CleanPolyBook() {
free(entries);
}
int HasPawnForCapture(const S_BOARD *board) {
int sqWithPawn = 0;
int targetPce = (board->side == WHITE) ? wP : bP;
if(board->enPas != NO_SQ) {
if(board->side == WHITE) {
sqWithPawn = board->enPas - 10;
} else {
sqWithPawn = board->enPas + 10;
}
if(board->pieces[sqWithPawn + 1] == targetPce) {
return TRUE;
} else if(board->pieces[sqWithPawn - 1] == targetPce) {
return TRUE;
}
}
return FALSE;
}
U64 PolyKeyFromBoard(const S_BOARD *board) {
int sq = 0, rank = 0, file = 0;
U64 finalKey = 0;
int piece = EMPTY;
int polyPiece = 0;
int offset = 0;
for(sq = 0; sq < BRD_SQ_NUM; ++sq) {
piece = board->pieces[sq];
if(piece != NO_SQ && piece != EMPTY && piece != OFFBOARD) {
ASSERT(piece >= wP && piece <= bK);
polyPiece = PolyKindOfPiece[piece];
rank = RanksBrd[sq];
file = FilesBrd[sq];
finalKey ^= Random64Poly[(64 * polyPiece) + (8 * rank) + file];
}
}
// castling
offset = 768;
if(board->castlePerm & WKCA) finalKey ^= Random64Poly[offset + 0];
if(board->castlePerm & WQCA) finalKey ^= Random64Poly[offset + 1];
if(board->castlePerm & BKCA) finalKey ^= Random64Poly[offset + 2];
if(board->castlePerm & BQCA) finalKey ^= Random64Poly[offset + 3];
// enpassant
offset = 772;
if(HasPawnForCapture(board) == TRUE) {
file = FilesBrd[board->enPas];
finalKey ^= Random64Poly[offset + file];
}
if(board->side == WHITE) {
finalKey ^= Random64Poly[780];
}
return finalKey;
}
unsigned short endian_swap_u16(unsigned short x)
{
x = (x>>8) |
(x<<8);
return x;
}
unsigned int endian_swap_u32(unsigned int x)
{
x = (x>>24) |
((x<<8) & 0x00FF0000) |
((x>>8) & 0x0000FF00) |
(x<<24);
return x;
}
U64 endian_swap_u64(U64 x)
{
x = (x>>56) |
((x<<40) & 0x00FF000000000000) |
((x<<24) & 0x0000FF0000000000) |
((x<<8) & 0x000000FF00000000) |
((x>>8) & 0x00000000FF000000) |
((x>>24) & 0x0000000000FF0000) |
((x>>40) & 0x000000000000FF00) |
(x<<56);
return x;
}
int ConvertPolyMoveToInternalMove(unsigned short polyMove, S_BOARD *board) {
int ff = (polyMove >> 6) & 7;
int fr = (polyMove >> 9) & 7;
int tf = (polyMove >> 0) & 7;
int tr = (polyMove >> 3) & 7;
int pp = (polyMove >> 12) & 7;
char moveString[6];
if(pp == 0) {
sprintf(moveString, "%c%c%c%c",
FileChar[ff],
RankChar[fr],
FileChar[tf],
RankChar[tr]);
} else {
char promChar = 'q';
switch(pp) {
case 1: promChar = 'n'; break;
case 2: promChar = 'b'; break;
case 3: promChar = 'r'; break;
}
sprintf(moveString, "%c%c%c%c%c",
FileChar[ff],
RankChar[fr],
FileChar[tf],
RankChar[tr],
promChar);
}
return ParseMove(moveString, board);
}
void ListBookMoves(U64 polyKey) {
int start = 0;
S_POLY_BOOK_ENTRY *entry;
unsigned short move;
for(entry = entries; entry < entries + NumEntries; entry++) {
if(polyKey == endian_swap_u64(entry->key)) {
move = endian_swap_u16(entry->move);
printf("Key:%llx Index:%d Move:%c%c%c%c\n",
endian_swap_u64(entry->key),
start,
FileChar[(move >> 6) & 7],
RankChar[(move >> 9) & 7],
FileChar[(move >> 0) & 7],
RankChar[(move >> 3) & 7]);
}
start++;
}
}
int GetBookMove(S_BOARD *board) {
int index = 0;
S_POLY_BOOK_ENTRY *entry;
unsigned short move;
const int MAXBOOKMOVES = 32;
int bookMoves[MAXBOOKMOVES];
int tempMove = NOMOVE;
int count = 0;
U64 polyKey = PolyKeyFromBoard(board);
//ListBookMoves(polyKey);
for(entry = entries; entry < entries + NumEntries; entry++) {
if(polyKey == endian_swap_u64(entry->key)) {
move = endian_swap_u16(entry->move);
tempMove = ConvertPolyMoveToInternalMove(move, board);
if(tempMove != NOMOVE) {
bookMoves[count++] = tempMove;
if(count > MAXBOOKMOVES) break;
}
}
}
if(count != 0) {
int randMove = rand() % count;
return bookMoves[randMove];
} else {
return NOMOVE;
}
}