summaryrefslogtreecommitdiff
path: root/src/Fen.hpp
blob: ac344ad1a6c83632d348b81d83df5df2a1f26eef (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
#include <iostream>
#include <sstream>
#include <string>

#define IS_DIGIT(c)                                                            \
  (c == '0' || c == '1' || c == '2' || c == '3' || c == '4' || c == '5' ||     \
   c == '6' || c == '7' || c == '8' || c == '9')
#define IS_BLANK(c) (c == ' ' || c == '\n' || c == '\t' || c == '\r')
#define CHECK_LOC() {if(loc>=(char)fen.size()){throw InvalidFEN();}}

namespace chessarbiter {

class FEN {
public:
  std::string board;
  bool player;
  bool white_castle_short;
  bool white_castle_long;
  bool black_castle_short;
  bool black_castle_long;
  std::string en_passant;
  short halfmove;
  short move;
  FEN()
      : board(""), player(false), white_castle_short(true),
        white_castle_long(true), black_castle_short(true),
        black_castle_long(true), en_passant("-"), halfmove(0), move(1) {}
};

class FENParser {
private:
  static std::string normalize_rank(const std::string &fen_rank);
  static char NextToken(const std::string &fen, char loc);
  static char NextRank(const std::string &fen, char loc);

public:
  /// @brief Parse a FEN from a string (can throw InvalidFEN)
  static FEN Parse(const std::string &fen);
  /// @brief Generate a fen string from the FEN object
  static std::string Serialize(const FEN &fen);
};

struct InvalidFEN : public std::exception {
  const char *what() const throw() { return "No piece found"; }
};
} // namespace chessarbiter