Browse Source

add basic figures

jezv 11 months ago
parent
commit
71ced37665
12 changed files with 254 additions and 10 deletions
  1. 48 0
      .vscode/settings.json
  2. 36 0
      Bishop.cpp
  3. 16 0
      Bishop.h
  4. 31 0
      Horse.cpp
  5. 16 0
      Horse.h
  6. 11 2
      Makefile
  7. 10 1
      Pawn.cpp
  8. 11 3
      Position.cpp
  9. 3 0
      Position.h
  10. 37 0
      Rook.cpp
  11. 16 0
      Rook.h
  12. 19 4
      test_figures.cpp

+ 48 - 0
.vscode/settings.json

@@ -0,0 +1,48 @@
+{
+    "files.associations": {
+        "array": "cpp",
+        "atomic": "cpp",
+        "bit": "cpp",
+        "*.tcc": "cpp",
+        "cctype": "cpp",
+        "clocale": "cpp",
+        "cmath": "cpp",
+        "compare": "cpp",
+        "concepts": "cpp",
+        "cstdarg": "cpp",
+        "cstddef": "cpp",
+        "cstdint": "cpp",
+        "cstdio": "cpp",
+        "cstdlib": "cpp",
+        "cwchar": "cpp",
+        "cwctype": "cpp",
+        "deque": "cpp",
+        "string": "cpp",
+        "unordered_map": "cpp",
+        "vector": "cpp",
+        "exception": "cpp",
+        "algorithm": "cpp",
+        "functional": "cpp",
+        "iterator": "cpp",
+        "memory": "cpp",
+        "memory_resource": "cpp",
+        "numeric": "cpp",
+        "random": "cpp",
+        "string_view": "cpp",
+        "system_error": "cpp",
+        "tuple": "cpp",
+        "type_traits": "cpp",
+        "utility": "cpp",
+        "initializer_list": "cpp",
+        "iosfwd": "cpp",
+        "iostream": "cpp",
+        "istream": "cpp",
+        "limits": "cpp",
+        "new": "cpp",
+        "numbers": "cpp",
+        "ostream": "cpp",
+        "stdexcept": "cpp",
+        "streambuf": "cpp",
+        "typeinfo": "cpp"
+    }
+}

+ 36 - 0
Bishop.cpp

@@ -0,0 +1,36 @@
+#include "Bishop.h"
+#include "Position.h"
+#include <utility>
+#include <stdexcept>
+
+
+Bishop::Bishop(Position pos) {
+    position = pos;
+}
+
+std::pair<int, Position*> Bishop::get_moves()
+{
+    // У ладьи всегда 14 ходов, если нет преград
+    Position* moves = new Position[14];
+    int moveNum = 0;
+    for(int i = 1; i < 8; i++) {
+        for(int j = -1; j < 2; j+=2) {
+            for(int k = -1; k < 2; k+=2) {
+                // Не добавляем невозможные ходы
+                if (not (position.posSym + i*j <= 'H' and position.posNum + i*k <= '8' and position.posSym + i*j >= 'A' and position.posNum + i*k >= '1')) 
+                    continue;
+                // Position создаст ошибку, если создать невозможный ход
+                Position p;
+                try {
+                    p = Position((char)(position.posSym+i*j), (char)(position.posNum+i*k));
+                }
+                catch (const std::invalid_argument) {
+                    p = Position();
+                }
+                moves[moveNum] = p;
+                moveNum++;
+            }
+        }
+    }
+    return std::pair<int, Position*>(14, moves);
+};

+ 16 - 0
Bishop.h

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

+ 31 - 0
Horse.cpp

@@ -0,0 +1,31 @@
+#include "Horse.h"
+#include "Position.h"
+#include <utility>
+#include <stdexcept>
+#include <cmath>
+
+
+Horse::Horse(Position pos) {
+    position = pos;
+}
+
+std::pair<int, Position*> Horse::get_moves()
+{
+    Position* moves = new Position[8];
+    int moveNum = 0;
+    for(int i = -2; i < 3; i++) {
+        for(int j = -2; j < 3; j++) {
+            if(std::abs(i) == std::abs(j) or i == 0 or j == 0) continue;
+            Position p;
+            try {
+                p = Position((char)(position.posSym+i), (char)(position.posNum+j));
+            }
+            catch (const std::invalid_argument) {
+                p = Position();
+            }
+            moves[moveNum] = p;
+            moveNum++;
+        }
+    }
+    return std::pair<int, Position*>(8, moves);
+};

+ 16 - 0
Horse.h

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

+ 11 - 2
Makefile

@@ -1,5 +1,14 @@
-test_figures: Position.o Pawn.o test_figures.cpp
-	g++ Position.o Pawn.o test_figures.cpp
+test_figures: Position.o Pawn.o Horse.o Bishop.o Rook.o test_figures.cpp
+	g++ Position.o Pawn.o Horse.o Bishop.o Rook.o test_figures.cpp
+
+Rook.o: Rook.cpp
+	g++ -c Rook.cpp
+
+Bishop.o: Bishop.cpp
+	g++ -c Bishop.cpp
+
+Horse.o: Horse.cpp
+	g++ -c Horse.cpp
 
 Pawn.o: Pawn.cpp
 	g++ -c Pawn.cpp

