Browse Source

add moves

jezv 11 months ago
parent
commit
9e173ec635
6 changed files with 123 additions and 26 deletions
  1. 2 0
      Cell.h
  2. 73 12
      Chessboard.cpp
  3. 2 1
      Chessboard.h
  4. 1 0
      Figure.h
  5. 3 1
      Position.cpp
  6. 42 12
      test_chessboard.cpp

+ 2 - 0
Cell.h

@@ -10,5 +10,7 @@ class Cell
     Cell();
     Cell(Figure* figure);
 
+    Figure* get_figure() {return figure;}
+
     friend std::ostream& operator <<(std::ostream& out, Cell& cell);
 };

+ 73 - 12
Chessboard.cpp

@@ -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];            
         }

+ 2 - 1
Chessboard.h

@@ -14,7 +14,8 @@ class Chessboard
     Cell** board;
     vector<std::pair<Position, Position>> moves;
 
-    vector<Position> get_possible_moves(Position pos, vector<Position> moves);
+    vector<Position> get_possible_moves(Figure* fig);
+    Cell get_cell(Position pos);
 
     public:
     Chessboard(vector<Figure*>);

+ 1 - 0
Figure.h

@@ -23,6 +23,7 @@ class Figure
     virtual const char* print() = 0;
 
     Position get_position(){return position;}
+    char get_color(){return color;}
 };
 
 #endif

+ 3 - 1
Position.cpp

@@ -18,9 +18,11 @@ Position::Position(char posS, char posN)
 
 Position::Position(char posS, int posN)
 {
+    posN += '0';
+    std::cout << posS << ' ' << posN << std::endl;
     if (posS > 'H' or posN > '8' or posS < 'A' or posN < '1')
         throw std::invalid_argument("Impossible move");
-    posNum = '0' + posN;
+    posNum = posN;
     posSym = posS;
 }
 

+ 42 - 12
test_chessboard.cpp

@@ -14,21 +14,51 @@ using namespace std;
 
 int main() {
     vector<Figure*> figures;
-    Pawn p(Position('A', '1'), 'W');
-    figures.push_back(&p);
-    Horse h(Position('B', '1'), 'B');
-    figures.push_back(&h);
-    Bishop b(Position('C', '1'), 'W');
-    figures.push_back(&b);
-    Rook r(Position('D', '1'), 'W');
-    figures.push_back(&r);
-    Queen q(Position('E', '1'), 'W');
-    figures.push_back(&q);
-    King k(Position('F', '1'), 'B');
-    figures.push_back(&k);
+    Horse bh(Position('A', '7'), 'B');
+    King bk(Position('C', '8'), 'B');
+    Bishop bb(Position('E', '8'), 'B');
+    Queen bq(Position('B', '6'), 'B');
+    Rook br(Position('C', '6'), 'B');
+
+    Pawn wp(Position('F', '3'), 'W');
+    King wk(Position('D', '2'), 'W');
+
+    figures.push_back(&bh);
+    figures.push_back(&bk);
+    figures.push_back(&bb);
+    figures.push_back(&bq);
+    figures.push_back(&br);
+
+    figures.push_back(&wp);
+    figures.push_back(&wk);
 
     Chessboard board(figures);
 
     cout << board;
+
+    board.create_move(Position('E', '8'), Position('G', '6'));
+
+    cout << board;
+
+    // vector<Figure*> figures2;
+    // King jbk(Position('E', '8'), 'B');
+    // Rook jbr(Position('A', '8'), 'B');
+    // Rook jbr2(Position('H', '8'), 'B');
+
+    // King jwk(Position('E', '1'), 'W');
+    // Rook jwr(Position('A', '1'), 'W');
+    // Rook jwr2(Position('H', '1'), 'W');
+
+    // figures2.push_back(&jbk);
+    // figures2.push_back(&jbr);
+    // figures2.push_back(&jbr2);
+
+    // figures2.push_back(&jwk);
+    // figures2.push_back(&jwr);
+    // figures2.push_back(&jwr2);
+
+    // Chessboard board2(figures2);
+
+    // cout << board2;
     return 0;
 }