ChessGame.cpp 3.4 KB

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