Horse.cpp 1.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. #include "Horse.h"
  2. #include "Position.h"
  3. #include <utility>
  4. #include <stdexcept>
  5. #include <cmath>
  6. Horse::Horse(Position pos, char c) {
  7. if (c != 'B' and c != 'W')
  8. throw std::invalid_argument("Impossible color of figure!");
  9. position = pos;
  10. color = c;
  11. }
  12. vector<Position> Horse::get_moves()
  13. {
  14. vector<Position> moves;
  15. for(int i = -2; i < 3; i++) {
  16. for(int j = -2; j < 3; j++) {
  17. if(std::abs(i) == std::abs(j) or i == 0 or j == 0) continue;
  18. Position p;
  19. // Position выдаст ошибку если указать невозможную позицию
  20. try {
  21. p = Position((char)(position.posSym+i), (char)(position.posNum+j));
  22. }
  23. catch (const std::invalid_argument) {
  24. continue;
  25. }
  26. moves.push_back(p);
  27. }
  28. }
  29. return moves;
  30. }
  31. const char* Horse::print()
  32. {
  33. const char* me;
  34. if (color == 'B')
  35. me = "♞";
  36. else
  37. me = "♘";
  38. return me;
  39. }