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
|
#include "Fen.hpp"
namespace chessarbiter {
std::string FENParser::normalize_rank(std::string fen_rank) {
std::string normalized;
for (char &c : fen_rank) {
if (IS_DIGIT(c)) {
for (char i = 0; i < (c - '0'); i++) {
normalized += ' ';
}
} else {
normalized += c;
}
}
return (normalized);
}
char FENParser::NextToken(std::string fen, char loc) {
while (loc < fen.size() && IS_BLANK(fen[loc])) {
loc++;
}
return (loc);
}
char FENParser::NextRank(std::string fen, char loc) {
loc++;
while (loc < fen.size() && fen[loc] != '/' && fen[loc] != ' ') {
loc++;
}
return (loc);
}
std::string FENParser::Serialize(FEN fen) {
std::string s;
char skip = 0;
char rank = 0;
for (char &c : fen.board) {
rank++;
if (c == ' ') {
skip++;
} else {
if (skip > 0) {
s += std::to_string(skip);
skip = 0;
}
s += c;
}
if (rank == 8) {
if (skip != 0) {
s += std::to_string(skip);
skip = 0;
}
s += '/';
rank = 0;
}
}
// Remove last /
s = s.substr(0, s.size() - 1);
s += " ";
// Player
if (fen.player) {
s += "b ";
} else {
s += "w ";
}
// Castle
if (!(fen.white_castle_short || fen.white_castle_long ||
fen.black_castle_short || fen.black_castle_long)) {
s += "-";
} else {
if (fen.white_castle_short) {
s += "K";
}
if (fen.white_castle_long) {
s += "Q";
}
if (fen.black_castle_short) {
s += "k";
}
if (fen.black_castle_long) {
s += "q";
}
}
// Remaining
s += " " + fen.en_passant + " " + std::to_string(fen.halfmove) + " " +
std::to_string(fen.move);
return (s);
}
FEN FENParser::Parse(std::string fen) {
FEN parsed;
// Parse board
char loc = 0;
for (char rank = 0; rank < 8; rank++) {
char newloc = NextRank(fen, loc);
parsed.board += normalize_rank(fen.substr(loc, newloc - loc));
loc = newloc + 1;
}
// Parse player to move
loc = NextToken(fen, loc);
parsed.player = fen[loc] == 'b';
// Parse castling
loc = NextToken(fen, loc + 1);
char length = 0;
char cur_loc = loc;
while (!IS_BLANK(fen[cur_loc])) {
length++;
cur_loc++;
}
parsed.white_castle_short = false;
parsed.white_castle_long = false;
parsed.black_castle_short = false;
parsed.black_castle_long = false;
std::string castle = fen.substr(loc, length);
for (char i = 0; i < length; i++) {
switch (fen[loc + i]) {
case 'K':
parsed.white_castle_short = true;
break;
case 'Q':
parsed.white_castle_long = true;
break;
case 'k':
parsed.black_castle_short = true;
break;
case 'q':
parsed.black_castle_long = true;
break;
}
}
// Parse en passant
loc = NextToken(fen, loc + length);
if (fen[loc] != '-') {
parsed.en_passant = fen.substr(loc, 2);
loc++;
}
loc++;
// Parse half move counter
loc = NextToken(fen, loc);
std::string halfmove;
while (!IS_BLANK(fen[loc])) {
halfmove += fen[loc];
loc++;
}
parsed.halfmove = stoi(halfmove);
// Parse move counter
loc = NextToken(fen, loc);
std::string move;
while (loc < fen.size() && !IS_BLANK(fen[loc])) {
move += fen[loc];
loc++;
}
parsed.move = stoi(move);
return (parsed);
}
} // namespace chessarbiter
|