-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpgn.cpp
198 lines (138 loc) · 4 KB
/
pgn.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
#include "pgn.h"
pgn::pgn(const std::vector<std::string>& files) {
pgn_files = std::vector<std::string>(files);
parse_files();
}
bool pgn::parse_files() {
size_t fcount = 0;
for (const auto& f : pgn_files) {
std::cout << "..parsing games from " << f << std::endl;
std::ifstream pgn_file(f.c_str(), std::fstream::in);
if (!pgn_file.is_open()) {
std::cout << "..failed to open pgn file, skipping" << std::endl;
continue;
}
std::string line;
game g;
position p;
std::istringstream fen(START_FEN);
p.setup(fen); // start position for each game
bool success = true;
while (std::getline(pgn_file, line)) {
if (is_empty(line) || is_header(line)) {
if (is_elo(line)) parse_elo(g, line);
continue;
}
if (!parse_moves(p, line, g)) {
std::cout << "..error parsing line: " << line << std::endl;
std::cout << "..from file " << f << std::endl;
success = false;
continue;
}
if (g.finished()) {
if (success) games.push_back(g);
g.clear();
p.clear();
std::istringstream tmp(START_FEN);
p.setup(tmp);
success = true;
}
}
++fcount;
pgn_file.close();
}
std::cout << "..parsed " << games.size() << " chess games." << std::endl;
return true;
}
bool pgn::parse_moves(position& p, const std::string& line, game& g) {
std::stringstream ss(line);
std::string token;
bool success = true;
bool comment = false;
while (ss >> std::skipws >> token) {
if (token.find('}') != std::string::npos) { comment = false; continue; }
if (token.find('{') != std::string::npos) { comment = true; }
if (comment) continue;
if (token == "1/2-1/2" || token == "1-0" ||
token == "0-1" || token == "*") {
g.result = (token == "1/2-1/2" ? Result::pgn_draw :
token == "1-0" ? Result::pgn_wwin :
Result::pgn_bwin);
return true;
}
strip(token);
if (token.find('.') != std::string::npos) {
std::string segment;
std::stringstream tmp(token);
std::getline(tmp, segment, '.');
std::getline(tmp, segment, '.');
token = segment;
}
if (is_empty(token)) continue;
Move m;
if (token.size() <= 1 || !move_from_san(p, token, m)) {
std::cout << "failed to parse move: " << token << std::endl;
std::cout << " line = " << line << std::endl;
success = false;
continue;
}
else {
g.moves.push_back(m);
p.do_move(m);
}
}
return success;
}
bool pgn::move_from_san(position& p, std::string& s, Move& m) {
Square to = Square::no_square;
m.type = Movetype::no_type;
int i = s.size() - 1;
if (s == "O-O" || s == "O-O-O") {
if (s == "O-O") to = (p.to_move() == white ? G1 : G8);
else to = (p.to_move() == white ? C1 : C8);
return find_move<king>(p, to, m);
}
m.type = U8(s[i] == 'N' ? promotion_n :
s[i] == 'B' ? promotion_b :
s[i] == 'R' ? promotion_r :
s[i] == 'Q' ? promotion_q : Movetype::no_type);
if (m.type != Movetype::no_type) {
i -= 1;
if (s[i] == '=') i -= 1;
}
to = get_square(s, i);
i -= 2;
// normal pawn moves 'a4' or promotion a8=q
if (i < 0 && to != Square::no_square) {
return find_move<pawn>(p, to, m);
}
// skip capture char
if (tolower(s[i]) == 'x') { i -= 1; }
if (p.piece_on(to) && m.type != Movetype::no_type) {
m.type = U8(m.type + 4); // convert to capture promotion
}
// row/col for moves with multiple candidates
int row = -1; int col = -1;
if (i > 0) {
if (isdigit(s[i])) row = int(s[i] - '1');
else col = int(s[i] - 'a');
i -= 1;
}
Piece piece = Piece::no_piece; int count = 0;
for (const auto& sp : SanPiece) {
if (s[0] == sp && count < 6) {
piece = Piece(count);
break;
}
++count;
}
if (piece == Piece::no_piece) {
col = int(s[0] - 'a');
}
return (piece == Piece::no_piece ? find_move<pawn>(p, to, m, row, col) :
piece == Piece::knight ? find_move<knight>(p, to, m, row, col) :
piece == Piece::bishop ? find_move<bishop>(p, to, m, row, col) :
piece == Piece::rook ? find_move<rook>(p, to, m, row, col) :
piece == Piece::queen ? find_move<queen>(p, to, m, row, col) :
find_move<king>(p, to, m, row, col));
}