#include "Chessboard.h" #include "Figure.h" #include "Position.h" #include #include #include #include #include #include Cell Chessboard::get_cell(Position p) { return board[p.posNum-'0'-1][p.posSym-'A']; } bool compare_by_angle(std::pair pos1, std::pair pos2) { if (std::atan2(pos1.first, pos1.second) == std::atan2(pos2.first, pos2.second)) return bool(std::abs(pos1.first) < std::abs(pos2.first) or std::abs(pos1.second) < std::abs(pos2.second)); return std::atan2(pos1.first, pos1.second) < std::atan2(pos2.first, pos2.second); } vector Chessboard::get_possible_moves(Figure* fig) { vector moves = fig->get_moves(); // for(int i = 0; i < moves.size(); i++) std::cout << moves[i] << ';'; // std::cout << '\n'; vector> grouped_moves; vector possible_moves; Position pos = fig->get_position(); // Нормализуем массив for(int i = 0; i < moves.size(); i++) grouped_moves.push_back(std::pair(moves[i].posSym-pos.posSym, moves[i].posNum-pos.posNum)); // Сортируем по углу и удалённости от фигуры sort(grouped_moves.begin(), grouped_moves.end(), compare_by_angle); // 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) << ';'; // std::cout << '\n'; bool blockedGroup = false; Figure* check_cell = board[grouped_moves[0].second+pos.posNum-'0'-1][grouped_moves[0].first+pos.posSym-'A'].get_figure(); if (check_cell != nullptr) { blockedGroup = true; 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))); } else possible_moves.push_back(Position((char)(grouped_moves[0].first+pos.posSym), (char)(grouped_moves[0].second+pos.posNum))); for(int i = 1; i < grouped_moves.size(); i++) { if (std::atan2(grouped_moves[i-1].first, grouped_moves[i-1].second) != std::atan2(grouped_moves[i].first, grouped_moves[i].second)) { blockedGroup = false; } if (blockedGroup == true) continue; Figure* check_cell = board[grouped_moves[i].second+pos.posNum-'0'-1][grouped_moves[i].first+pos.posSym-'A'].get_figure(); if (check_cell != nullptr) { blockedGroup = true; 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))); continue; } possible_moves.push_back(Position((char)(grouped_moves[i].first+pos.posSym), (char)(grouped_moves[i].second+pos.posNum))); } return possible_moves; } Chessboard::Chessboard(vector figures) { board = new Cell*[8]; for(int i = 0; i < 8; i++) board[i] = new Cell[8]; Position p; for(int i = 0; i < figures.size(); i++) { p = figures[i]->get_position(); board[p.posNum-'0'-1][p.posSym-'A'] = Cell(figures[i]); } // vector grouped_moves; // for(int j = 0; j < figures.size(); j++) { // grouped_moves = get_possible_moves(figures[j]); // std::cout << figures[j]->print() << ' '; // for(int i = 0; i < grouped_moves.size(); i++) { // std::cout << grouped_moves[i] << ';'; // } // std::cout << std::endl; // } } void Chessboard::create_move(Position pos1, Position pos2) { Figure* fig = get_cell(pos1).get_figure(); vector pos_moves = get_possible_moves(fig); // std::cout << pos1 << ' ' << fig->print() << '\n'; // for(int i = 0; i < pos_moves.size(); i++) std::cout << pos_moves[i] << ";"; // std::cout << '\n'; for(int i = 0; i < pos_moves.size(); i++) { if (pos_moves[i].posSym == pos2.posSym and pos_moves[i].posNum == pos2.posNum) { board[pos2.posNum-'0'-1][pos2.posSym-'A'] = Cell(fig); board[pos1.posNum-'0'-1][pos1.posSym-'A'] = Cell(); return; } } throw std::invalid_argument("Impossible move"); }; std::ostream& operator <<(std::ostream& out, Chessboard& chess) { for (int i = 7; i >= 0; i--) { for(int j = 0; j < 8; j++) { out << chess.board[i][j]; } out << '\n'; } return out; }