Procházet zdrojové kódy

start doing get_possible_moves

jezv před 11 měsíci
rodič
revize
635b8f5018
2 změnil soubory, kde provedl 32 přidání a 1 odebrání
  1. 31 0
      Chessboard.cpp
  2. 1 1
      Chessboard.h

+ 31 - 0
Chessboard.cpp

@@ -1,6 +1,31 @@
 #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)
 {
@@ -12,6 +37,12 @@ Chessboard::Chessboard(vector<Figure*> figures)
         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)

+ 1 - 1
Chessboard.h

@@ -14,7 +14,7 @@ class Chessboard
     Cell** board;
     vector<std::pair<Position, Position>> moves;
 
-    bool possible_move(Position pos1, Position pos2, Figure* figure);
+    vector<Position> get_possible_moves(Position pos, vector<Position> moves);
 
     public:
     Chessboard(vector<Figure*>);