-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFEN.hpp
352 lines (335 loc) · 10.5 KB
/
FEN.hpp
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
#pragma once
#ifdef FLAG_JEMALLOC_EXTERNAL
#include <jemalloc/jemalloc.h>
#endif
#include <cctype>
#include <strings.h>
#include <String.hpp>
#include <Piece.hpp>
class Board;
// shredder-fen
// https://www.chessprogramming.org/Forsyth-Edwards_Notation#Shredder-FEN
namespace fen {
struct FEN {
COLOR active_player : 2;
std::string board;
std::string subs;
pos_pair_t castlings;
pos_t enpassant;
pos_t halfmove_clock;
ply_index_t fullmove;
bool chess960;
bool crazyhouse;
piece_bitboard_t crazyhouse_promoted;
inline bool operator==(const struct FEN &other) const {
return active_player == other.active_player
&& board == other.board && subs == other.subs
&& castlings == other.castlings
&& enpassant == other.enpassant && halfmove_clock == other.halfmove_clock
&& fullmove == other.fullmove;
}
};
fen::FEN load_from_string(const std::string &s) {
size_t i = 0;
fen::FEN f = {
.active_player=WHITE,
.board=""s,
.subs=""s,
.castlings=0x00,
.enpassant=board::nopos,
.halfmove_clock=0,
.fullmove=0,
.chess960=false,
.crazyhouse=false,
.crazyhouse_promoted=0x00,
};
// board
pos_t kingpos[2] = {board::nopos, board::nopos};
pos_t qrook[2] = {board::nopos, board::nopos};
pos_t krook[2] = {board::nopos, board::nopos};
pos_t board_index = 0;
// str::pdebug("FEN", s);
size_t slashcount = 0;
for(const char *c = s.c_str(); *c != '\0' && !isspace(*c); ++c, ++i) {
if(*c == '~') {
piece::set_pos(f.crazyhouse_promoted, board_index - 1);
continue;
}
// for some reason occurs in crazyhouse
assert(slashcount < 9);
if(*c == '/') {
++slashcount;
if(slashcount == 8) {
f.crazyhouse = true;
}
continue;
} else if(*c == '[') {
++slashcount;
assert(slashcount == 8);
f.crazyhouse = true;
continue;
} else if(*c == ']') {
continue;
}
if(slashcount == 8) {
f.subs += *c;
continue;
}
if(isdigit(*c)) {
for(int j=0;j<*c-'0';++j, ++board_index)f.board+=' ';
continue;
}
const pos_t ind = board::_pos(board::_x(board_index), 8 - board::_y(board_index));
f.board += *c;
switch(*c) {
case 'K':
{
kingpos[WHITE]=board::_x(ind);
// const char ch[] = {char('A' + kingpos[WHITE]), '\0'};
// str::pdebug("kingpos[WHITE]", ch);
}
break;
case 'k':
{
kingpos[BLACK]=board::_x(ind);
// const char ch[] = {char('A' + kingpos[BLACK]), '\0'};
// str::pdebug("kingpos[BLACK]", ch);
}
break;
case 'R':
if(board::_y(ind) == -1+1) {
if(kingpos[WHITE]==board::nopos) {
qrook[WHITE] = board::_x(ind);
// const char ch[] = {char('A' + qrook[WHITE]), '\0'};
// str::pdebug("qrook[WHITE]", ch);
} else {
krook[WHITE] = board::_x(ind);
// const char ch[] = {char('A' + krook[WHITE]), '\0'};
// str::pdebug("krook[WHITE]", ch);
}
}
break;
case 'r':
if(board::_y(ind) == -1+8) {
if(kingpos[BLACK]==board::nopos) {
qrook[BLACK] = board::_x(ind);
// const char ch[] = {char('A' + qrook[BLACK]), '\0'};
// str::pdebug("qrook[BLACK]", ch);
} else {
krook[BLACK] = board::_x(ind);
// const char ch[] = {char('A' + krook[BLACK]), '\0'};
// str::pdebug("krook[BLACK]", ch);
}
}
break;
}
++board_index;
}
assert(board_index == board::SIZE);
assert(f.board.length() <= board::SIZE);
// skip space
while(isspace(s[i]))++i;
// active plaer
assert(index("wWbB", s[i]) != nullptr);
if(s[i] == 'b' || s[i] == 'B')f.active_player=BLACK;
++i;
// skip space
while(isspace(s[i]))++i;
// castlings
while(!isspace(s[i])) {
assert(index("KkQqabcdefghABCDEFGH-", s[i]) != nullptr);
char c = s[i];
switch(s[i]) {
case 'K':
c='A'+krook[WHITE];
// {
// const char ch[2] = {char(c), '\0'};
// str::pdebug("convert K", ch);
// }
assert(krook[WHITE] != board::nopos);
break;
case 'Q':
c='A'+qrook[WHITE];
// {
// const char ch[2] = {char(c), '\0'};
// str::pdebug("convert Q", ch);
// }
assert(qrook[WHITE] != board::nopos);
break;
case 'k':
c='a'+krook[BLACK];
// {
// const char ch[2] = {char(c), '\0'};
// str::pdebug("convert k", ch);
// }
assert(krook[BLACK] != board::nopos);
break;
case 'q':
c='a'+qrook[BLACK];
// {
// const char ch[2] = {char(c), '\0'};
// str::pdebug("convert q", ch);
// }
assert(qrook[BLACK] != board::nopos);
break;
case '-':break;
default:;break;
}
if(isupper(c)) {
const pos_t castlfile = c - 'A';
assert(castlfile == qrook[WHITE] || castlfile == krook[WHITE]);
if(kingpos[WHITE] != E || (castlfile != A && castlfile != H)) {
f.chess960 = true;
}
f.castlings |= bitmask::_pos_pair(1u << (c - 'A'), 0x00);
} else if(c != '-') {
const pos_t castlfile = c - 'a';
assert(castlfile == qrook[BLACK] || castlfile == krook[BLACK]);
if(kingpos[BLACK] != E || (castlfile != A && castlfile != H)) {
f.chess960 = true;
}
f.castlings |= bitmask::_pos_pair(0x00, 1u << (c - 'a'));
}
++i;
}
// str::pdebug("chess960", f.chess960);
// skip space
while(isspace(s[i]))++i;
// enpassant
char probe = s[i];
if(probe != '-') {
char x = probe;
assert(index("abcdefgh", x)!=nullptr);
x -= 'a';
char y = s[++i];
assert(index("12345678", y)!=nullptr);
y -= '1';
f.enpassant = board::_pos(A+x, 1+y);
} else {
f.enpassant = board::nopos;
++i;
}
// skip space
while(isspace(s[i]))++i;
// half-moves, fullmoves
if(s[i] == '\0') {
f.halfmove_clock = 0;
f.fullmove = 1;
return f;
}
int sc;
sc = sscanf(&s[i], "%hhu %hd", &f.halfmove_clock, &f.fullmove);
if(f.fullmove == 0)++f.fullmove;
assert(sc != -1);
i += sc;
// done
return f;
}
fen::FEN load_from_file(const std::string &fname) {
FILE *fp = fopen(fname.c_str(), "r");
assert(fp != nullptr);
std::string s; int c;
while((c=fgetc(fp))!=EOF)s+=c;
fclose(fp);
return load_from_string(s);
}
// performs FEN reading only on demand - useful for debugging
// and perhaps faster loading time
struct lazyFEN {
const char *fenstring;
constexpr INLINE explicit lazyFEN(const char *c):
fenstring(c)
{}
INLINE operator FEN() const {
return fen::load_from_string(fenstring);
}
};
// standard
const fen::FEN starting_pos = fen::load_from_string("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"s);
constexpr fen::lazyFEN pins_pos("rnb1k1nr/ppppbppp/8/4Q3/4P2q/8/PPPP1PPP/RNB1KBNR w KQkq - 1 4");
constexpr fen::lazyFEN castling_pos("r3k2r/pppppppp/8/8/8/8/PPPPPPPP/R3K2R w KQkq - 0 1");
constexpr fen::lazyFEN doublecheck_test_pos("rnbqkbnr/pppp1ppp/8/8/4N3/5N2/PPPPQPPP/R1B1KB1R w KQkq - 8 8");
constexpr fen::lazyFEN check_test_pos("rnbqkb1r/pppp1ppp/5n2/4N3/8/8/PPPPQPPP/RNB1KB1R w KQkq - 2 5");
constexpr fen::lazyFEN promotion_test_pos("1k3n1n/4PPP1/8/8/8/8/1pp1PPPP/4K3 w - - 0 1");
constexpr fen::lazyFEN search_explosion_pos("q2k2q1/2nqn2b/1n1P1n1b/2rnr2Q/1NQ1QN1Q/3Q3B/2RQR2B/Q2K2Q1 w - -");
constexpr fen::lazyFEN quiesc_fork_position("r1b1k2r/pp1p1ppp/3pp3/1Nb4q/1n2PPnN/1P1P2P1/P1P4P/R1BQKB1R w KQkq - 0 1");
// crazyhouse
constexpr fen::lazyFEN starting_pos_ch("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR[] w KQkq - 0 1");
fen::FEN export_from_board(const Board &board);
std::string export_as_string(const fen::FEN &f) {
std::string s = ""s;
pos_t kingpos[2] = {board::nopos, board::nopos};
pos_t write_index = 0;
for(pos_t y = 0; y < board::LEN; ++y) {
pos_t emptycount = 0;
for(pos_t x = 0; x < board::LEN; ++x, ++write_index) {
const pos_t ind = board::_pos(A+x, 1+y);
assert(index("KkQqRrBbNnPp ", f.board[ind]) != nullptr);
// increment counter of empty spaces
if(f.board[ind] == ' ') {
++emptycount;
continue;
}
// drop the number of empty cells before proceeding
if(emptycount > 0) {
s += std::to_string(emptycount);
emptycount = 0;
}
// add viewed piece
s += f.board[ind];
// mark it as promoted
if(f.crazyhouse && piece::is_set(f.crazyhouse_promoted, ind)) {
s += "~"s;
}
// keep track of king positions (obsolete in exporting)
if(f.board[ind] == 'K') {
kingpos[WHITE] = ind;
} else if(f.board[ind] == 'k') {
kingpos[BLACK] = ind;
}
}
if(emptycount > 0) {
s += std::to_string(emptycount);
emptycount = 0;
}
if(y != board::LEN - 1)s+="/";
}
if(f.crazyhouse) {
//s += "/"s + f.subs;
s += "["s + f.subs + "]"s;
}
s += ' ';
s += (f.active_player == WHITE) ? 'w' : 'b';
s += ' ';
if(!f.castlings) {
s += '-';
} else {
for(COLOR color : {WHITE, BLACK}) {
const pos_t mask = (color == WHITE) ? bitmask::first(f.castlings) : bitmask::second(f.castlings);
for(pos_t _c = 0; _c < board::LEN; ++_c) {
const pos_t c = board::LEN - _c - 1;
if(mask & (1 << c)) {
char ch = ((color == WHITE) ? 'A' : 'a') + c;
if(!f.chess960) {
switch(ch) {
case 'H':ch='K';break;
case 'A':ch='Q';break;
case 'h':ch='k';break;
case 'a':ch='q';break;
default:break;
}
}
s += ch;
}
}
}
}
s += ' ';
s += (f.enpassant == board::nopos) ? "-"s : board::_pos_str(f.enpassant);
s += " "s + std::to_string(f.halfmove_clock) + " "s + std::to_string(f.fullmove);
return s;
}
INLINE std::string export_as_string(const Board &board) {
return export_as_string(export_from_board(board));
}
} // namespace fen