123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657 |
- #include "Chessboard.h"
- #include "Figure.h"
- #include "Position.h"
- #include <iostream>
- #include <vector>
- #include <algorithm>
- #include <math.h>
- #include <utility>
- bool compare_by_angle(std::pair<int, int> pos1, std::pair<int, int> 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<Position> Chessboard::get_possible_moves(Position pos, vector<Position> moves)
- {
- vector<std::pair<int, int>> grouped_moves;
- // Нормализуем массив
- 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;
- }
- Chessboard::Chessboard(vector<Figure*> 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<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;
- }
- std::ostream& operator <<(std::ostream& out, Chessboard& chess)
- {
- for (int i = 0; i < 8; i++) {
- for(int j = 0; j < 8; j++) {
- out << chess.board[i][j];
- }
- out << '\n';
- }
- return out;
- }
|