Horse.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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. Figure* Horse::copy() {
  13. static Horse h(position, color);
  14. Figure* f = &h;
  15. return f;
  16. }
  17. vector<Position> Horse::get_moves()
  18. {
  19. vector<Position> moves;
  20. for(int i = -2; i < 3; i++) {
  21. for(int j = -2; j < 3; j++) {
  22. if(std::abs(i) == std::abs(j) or i == 0 or j == 0) continue;
  23. Position p;
  24. // Position выдаст ошибку если указать невозможную позицию
  25. try {
  26. p = Position((char)(position.posSym+i), (char)(position.posNum+j));
  27. }
  28. catch (const std::invalid_argument) {
  29. continue;
  30. }
  31. moves.push_back(p);
  32. }
  33. }
  34. return moves;
  35. }
  36. const char* Horse::print()
  37. {
  38. const char* me;
  39. if (color == 'B')
  40. me = "♞";
  41. else
  42. me = "♘";
  43. return me;
  44. }