Chessboard.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. #include <stdexcept>
  10. Cell Chessboard::get_cell(Position p)
  11. {
  12. return board[p.posNum-'0'-1][p.posSym-'A'];
  13. }
  14. bool compare_by_angle(std::pair<int, int> pos1, std::pair<int, int> pos2)
  15. {
  16. if (std::atan2(pos1.first, pos1.second) == std::atan2(pos2.first, pos2.second))
  17. return bool(std::abs(pos1.first) < std::abs(pos2.first) or std::abs(pos1.second) < std::abs(pos2.second));
  18. return std::atan2(pos1.first, pos1.second) < std::atan2(pos2.first, pos2.second);
  19. }
  20. vector<Position> Chessboard::get_possible_moves(Figure* fig)
  21. {
  22. vector<Position> moves = fig->get_moves();
  23. // for(int i = 0; i < moves.size(); i++) std::cout << moves[i] << ';';
  24. // std::cout << '\n';
  25. vector<std::pair<int, int>> grouped_moves;
  26. vector<Position> possible_moves;
  27. Position pos = fig->get_position();
  28. // Нормализуем массив
  29. 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));
  30. // Сортируем по углу и удалённости от фигуры
  31. sort(grouped_moves.begin(), grouped_moves.end(), compare_by_angle);
  32. // for(int i = 0; i < grouped_moves.size(); i++) std::cout << char(grouped_moves[i].first+pos.posSym) << char(grouped_moves[i].second+pos.posNum) << ';';
  33. // std::cout << '\n';
  34. bool blockedGroup = false;
  35. Figure* check_cell = board[grouped_moves[0].second+pos.posNum-'0'-1][grouped_moves[0].first+pos.posSym-'A'].get_figure();
  36. if (check_cell != nullptr) {
  37. blockedGroup = true;
  38. if (check_cell->get_color() != fig->get_color()) possible_moves.push_back(Position(grouped_moves[0].first+pos.posSym, (char)(grouped_moves[0].second+pos.posNum)));
  39. }
  40. else possible_moves.push_back(Position((char)(grouped_moves[0].first+pos.posSym), (char)(grouped_moves[0].second+pos.posNum)));
  41. for(int i = 1; i < grouped_moves.size(); i++) {
  42. if (std::atan2(grouped_moves[i-1].first, grouped_moves[i-1].second) != std::atan2(grouped_moves[i].first, grouped_moves[i].second)) {
  43. blockedGroup = false;
  44. }
  45. if (blockedGroup == true) continue;
  46. Figure* check_cell = board[grouped_moves[i].second+pos.posNum-'0'-1][grouped_moves[i].first+pos.posSym-'A'].get_figure();
  47. if (check_cell != nullptr) {
  48. blockedGroup = true;
  49. if (check_cell->get_color() != fig->get_color()) possible_moves.push_back(Position(grouped_moves[i].first+pos.posSym, (char)(grouped_moves[i].second+pos.posNum)));
  50. continue;
  51. }
  52. possible_moves.push_back(Position((char)(grouped_moves[i].first+pos.posSym), (char)(grouped_moves[i].second+pos.posNum)));
  53. }
  54. return possible_moves;
  55. }
  56. Chessboard::Chessboard(vector<Figure*> figures)
  57. {
  58. board = new Cell*[8];
  59. for(int i = 0; i < 8; i++) board[i] = new Cell[8];
  60. Position p;
  61. for(int i = 0; i < figures.size(); i++) {
  62. p = figures[i]->get_position();
  63. board[p.posNum-'0'-1][p.posSym-'A'] = Cell(figures[i]);
  64. }
  65. // vector<Position> grouped_moves;
  66. // for(int j = 0; j < figures.size(); j++) {
  67. // grouped_moves = get_possible_moves(figures[j]);
  68. // std::cout << figures[j]->print() << ' ';
  69. // for(int i = 0; i < grouped_moves.size(); i++) {
  70. // std::cout << grouped_moves[i] << ';';
  71. // }
  72. // std::cout << std::endl;
  73. // }
  74. }
  75. void Chessboard::create_move(Position pos1, Position pos2)
  76. {
  77. Figure* fig = get_cell(pos1).get_figure();
  78. vector<Position> pos_moves = get_possible_moves(fig);
  79. // std::cout << pos1 << ' ' << fig->print() << '\n';
  80. // for(int i = 0; i < pos_moves.size(); i++) std::cout << pos_moves[i] << ";";
  81. // std::cout << '\n';
  82. for(int i = 0; i < pos_moves.size(); i++) {
  83. if (pos_moves[i].posSym == pos2.posSym and pos_moves[i].posNum == pos2.posNum) {
  84. board[pos2.posNum-'0'-1][pos2.posSym-'A'] = Cell(fig);
  85. board[pos1.posNum-'0'-1][pos1.posSym-'A'] = Cell();
  86. return;
  87. }
  88. }
  89. throw std::invalid_argument("Impossible move");
  90. };
  91. std::ostream& operator <<(std::ostream& out, Chessboard& chess)
  92. {
  93. for (int i = 7; i >= 0; i--) {
  94. for(int j = 0; j < 8; j++) {
  95. out << chess.board[i][j];
  96. }
  97. out << '\n';
  98. }
  99. return out;
  100. }