123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126 |
- #include "ChessGame.h"
- #include <sstream>
- #include <stdexcept>
- using namespace chessy;
- ChessGame::ChessGame(const std::string& fenString)
- {
- loadFromFen(fenString);
- }
- bool ChessGame::isValidMove(const Move& move) const
- {
- return false;
- }
- std::vector<Move> ChessGame::getValidMoves(void) const
- {
- std::vector<Move> ret;
- return ret;
- }
- 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") this->turn = Turn::WHITE;
- else if (turn == "b") this->turn = Turn::BLACK;
- else throw runtime_error("invalid turn "s + turn);
- canCastleQueenSideWhite = false;
- canCastleKingSideWhite = false;
- canCastleQueenSideBlack = false;
- canCastleKingSideBlack = false;
- 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 == "-") enPassant = -1;
- 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" : "b";
-
- string castlingRights = ""s +
- (canCastleKingSideBlack ? "K" : "") +
- (canCastleQueenSideBlack ? "Q" : "") +
- (canCastleKingSideWhite ? "k" : "") +
- (canCastleQueenSideWhite ? "q" : "");
- string enPassant;
- if (this->enPassant == -1)
- enPassant = "-";
- else {
- auto a = this->enPassant.getName();
- enPassant = {a[0], a[1]};
- }
- string halfmoves = to_string(reversibleHalfMoves);
- string mCount = to_string(moveCount);
- return board + " " + turn + " " + castlingRights + " " + enPassant + " " + halfmoves +
- " " + mCount;
- }
|