Chessboard.cpp 4.8 KB

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