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
|
#include "fen.hpp"
#include "board.hpp"
#include <cassert>
#include <cctype>
#include <cstdint>
#include <cstdlib>
#include <print>
#include <string>
#include <sys/types.h>
bool WHITE = true;
bool BLACK = false;
void setBoardFen(const std::string fen, Game *g) {
// example fen rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1
int file = 0;
int rank = 0;
Position enpassant = {};
enum parserState {
BOARD,
PLAYER_TO_MOVE,
CASTLE,
ENPASSANT,
HALF_MOVE_CLOCK,
FULL_MOVE_CLOCK,
};
parserState parser_state = BOARD;
for (uint i = 0; i < fen.length(); i++) {
char c = fen[i];
if (c == ' ') {
if (parser_state == BOARD) {
parser_state = PLAYER_TO_MOVE;
continue;
}
if (parser_state == PLAYER_TO_MOVE) {
parser_state = CASTLE;
continue;
}
if (parser_state == CASTLE) {
parser_state = ENPASSANT;
continue;
}
if (parser_state == ENPASSANT) {
parser_state = HALF_MOVE_CLOCK;
continue;
}
if (parser_state == HALF_MOVE_CLOCK) {
parser_state = FULL_MOVE_CLOCK;
continue;
}
}
if (parser_state == ENPASSANT) {
if (toupper(c) >= 'A' && toupper(c) <= 'H') {
enpassant.file = static_cast<uint8_t>(toupper(c) - 'A');
g->canEnpassant = true;
}
if (c >= '1' && c <= '8') {
enpassant.rank = static_cast<uint8_t>(c - '0');
g->canEnpassant = true;
}
continue;
}
if (parser_state == CASTLE) {
if (c == '-') {
g->blackCastleKing = false;
g->blackCastleQueen = false;
g->whiteCastleKing = false;
g->whiteCastleQueen = false;
continue;
}
if (c == 'K') {
g->whiteCastleKing = true;
continue;
}
if (c == 'Q') {
g->whiteCastleQueen = true;
continue;
}
if (c == 'k') {
g->blackCastleKing = true;
continue;
}
if (c == 'q') {
g->blackCastleQueen = true;
continue;
}
}
if (parser_state == PLAYER_TO_MOVE) {
if (toupper(c) == 'W') {
g->turn = WHITE;
continue;
}
if (toupper(c) == 'B') {
g->turn = BLACK;
continue;
}
}
if (parser_state == BOARD) {
if (isdigit(c)) {
int move = c - '0';
file += move;
if (file > 8) {
std::println("Fatal: file was to big");
assert(false && "Malformed file");
exit(1);
return; // malformed file
}
continue;
}
if (c == '/') {
file = 0;
rank++;
if (rank >= 8) {
std::println("Fatal: rank was to big");
assert(false && "Malformed rank");
exit(1);
return; // malformed rank
}
continue;
};
if (toupper(c) == 'N') {
SetPiece(rank * 8 + file, KNIGHT, isupper(c), g);
file++;
continue;
};
if (toupper(c) == 'R') {
SetPiece(rank * 8 + file, ROOK, isupper(c), g);
file++;
continue;
}
if (toupper(c) == 'Q') {
SetPiece(rank * 8 + file, QUEEN, isupper(c), g);
file++;
continue;
}
if (toupper(c) == 'B') {
SetPiece(rank * 8 + file, BISHOP, isupper(c), g);
file++;
continue;
}
if (toupper(c) == 'P') {
SetPiece(rank * 8 + file, PAWN, isupper(c), g);
file++;
continue;
}
if (toupper(c) == 'K') {
SetPiece(rank * 8 + file, KING, isupper(c), g);
file++;
continue;
}
}
break;
}
g->enPassant = enpassant;
UpdateBitboards(g);
}
|