summaryrefslogtreecommitdiff
path: root/src/Fen.cpp
blob: 54dddfd07a718e54b060e8db17d284aa596a34cc (plain)
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
#include "Fen.hpp"

namespace chessarbiter {

std::string FENParser::normalize_rank(const std::string &fen_rank) {
  std::string normalized;
  for (const char &c : fen_rank) {
    if (IS_DIGIT(c)) {
      for (char i = 0; i < (c - '0'); i++) {
        normalized += ' ';
      }
    } else {
      // Check validity
      char c2 = std::tolower(c);
      if (!(c2 == 'p' || c2 == 'r' || c2 == 'n' || c2 == 'b' || c2 == 'q' ||
            c2 == 'k' || c2 == ' ')) {
        throw InvalidFEN();
      }
      // Add
      normalized += c;
    }
  }
  // Check validity
  if (normalized.size() != 8) {
    throw InvalidFEN();
  }
  return (normalized);
}

char FENParser::NextToken(const std::string &fen, char loc) {
  while (loc < fen.size() && IS_BLANK(fen[loc])) {
    loc++;
  }
  return (loc);
}

char FENParser::NextRank(const std::string &fen, char loc) {
  loc++;
  while (loc < fen.size() && fen[loc] != '/' && fen[loc] != ' ') {
    loc++;
  }
  return (loc);
}

std::string FENParser::Serialize(const FEN &fen) {
  std::string s;
  char skip = 0;
  char rank = 0;
  for (const 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(const std::string &fen) {
  FEN parsed;

  // Parse board
  char loc = 0;
  for (char rank = 0; rank < 8; rank++) {
    CHECK_LOC();
    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);
  if (!(fen[loc] == 'w' || fen[loc] == 'b')) {
    throw InvalidFEN();
  }
  parsed.player = fen[loc] == 'b';
  CHECK_LOC();

  // Parse castling
  loc = NextToken(fen, loc + 1);
  char length = 0;
  char cur_loc = loc;
  while (!IS_BLANK(fen[cur_loc])) {
    length++;
    cur_loc++;
    CHECK_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;
    case '-':
      break;
    default:
      throw InvalidFEN();
    }
  }

  // Parse en passant
  loc = NextToken(fen, loc + length);
  if (fen[loc] != '-') {
    parsed.en_passant = fen.substr(loc, 2);
    loc++;
    // Check format
    char f = parsed.en_passant[0];
    char r = parsed.en_passant[1];
    if (!((f >= 'a' && f <= 'h') && (r >= '1' && r <= '8'))) {
      throw InvalidFEN();
    }
  }
  loc++;
  CHECK_LOC();

  // Parse half move counter
  loc = NextToken(fen, loc);
  std::string halfmove;
  while (!IS_BLANK(fen[loc])) {
    if (!IS_DIGIT(fen[loc])) {
      throw InvalidFEN();
    }
    halfmove += fen[loc];
    loc++;
    CHECK_LOC();
  }
  parsed.halfmove = stoi(halfmove);

  // Parse move counter
  loc = NextToken(fen, loc);
  std::string move;
  while (loc < fen.size() && !IS_BLANK(fen[loc])) {
    if (!IS_DIGIT(fen[loc])) {
      throw InvalidFEN();
    }
    CHECK_LOC();
    move += fen[loc];
    loc++;
  }
  parsed.move = stoi(move);

  return (parsed);
}

} // namespace chessarbiter