|
@@ -6,6 +6,12 @@
|
|
|
#include <algorithm>
|
|
|
#include <math.h>
|
|
|
#include <utility>
|
|
|
+#include <stdexcept>
|
|
|
+
|
|
|
+Cell Chessboard::get_cell(Position p)
|
|
|
+{
|
|
|
+ return board[p.posNum-'0'-1][p.posSym-'A'];
|
|
|
+}
|
|
|
|
|
|
bool compare_by_angle(std::pair<int, int> pos1, std::pair<int, int> pos2)
|
|
|
{
|
|
@@ -14,17 +20,49 @@ bool compare_by_angle(std::pair<int, int> pos1, std::pair<int, int> pos2)
|
|
|
return std::atan2(pos1.first, pos1.second) < std::atan2(pos2.first, pos2.second);
|
|
|
}
|
|
|
|
|
|
-vector<Position> Chessboard::get_possible_moves(Position pos, vector<Position> moves)
|
|
|
+vector<Position> Chessboard::get_possible_moves(Figure* fig)
|
|
|
{
|
|
|
+ vector<Position> moves = fig->get_moves();
|
|
|
+ // for(int i = 0; i < moves.size(); i++) std::cout << moves[i] << ';';
|
|
|
+ // std::cout << '\n';
|
|
|
vector<std::pair<int, int>> grouped_moves;
|
|
|
+ vector<Position> possible_moves;
|
|
|
+ Position pos = fig->get_position();
|
|
|
// Нормализуем массив
|
|
|
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));
|
|
|
- // Сортируем по углу
|
|
|
+ // Сортируем по углу и удалённости от фигуры
|
|
|
sort(grouped_moves.begin(), grouped_moves.end(), compare_by_angle);
|
|
|
-
|
|
|
- vector<Position> moves2;
|
|
|
- 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)));
|
|
|
- return moves2;
|
|
|
+
|
|
|
+ // 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<Figure*> figures)
|
|
@@ -38,16 +76,39 @@ Chessboard::Chessboard(vector<Figure*> figures)
|
|
|
board[p.posNum-'0'-1][p.posSym-'A'] = Cell(figures[i]);
|
|
|
}
|
|
|
|
|
|
- vector<Position> grouped_moves = get_possible_moves(figures[4]->get_position(), figures[4]->get_moves());
|
|
|
- for(int i = 0; i < grouped_moves.size(); i++) {
|
|
|
- std::cout << grouped_moves[i] << ';';
|
|
|
- }
|
|
|
- std::cout << std::endl;
|
|
|
+ // vector<Position> 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<Position> 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 = 0; i < 8; i++) {
|
|
|
+ for (int i = 7; i >= 0; i--) {
|
|
|
for(int j = 0; j < 8; j++) {
|
|
|
out << chess.board[i][j];
|
|
|
}
|