-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFen.cpp
168 lines (128 loc) · 3.7 KB
/
Fen.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
#include "Fen.hpp"
#include "Square.hpp"
#include <sstream>
#include <iostream>
const char* const Fen::StartingPos =
"rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
static std::string nextField(std::istream& stream) {
auto field = std::string();
stream >> field;
return field;
}
static bool parsePlacement(const std::string& placement, Board& board) {
auto currentFile = Square::Coordinate(0);
auto currentRank = Square::Coordinate(7);
for (auto c: placement) {
if (c >= '1' && c <='8') {
auto offset = c - '0';
currentFile += offset;
if (currentFile > 8) {
return false;
}
} else if (c == '/') {
if (currentRank == 0) {
return false;
}
currentFile = 0;
currentRank--;
} else {
auto optPiece = Piece::fromSymbol(c);
if (!optPiece.has_value()) {
return false;
}
auto optSquare = Square::fromCoordinates(currentFile, currentRank);
if (!optSquare.has_value()) {
return false;
}
board.setPiece(optSquare.value(), optPiece.value());
currentFile++;
}
}
return true;
}
static bool parseTurn(const std::string& turn, Board& board) {
if (turn == "w") {
board.setTurn(PieceColor::White);
return true;
} else if (turn == "b") {
board.setTurn(PieceColor::Black);
return true;
} else {
return false;
}
}
static bool parseCastlingRights(const std::string& rights, Board& board) {
if (rights == "-") {
board.setCastlingRights(CastlingRights::None);
return true;
}
auto cr = CastlingRights::None;
for (auto right : rights) {
switch (right) {
case 'K': cr |= CastlingRights::WhiteKingside; break;
case 'Q': cr |= CastlingRights::WhiteQueenside; break;
case 'k': cr |= CastlingRights::BlackKingside; break;
case 'q': cr |= CastlingRights::BlackQueenside; break;
default: return false;
}
}
board.setCastlingRights(cr);
return true;
}
static bool parseEnPassantSquare(const std::string& ep, Board& board) {
if (ep == "-") {
// No en passant square
return true;
}
auto square = Square::fromName(ep);
if (square.has_value()) {
board.setEnPassantSquare(square);
return true;
} else {
return false;
}
}
Board::Optional Fen::createBoard(std::istream& fenStream) {
auto placement = nextField(fenStream);
if (placement.empty()) {
return std::nullopt;
}
auto board = Board();
if (!parsePlacement(placement, board)) {
return std::nullopt;
}
auto turn = nextField(fenStream);
if (turn.empty()) {
return std::nullopt;
}
if (!parseTurn(turn, board)) {
return std::nullopt;
}
auto castlingRights = nextField(fenStream);
if (castlingRights.empty()) {
return std::nullopt;
}
if (!parseCastlingRights(castlingRights, board)) {
return std::nullopt;
}
auto ep = nextField(fenStream);
if (ep.empty()) {
return std::nullopt;
}
if (!parseEnPassantSquare(ep, board)) {
return std::nullopt;
}
auto halfmove = nextField(fenStream);
if (halfmove.empty()) {
return std::nullopt;
}
auto fullmove = nextField(fenStream);
if (fullmove.empty()) {
return std::nullopt;
}
return board;
}
Board::Optional Fen::createBoard(const std::string& fen) {
auto fenStream = std::stringstream(fen);
return createBoard(fenStream);
}