ChessGame.cpp 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #include "ChessGame.h"
  2. #include <sstream>
  3. #include <stdexcept>
  4. using namespace chessy;
  5. ChessGame::ChessGame(const std::string& fenString)
  6. {
  7. loadFromFen(fenString);
  8. }
  9. bool ChessGame::isValidMove(const Move& move) const
  10. {
  11. return false;
  12. }
  13. std::vector<Move> ChessGame::getValidMoves(void) const
  14. {
  15. std::vector<Move> ret;
  16. return ret;
  17. }
  18. void ChessGame::loadFromFen(const std::string& fenString)
  19. {
  20. using namespace std;
  21. stringstream tokenizer (fenString);
  22. string board;
  23. string turn;
  24. string castling;
  25. string enPassant;
  26. string halfmoves;
  27. string moveCount;
  28. tokenizer >> board;
  29. tokenizer >> turn;
  30. tokenizer >> castling;
  31. tokenizer >> enPassant;
  32. tokenizer >> halfmoves;
  33. tokenizer >> moveCount;
  34. this->board.setBoard(board);
  35. this->enPassant = Index {enPassant};
  36. if (turn == "w") this->turn = Turn::WHITE;
  37. else if (turn == "b") this->turn = Turn::BLACK;
  38. else throw runtime_error("invalid turn "s + turn);
  39. canCastleQueenSideWhite = false;
  40. canCastleKingSideWhite = false;
  41. canCastleQueenSideBlack = false;
  42. canCastleKingSideBlack = false;
  43. for (auto character : castling) {
  44. switch (character) {
  45. case 'k':
  46. canCastleKingSideWhite = true;
  47. break;
  48. case 'q':
  49. canCastleQueenSideWhite = true;
  50. break;
  51. case 'K':
  52. canCastleKingSideBlack = true;
  53. break;
  54. case 'Q':
  55. canCastleQueenSideBlack = true;
  56. break;
  57. default:
  58. throw runtime_error("invalid castling right: "s + character);
  59. }
  60. }
  61. if (enPassant == "-") enPassant = -1;
  62. try {
  63. reversibleHalfMoves = stoi(halfmoves);
  64. }
  65. catch(...) {
  66. throw runtime_error("invalid number of halfmoves: "s + halfmoves);
  67. }
  68. try {
  69. this->moveCount = stoi(moveCount);
  70. }
  71. catch(...) {
  72. throw runtime_error("invalid number of moves: "s + halfmoves);
  73. }
  74. }
  75. std::string ChessGame::generateFen(void) const
  76. {
  77. using namespace std;
  78. string board = this->board.getFenBoard();
  79. string turn = this->turn == Turn::WHITE ? "w" : "b";
  80. string castlingRights = ""s +
  81. (canCastleKingSideBlack ? "K" : "") +
  82. (canCastleQueenSideBlack ? "Q" : "") +
  83. (canCastleKingSideWhite ? "k" : "") +
  84. (canCastleQueenSideWhite ? "q" : "");
  85. string enPassant;
  86. if (this->enPassant == -1)
  87. enPassant = "-";
  88. else {
  89. auto a = this->enPassant.getName();
  90. enPassant = {a[0], a[1]};
  91. }
  92. string halfmoves = to_string(reversibleHalfMoves);
  93. string mCount = to_string(moveCount);
  94. return board + " " + turn + " " + castlingRights + " " + enPassant + " " + halfmoves +
  95. " " + mCount;
  96. }