Procházet zdrojové kódy

add Figure, Position and Pawn.

jezvgg před 11 měsíci
revize
3df45119ed
10 změnil soubory, kde provedl 118 přidání a 0 odebrání
  1. 22 0
      Figure.h
  2. 11 0
      Makefile
  3. 15 0
      Pawn.cpp
  4. 16 0
      Pawn.h
  5. binární
      Pawn.o
  6. 23 0
      Position.cpp
  7. 14 0
      Position.h
  8. binární
      Position.o
  9. binární
      a.out
  10. 17 0
      test_figures.cpp

+ 22 - 0
Figure.h

@@ -0,0 +1,22 @@
+#ifndef _FIGURE_H
+#define _FIGURE_H
+
+#include "Position.h"
+#include <utility>
+
+using namespace std;
+
+class Figure
+{
+    protected:
+    Position position;
+
+    public:
+
+    // Получить возможные движения фигуры на доске
+    // вернёт moves_count - количество возможных ходов
+    // и список из char[2] - сами ходы (вида A5, B3 и т.п.)
+    virtual std::pair<int, Position*> get_moves() = 0;
+};
+
+#endif

+ 11 - 0
Makefile

@@ -0,0 +1,11 @@
+test_figures: Position.o Pawn.o test_figures.cpp
+	g++ Position.o Pawn.o test_figures.cpp
+
+Pawn.o: Pawn.cpp
+	g++ -c Pawn.cpp
+
+Position.o: Position.cpp
+	g++ -c Position.cpp
+
+clean:
+	rm -rf *.o

+ 15 - 0
Pawn.cpp

@@ -0,0 +1,15 @@
+#include "Pawn.h"
+#include "Position.h"
+#include <utility>
+
+Pawn::Pawn(Position pos) {
+    position = pos;
+}
+
+std::pair<int, Position*> Pawn::get_moves()
+{
+    Position* moves = new Position[1];
+    Position p(position.posSym, position.posNum+1);
+    moves[0] = p;
+    return std::pair<int, Position*>(1, moves);
+};

+ 16 - 0
Pawn.h

@@ -0,0 +1,16 @@
+#ifndef _PAWN_H
+#define _PAWN_H
+
+#include "Figure.h"
+#include "Position.h"
+
+
+class Pawn: public Figure
+{
+    public:
+    Pawn(Position pos);
+
+    virtual std::pair<int, Position*> get_moves();
+};
+
+#endif

binární
Pawn.o


+ 23 - 0
Position.cpp

@@ -0,0 +1,23 @@
+#include "Position.h"
+
+Position::Position()
+{
+    posSym = '0';
+    posSym = '0';
+}
+
+Position::Position(char posS, char posN)
+{
+    if (posS > 'H' or posN > '8' or posS < 'A' or posN < '1')
+        throw "Impossible move";
+    posSym = posS;
+    posNum = posN;
+}
+
+Position::Position(char posS, int posN)
+{
+    if (posS > 'H' or posN > '8' or posS < 'A' or posN < '1')
+        throw "Impossible move";
+    posNum = '0' + posN;
+    posSym = posS;
+}

+ 14 - 0
Position.h

@@ -0,0 +1,14 @@
+#ifndef _POSITION_H
+#define _POSITION_H
+
+class Position
+{
+    public:
+    char posSym, posNum;
+
+    Position();
+    Position(char posSym, char posNum);
+    Position(char posSym, int posNum);
+};
+
+#endif

binární
Position.o


binární
a.out


+ 17 - 0
test_figures.cpp

@@ -0,0 +1,17 @@
+#include "Position.h"
+#include "Figure.h"
+#include "Pawn.h"
+#include <iostream>
+#include <vector>
+
+using namespace std;
+
+int main() {
+    Figure** figures = new Figure*[1];
+    Pawn p = Pawn(Position('A', '1'));
+    figures[0] = &p;
+    for (int i = 0; i < 1; i++) {
+        cout << (figures[i])->get_moves().first << endl;
+    }
+    return 0;
+}