Bishop.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. #include "Bishop.h"
  2. #include "Position.h"
  3. #include <utility>
  4. #include <stdexcept>
  5. #include <iostream>
  6. Bishop::Bishop(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* Bishop::copy()
  13. {
  14. static Bishop b(position, color);
  15. Figure* f = &b;
  16. return f;
  17. }
  18. vector<Position> Bishop::get_moves()
  19. {
  20. // У ладьи всегда 14 ходов, если нет преград
  21. vector<Position> moves;
  22. for(int i = 1; i < 8; i++) {
  23. for(int j = -1; j < 2; j+=2) {
  24. for(int k = -1; k < 2; k+=2) {
  25. // Не добавляем невозможные ходы
  26. if (not (position.posSym + i*j <= 'H' and position.posNum + i*k <= '8' and position.posSym + i*j >= 'A' and position.posNum + i*k >= '1'))
  27. continue;
  28. // Position создаст ошибку, если создать невозможный ход
  29. Position p;
  30. try {
  31. p = Position((char)(position.posSym+i*j), (char)(position.posNum+i*k));
  32. }
  33. catch (const std::invalid_argument) {
  34. continue;
  35. }
  36. moves.push_back(p);
  37. }
  38. }
  39. }
  40. return moves;
  41. }
  42. const char* Bishop::print()
  43. {
  44. const char* me;
  45. if (color == 'B')
  46. me = "♝";
  47. else
  48. me = "♗";
  49. return me;
  50. }