Chessboard.cpp 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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 get_icell(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. // std::cout << "\n\n\n1111111111\n\n\n";
  23. vector<Position> moves = fig->get_moves();
  24. vector<std::pair<int, int>> grouped_moves;
  25. vector<Position> possible_moves;
  26. Position pos = fig->get_position();
  27. // Нормализуем массив
  28. 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));
  29. // Сортируем по углу и удалённости от фигуры
  30. sort(grouped_moves.begin(), grouped_moves.end(), compare_by_angle);
  31. bool blockedGroup = false;
  32. // Проверяем первую фигуру
  33. Figure* check_cell;
  34. check_cell = board[grouped_moves[0].second+pos.posNum-'0'-1][grouped_moves[0].first+pos.posSym-'A'].get_figure();
  35. if (check_cell != nullptr) {
  36. blockedGroup = true;
  37. 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)));
  38. }
  39. else possible_moves.push_back(Position((char)(grouped_moves[0].first+pos.posSym), (char)(grouped_moves[0].second+pos.posNum)));
  40. // Проверяем остальные
  41. for(int i = 1; i < grouped_moves.size(); i++) {
  42. // Разделение на разные группы по углу относ фигуры
  43. if (std::atan2(grouped_moves[i-1].first, grouped_moves[i-1].second) != std::atan2(grouped_moves[i].first, grouped_moves[i].second)) {
  44. blockedGroup = false;
  45. }
  46. // Пропускаем позиции перед которыми препядствие
  47. if (blockedGroup == true) continue;
  48. check_cell = board[grouped_moves[i].second+pos.posNum-'0'-1][grouped_moves[i].first+pos.posSym-'A'].get_figure();
  49. // Добавление
  50. if (check_cell != nullptr) {
  51. blockedGroup = true;
  52. 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)));
  53. continue;
  54. }
  55. possible_moves.push_back(Position((char)(grouped_moves[i].first+pos.posSym), (char)(grouped_moves[i].second+pos.posNum)));
  56. }
  57. if (fig->print()[2] == "♟"[2] or fig->print()[2] == "♙"[2]) {
  58. // std::cout << "pawn\n";
  59. // std::cout << (char)(possible_moves[0].posSym+1) << (char)(possible_moves[0].posNum) << '\n';
  60. try {
  61. // std::cout << possible_moves[0].posSym+1 << possible_moves[0].posNum << '\n';
  62. if (possible_moves.size() > 0 and
  63. get_cell(Position(possible_moves[0].posSym+1, possible_moves[0].posNum)).get_figure() != nullptr
  64. and get_cell(Position(possible_moves[0].posSym+1, possible_moves[0].posNum)).get_figure()->get_color() != fig->get_color())
  65. possible_moves.push_back(Position(possible_moves[0].posSym+1, possible_moves[0].posNum));
  66. }
  67. catch( std::invalid_argument ) {}
  68. // std::cout << (char)(possible_moves[0].posSym+1) << (char)(possible_moves[0].posNum) << '\n';
  69. try {
  70. // std::cout << possible_moves[0].posSym+1 << possible_moves[0].posNum << '\n';
  71. if (possible_moves.size() > 0 and
  72. get_cell(Position(possible_moves[0].posSym-1, possible_moves[0].posNum)).get_figure() != nullptr
  73. and get_cell(Position(possible_moves[0].posSym-1, possible_moves[0].posNum)).get_figure()->get_color() != fig->get_color())
  74. possible_moves.push_back(Position(possible_moves[0].posSym-1, possible_moves[0].posNum));
  75. }
  76. catch( std::invalid_argument ) {}
  77. if (possible_moves.size() > 0 and get_cell(possible_moves[0]).get_figure() != nullptr ) {
  78. vector<Position> possible_swap;
  79. for(int i = 1; i < possible_moves.size(); i++) {
  80. possible_swap.push_back(possible_moves[i]);
  81. }
  82. possible_moves = possible_swap;
  83. }
  84. }
  85. return possible_moves;
  86. }
  87. Chessboard::Chessboard()
  88. {
  89. board = new Cell*[8];
  90. for(int i = 0; i < 8; i++) board[i] = new Cell[8];
  91. }
  92. Chessboard::Chessboard(vector<Figure*> figures)
  93. {
  94. board = new Cell*[8];
  95. for(int i = 0; i < 8; i++) board[i] = new Cell[8];
  96. Position p;
  97. for(int i = 0; i < figures.size(); i++) {
  98. p = figures[i]->get_position();
  99. board[p.posNum-'0'-1][p.posSym-'A'] = Cell(figures[i]);
  100. }
  101. }
  102. Chessboard Chessboard::copy() {
  103. vector<Figure*> my_figures;
  104. for(int i = 0; i < 8; i++) {
  105. for(int j = 0; j < 8; j++) {
  106. if (get_icell(i,j).get_figure() != nullptr)
  107. my_figures.push_back(get_icell(i,j).get_figure()->copy());
  108. }
  109. }
  110. return Chessboard(my_figures);
  111. }
  112. void Chessboard::create_move(Position pos1, Position pos2)
  113. {
  114. Figure* fig = get_cell(pos1).get_figure();
  115. vector<Position> pos_moves = get_possible_moves(fig);
  116. // std::cout << pos1 << ' ' << fig->print() << '\n';
  117. // for(int i = 0; i < pos_moves.size(); i++) std::cout << pos_moves[i] << ";";
  118. // std::cout << '\n';
  119. for(int i = 0; i < pos_moves.size(); i++) {
  120. if (pos_moves[i].posSym == pos2.posSym and pos_moves[i].posNum == pos2.posNum) {
  121. fig->set_position(Position((char)pos2.posSym, pos2.posNum));
  122. board[pos2.posNum-'0'-1][pos2.posSym-'A'] = Cell(fig);
  123. board[pos1.posNum-'0'-1][pos1.posSym-'A'] = Cell();
  124. return;
  125. }
  126. }
  127. throw std::invalid_argument("Impossible move");
  128. }
  129. Cell Chessboard::get_icell(int index, int jindex)
  130. {
  131. if (not (index >= 0 and index < 8 and jindex >= 0 and jindex < 8))
  132. throw std::invalid_argument("Trying to get figure out of board");
  133. return board[index][jindex];
  134. }
  135. std::ostream& operator <<(std::ostream& out, Chessboard& chess)
  136. {
  137. vector<Figure*> figures;
  138. for (int i = 7; i >= 0; i--) {
  139. for(int j = 0; j < 8; j++) {
  140. out << chess.board[i][j];
  141. if (chess.board[i][j].get_figure() != nullptr) {
  142. figures.push_back(chess.board[i][j].get_figure());
  143. }
  144. }
  145. out << '\n';
  146. }
  147. // Дебаг ходов
  148. // vector<Position> grouped_moves;
  149. // for(int j = 0; j < figures.size(); j++) {
  150. // grouped_moves = chess.get_possible_moves(figures[j]);
  151. // std::cout << figures[j]->print() << ' ' << figures[j]->get_position().posSym << figures[j]->get_position().posNum << ' ';
  152. // for(int i = 0; i < grouped_moves.size(); i++) {
  153. // std::cout << grouped_moves[i] << ';';
  154. // }
  155. // std::cout << std::endl;
  156. // }
  157. return out;
  158. }