Procházet zdrojové kódy

add Chessboard,cell and print chessboard

jezv před 11 měsíci
rodič
revize
ff43b9086a
7 změnil soubory, kde provedl 124 přidání a 3 odebrání
  1. 17 0
      Cell.cpp
  2. 14 0
      Cell.h
  3. 26 0
      Chessboard.cpp
  4. 22 3
      Chessboard.h
  5. 2 0
      Figure.h
  6. 9 0
      Makefile
  7. 34 0
      test_chessboard.cpp

+ 17 - 0
Cell.cpp

@@ -0,0 +1,17 @@
+#include "Cell.h"
+#include <iostream>
+
+Cell::Cell()
+{}
+
+Cell::Cell(Figure* fig)
+{  
+    figure = fig;
+}
+
+std::ostream& operator <<(std::ostream& out, Cell& cell)
+{
+    if (cell.figure == nullptr) out << ". ";
+    else out << cell.figure->print() << ' ';
+    return out;
+}

+ 14 - 0
Cell.h

@@ -0,0 +1,14 @@
+#include "Figure.h"
+#include <iostream>
+
+class Cell
+{
+    private:
+    Figure* figure = nullptr;
+
+    public:
+    Cell();
+    Cell(Figure* figure);
+
+    friend std::ostream& operator <<(std::ostream& out, Cell& cell);
+};

+ 26 - 0
Chessboard.cpp

@@ -0,0 +1,26 @@
+#include "Chessboard.h"
+#include "Figure.h"
+#include <iostream>
+
+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]);
+    }
+}
+
+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;
+}

+ 22 - 3
Chessboard.h

@@ -1,8 +1,27 @@
+#ifndef _CHESSBOARD_H
+#define _CHESSBOARD_H
 
-
+#include <vector>
+#include "Figure.h"
+#include "Position.h"
+#include "Cell.h"
+#include <utility>
+#include <iostream>
 
 class Chessboard
 {
+    private:
+    Cell** board;
+    vector<std::pair<Position, Position>> moves;
+
+    bool possible_move(Position pos1, Position pos2, Figure* figure);
+
     public:
-    Chessboard();
-};
+    Chessboard(vector<Figure*>);
+
+    void create_move(Position pos1, Position pos2);
+
+    friend std::ostream& operator <<(std::ostream& out, Chessboard& chess);
+};
+
+#endif

+ 2 - 0
Figure.h

@@ -21,6 +21,8 @@ class Figure
     virtual vector<Position> get_moves() = 0;
 
     virtual const char* print() = 0;
+
+    Position get_position(){return position;}
 };
 
 #endif

+ 9 - 0
Makefile

@@ -1,6 +1,15 @@
 test_figures: Position.o Pawn.o Horse.o Bishop.o Rook.o King.o Queen.o test_figures.cpp
 	g++ Position.o Pawn.o Horse.o Bishop.o Rook.o King.o Queen.o test_figures.cpp
 
+test_board: Position.o Pawn.o Horse.o Bishop.o Rook.o King.o Queen.o Cell.o Chessboard.o test_chessboard.cpp
+	g++ Position.o Pawn.o Horse.o Bishop.o Rook.o King.o Queen.o Cell.o Chessboard.o test_chessboard.cpp
+
+Chessboard.o: Chessboard.cpp
+	g++ -c Chessboard.cpp
+
+Cell.o: Cell.cpp
+	g++ -c Cell.cpp
+
 Queen.o: Queen.cpp
 	g++ -c Queen.cpp
 

+ 34 - 0
test_chessboard.cpp

@@ -0,0 +1,34 @@
+#include "Chessboard.h"
+#include "Position.h"
+#include "Figure.h"
+#include "Pawn.h"
+#include "Horse.h"
+#include "Bishop.h"
+#include "Rook.h"
+#include "Queen.h"
+#include "King.h"
+#include <iostream>
+#include <vector>
+
+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);
+
+    Chessboard board(figures);
+
+    cout << board;
+    return 0;
+}