#include "ChessGame.h" #include #include using namespace chessy; ChessGame::ChessGame(void) { board.resetBoard(); } ChessGame::ChessGame(const std::string& fenString) { loadFromFen(fenString); } bool ChessGame::isValidMove(const Move& move) const { return false; } std::vector ChessGame::getValidMoves(void) const { std::vector ret; return ret; } void ChessGame::move(Move move) { board.move(move); if (turn == Turn::BLACK) { moveCount++; } turn = turn == Turn::WHITE ? Turn::BLACK : Turn::WHITE; } void ChessGame::loadFromFen(const std::string& fenString) { using namespace std; stringstream tokenizer (fenString); string board; string turn; string castling; string enPassant; string halfmoves; string moveCount; tokenizer >> board; tokenizer >> turn; tokenizer >> castling; tokenizer >> enPassant; tokenizer >> halfmoves; tokenizer >> moveCount; this->board.setBoard(board); this->enPassant = Index {enPassant}; if (turn == "w"s) this->turn = Turn::WHITE; else if (turn == "b"s) this->turn = Turn::BLACK; else throw runtime_error("invalid turn "s + turn); canCastleQueenSideWhite = false; canCastleKingSideWhite = false; canCastleQueenSideBlack = false; canCastleKingSideBlack = false; if (castling != "-") for (auto character : castling) { switch (character) { case 'k': canCastleKingSideWhite = true; break; case 'q': canCastleQueenSideWhite = true; break; case 'K': canCastleKingSideBlack = true; break; case 'Q': canCastleQueenSideBlack = true; break; default: throw runtime_error("invalid castling right: "s + character); } } if (enPassant == "-"s) { this->enPassant = -1; } else { if (enPassant.size() != 2 || (enPassant[1] != '3' && enPassant[1] != '6')) throw runtime_error("invalid en passant string: "s + enPassant); this->enPassant = Index{ enPassant }; } try { reversibleHalfMoves = stoi(halfmoves); } catch(...) { throw runtime_error("invalid number of halfmoves: "s + halfmoves); } try { this->moveCount = stoi(moveCount); } catch(...) { throw runtime_error("invalid number of moves: "s + halfmoves); } } std::string ChessGame::generateFen(void) const { using namespace std; string board = this->board.getFenBoard(); string turn = this->turn == Turn::WHITE ? "w"s : "b"s; string castlingRights = (canCastleKingSideBlack ? "K"s : ""s) + (canCastleQueenSideBlack ? "Q"s : ""s) + (canCastleKingSideWhite ? "k"s : ""s) + (canCastleQueenSideWhite ? "q"s : ""s); if (castlingRights.empty()) castlingRights = "-"s; string enPassant; if (this->enPassant == -1) { enPassant = "-"s; } else { enPassant = this->enPassant.getName(); } string halfmoves = to_string(reversibleHalfMoves); string mCount = to_string(moveCount); return board + " " + turn + " " + castlingRights + " " + enPassant + " " + halfmoves + " " + mCount; }