#include "fen.hpp" #include "board.hpp" #include #include #include #include #include #include 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 = 7; 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) { 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 < 0) { 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; UpdateHelpers(g); }