1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
|
#include <iostream>
#include <string.h>
#include "../../mystring/mystr.hpp"
using namespace std;
class Entity {
public:
char interpretation;
bool walkable;
bool hasdirection;
virtual String erronwalk() const = 0;
Entity(char interpret = 0, bool wkable = false, bool hasdir = false): interpretation{interpret}, walkable{wkable}, hasdirection{hasdir} {}
};
class Empty: public Entity {
public:
Empty(): Entity('w', true) {}
String erronwalk() const override{
return "possible";
}
};
class myKara: public Entity {
public:
myKara(char dir = 'R'): Entity('K', false) {}
String erronwalk() const override{
return "cannot walk over myKara";
}
};
class KaraWorld {
void _preset(size_t, size_t);
size_t _rows{};
size_t _columns{};
Entity **_map = (Entity **) new Entity*[_rows];
public:
KaraWorld(): _map{(Entity **) new Entity*[10]}, _rows{10}, _columns{10} { _preset(10, 10); };
KaraWorld(size_t row_s, size_t column_s): _map{(Entity **) new Entity*[row_s]}, _rows{row_s}, _columns{column_s} { _preset(row_s, column_s); }
KaraWorld(size_t row_s): _map{(Entity **) new Entity*[row_s]}, _rows{row_s}, _columns{row_s} { _preset(row_s, row_s); }
KaraWorld(const KaraWorld& cp): _map{cp._map}, _columns{cp._columns}, _rows{cp._rows} {}
KaraWorld(KaraWorld&& other) {std::swap(_map, other._map); std::swap(_rows, other._rows); std::swap(_columns, other._columns);}
~KaraWorld() {for (size_t i = 0; i < _rows; i++) {delete[] _map[i];} delete[] _map;}
const size_t rows() const {return _rows;}
const size_t columns() const {return _columns;}
Entity **map() {return _map; }
KaraWorld& put(size_t x, size_t y, Entity& obj);
};
void KaraWorld::_preset(size_t r, size_t c) {
for (size_t i = 0; i < r; i++) {
_map[i] = new Empty[c];
}
}
KaraWorld& KaraWorld::put(size_t x, size_t y, Entity& obj) {
_map[x][y] = obj;
return *this;
}
ostream& operator<<(ostream& os, KaraWorld& k) {
for (size_t m = 0; m < k.columns() * 2 + 1; m++)
os << "_";
os << "\n";
for (size_t i = 0; i < k.rows(); i++) {
for (size_t j = 0; j < k.columns(); j++) {
os << "|";
os << k.map()[i][j].interpretation << ((j + 1) == k.columns() ? "|\n":"");
}
}
for (size_t m = 0; m < k.columns() * 2 + 1; m++)
os << "_";
os << "\n";
return os;
}
| |