+ 10 - 1
Pawn.cpp

@@ -1,6 +1,9 @@
 #include "Pawn.h"
 #include "Position.h"
 #include <utility>
+#include <stdexcept>
+#include <iostream>
+
 
 Pawn::Pawn(Position pos) {
     position = pos;
@@ -9,7 +12,13 @@ Pawn::Pawn(Position pos) {
 std::pair<int, Position*> Pawn::get_moves()
 {
     Position* moves = new Position[1];
-    Position p(position.posSym, position.posNum+1);
+    Position p;
+    try {
+        p = Position(position.posSym, (char)(position.posNum+1));
+    }
+    catch (const std::invalid_argument) {
+        p = Position();
+    }
     moves[0] = p;
     return std::pair<int, Position*>(1, moves);
 };

+ 11 - 3
Position.cpp

@@ -1,15 +1,17 @@
 #include "Position.h"
+#include <stdexcept>
+#include <iostream>
 
 Position::Position()
 {
     posSym = '0';
-    posSym = '0';
+    posNum = '0';
 }
 
 Position::Position(char posS, char posN)
 {
     if (posS > 'H' or posN > '8' or posS < 'A' or posN < '1')
-        throw "Impossible move";
+        throw std::invalid_argument("Impossible move");
     posSym = posS;
     posNum = posN;
 }
@@ -17,7 +19,13 @@ Position::Position(char posS, char posN)
 Position::Position(char posS, int posN)
 {
     if (posS > 'H' or posN > '8' or posS < 'A' or posN < '1')
-        throw "Impossible move";
+        throw std::invalid_argument("Impossible move");
     posNum = '0' + posN;
     posSym = posS;
+}
+
+std::ostream& operator <<(std::ostream& out, Position& p)
+{
+    out << p.posSym << p.posNum;
+    return out;
 }

+ 3 - 0
Position.h

@@ -1,5 +1,6 @@
 #ifndef _POSITION_H
 #define _POSITION_H
+#include <iostream>
 
 class Position
 {
@@ -9,6 +10,8 @@ class Position
     Position();
     Position(char posSym, char posNum);
     Position(char posSym, int posNum);
+
+    friend std::ostream& operator <<(std::ostream& out, Position& p);
 };
 
 #endif

+ 37 - 0
Rook.cpp

@@ -0,0 +1,37 @@
+#include "Rook.h"
+#include "Position.h"
+#include <utility>
+#include <stdexcept>
+#include <iostream>
+
+
+Rook::Rook(Position pos) {
+    position = pos;
+}
+
+std::pair<int, Position*> Rook::get_moves()
+{
+    Position* moves = new Position[14];
+    int moveNum = 0;
+    for (int i = 1; i < 9; i++) {
+        Position p;
+        Position p2;
+        try {
+            if ('0'+i != position.posNum) p2 = Position(position.posSym, (char)('0'+i));
+            if ('0'+i+16 != position.posSym) p = Position((char)('0'+i+16), position.posNum);
+        }
+        catch (const std::invalid_argument) {
+            p = Position();
+            p2 = Position();
+        }
+        if ('0'+i+16 != position.posSym) {
+            moves[moveNum] = p;
+            moveNum++;
+        }
+        if ('0'+i != position.posNum) {
+            moves[moveNum] = p2;
+            moveNum++;
+        }
+    }
+    return std::pair<int, Position*>(14, moves);
+};

+ 16 - 0
Rook.h

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

+ 19 - 4
test_figures.cpp

@@ -1,17 +1,32 @@
 #include "Position.h"
 #include "Figure.h"
 #include "Pawn.h"
+#include "Horse.h"
+#include "Bishop.h"
+#include "Rook.h"
 #include <iostream>
 #include <vector>
 
 using namespace std;
 
 int main() {
-    Figure** figures = new Figure*[1];
-    Pawn p = Pawn(Position('A', '1'));
+    Figure** figures = new Figure*[4];
+    Pawn p(Position('A', '1'));
     figures[0] = &p;
-    for (int i = 0; i < 1; i++) {
-        cout << (figures[i])->get_moves().first << endl;
+    Horse h(Position('B', '1'));
+    figures[1] = &h;
+    Bishop b(Position('C', '1'));
+    figures[2] = &b;
+    Rook r(Position('D', '1'));
+    figures[3] = &r;
+    for (int i = 0; i < 4; i++) {
+        int movesCount = (figures[i])->get_moves().first;
+        Position* moves = (figures[i])->get_moves().second;
+        cout << movesCount << ' ';
+        for(int j = 0; j < movesCount; j++) {
+            cout << moves[j] << ';';
+        }
+        cout << endl;
     }
     return 0;
 }