Chessboard.cpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. #include "Chessboard.h"
  2. #include "Figure.h"
  3. #include "Position.h"
  4. #include <iostream>
  5. #include <vector>
  6. #include <algorithm>
  7. #include <math.h>
  8. #include <utility>
  9. bool compare_by_angle(std::pair<int, int> pos1, std::pair<int, int> pos2)
  10. {
  11. if (std::atan2(pos1.first, pos1.second) == std::atan2(pos2.first, pos2.second))
  12. return bool(std::abs(pos1.first) < std::abs(pos2.first) or std::abs(pos1.second) < std::abs(pos2.second));
  13. return std::atan2(pos1.first, pos1.second) < std::atan2(pos2.first, pos2.second);
  14. }
  15. vector<Position> Chessboard::get_possible_moves(Position pos, vector<Position> moves)
  16. {
  17. vector<std::pair<int, int>> grouped_moves;
  18. // Нормализуем массив
  19. for(int i = 0; i < moves.size(); i++) grouped_moves.push_back(std::pair<int, int>(moves[i].posSym-pos.posSym, moves[i].posNum-pos.posNum));
  20. // Сортируем по углу
  21. sort(grouped_moves.begin(), grouped_moves.end(), compare_by_angle);
  22. vector<Position> moves2;
  23. for(int i = 0; i < grouped_moves.size(); i++) moves2.push_back(Position(grouped_moves[i].first+pos.posSym,(char)(grouped_moves[i].second+pos.posNum)));
  24. return moves2;
  25. }
  26. Chessboard::Chessboard(vector<Figure*> figures)
  27. {
  28. board = new Cell*[8];
  29. for(int i = 0; i < 8; i++) board[i] = new Cell[8];
  30. Position p;
  31. for(int i = 0; i < figures.size(); i++) {
  32. p = figures[i]->get_position();
  33. board[p.posNum-'0'-1][p.posSym-'A'] = Cell(figures[i]);
  34. }
  35. vector<Position> grouped_moves = get_possible_moves(figures[4]->get_position(), figures[4]->get_moves());
  36. for(int i = 0; i < grouped_moves.size(); i++) {
  37. std::cout << grouped_moves[i] << ';';
  38. }
  39. std::cout << std::endl;
  40. }
  41. std::ostream& operator <<(std::ostream& out, Chessboard& chess)
  42. {
  43. for (int i = 0; i < 8; i++) {
  44. for(int j = 0; j < 8; j++) {
  45. out << chess.board[i][j];
  46. }
  47. out << '\n';
  48. }
  49. return out;
  50. }