#include #include #include using namespace std; class MapT { public: MapT(size_t x=10, size_t y=10) : width{x}, height{y} , data(width, vector(height, '.')) { cout << "The map is " << data.size() << " by " << data[0].size() << endl; } size_t Width() const { return width; } size_t Height() const { return height; } char Pos(size_t x, size_t y) const { if (x < width and y < height) { return data[x][y]; } else { throw out_of_range("Pos must be in range"); } } void Set(size_t x, size_t y, char thing) { if (x < width and y < height) { data[x][y] = thing; } else { throw out_of_range("Pos must be in range"); } } private: size_t width, height; vector> data; }; class CritterT { public: CritterT(MapT & map, size_t xpos=0, size_t ypos=0, char r ='@'): theMap{map}, x{xpos}, y{ypos}, rep{r} { Fix(); cout << "Currently the map has a " << theMap.Pos(x,y) << endl; theMap.Set(x,y,rep); } void Up() { theMap.Set(x,y,'.'); ++y; Fix(); theMap.Set(x,y,rep); } void Down() { theMap.Set(x,y,'.'); if (y == 0) { y = theMap.Height()-1; } else { --y; } theMap.Set(x,y,rep); } void Left() { theMap.Set(x,y,'.'); if (x == 0) { x = theMap.Width()-1; } else { --x; } theMap.Set(x,y,rep); } void Right() { theMap.Set(x,y,'.'); ++x; Fix(); theMap.Set(x,y,rep); } private: void Fix() { x = x % theMap.Width(); y = y % theMap.Height(); } MapT & theMap; size_t x, y; char rep; }; void PrintMap(const MapT & map); int main() { MapT world(5,7); char c; bool done {false}; CritterT me(world, 5, 8); PrintMap(world); while( not done) { cout << "Action (wasdq) :"; cin >> c; switch(c) { case 'w': me.Up(); break; case 'a': me.Left(); break; case 's': me.Down(); break; case 'd': me.Right(); break; case 'q': done = true; break; } cout << endl; PrintMap(world); } } void PrintMap(const MapT & map) { size_t x,y; for(y = map.Height(); y > 0; --y) { for(x = 0; x < map.Width(); ++ x) { cout << map.Pos(x,y-1); } cout << endl; } }