Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
213
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.67 KB | None | 0 0
  1. //
  2. //
  3. //
  4. #include <iostream>
  5. #include <cstdlib>
  6. using namespace std;
  7.  
  8. const int EMPTY = 0;
  9. const int PLAYER = 1;
  10. const int EXIT = 2;
  11. const int WALL = 3;
  12.  
  13. class Space
  14. {
  15. private:
  16.   int x;
  17.   int y;
  18.   int type;
  19. public:
  20.   Space(){}
  21.   Space(int a, int b)
  22.   {
  23.     x = a;
  24.     y = b;
  25.     type = EMPTY;
  26.   }
  27.   int setX(int a){x = a;}
  28.   int setY(int b){y = b;}
  29.   int setType(int t){type = t;}
  30.   int getX(){return x;}
  31.   int getY(){return y;}
  32.   int getType(){return type;}
  33.   virtual string print(){return "O";}
  34. };
  35.  
  36. class Player : public Space
  37. {
  38. public:
  39.   Player(){}
  40.   Player(int a, int b)
  41.   {
  42.     setX(a);
  43.     setY(b);
  44.     setType(PLAYER);
  45.   }
  46.   string print(){return "P";}
  47. };
  48.  
  49. class Exit : public Space
  50. {
  51. public:
  52.   Exit(){}
  53.   Exit(int a, int b)
  54.   {
  55.     setX(a);
  56.     setY(b);
  57.     setType(EXIT);
  58.   }
  59.   string print(){return "X";}
  60. };
  61.  
  62. class Grid
  63. {
  64. private:
  65.   int height;
  66.   int width;
  67.   //Space** spaces;
  68. public:
  69.   Grid(){}
  70.   Grid(int h, int w)
  71.   {
  72.     height = h;
  73.     width = w;
  74.   }
  75.   ~Grid()
  76.   {
  77.     for(int i = 0; i < width; ++i)
  78.     {
  79.       delete[] spaces[i];
  80.     }
  81.     delete[] spaces;
  82.   }
  83.   int getHeight(){return height;}
  84.   int getWidth(){return width;}
  85.   Space** spaces;
  86.   void generateSpaces()
  87.   {
  88.     spaces = new Space*[width];
  89.     for(int i = 0; i < height; ++i)
  90.     {
  91.       spaces[i] = new Space[height];
  92.     }
  93.   }
  94.   void generatePlayer()
  95.   {
  96.     int pRandX = rand()%height;
  97.     int pRandY = rand()%width;
  98.     /*cout << pRandX << " " << pRandY;
  99.     Player p;
  100.     p.setX(pRandX);
  101.     p.setY(pRandY);
  102.     cout << p.print() << endl;
  103.     spaces[pRandX][pRandY] = p;
  104.     cout << spaces[pRandX][pRandY].print() << endl;*/
  105.     spaces[pRandX][pRandY] = new Player(pRandX, pRandY);
  106.   }
  107.   void generateExit()
  108.   {
  109.     int eRandX = rand()%height;
  110.     int eRandY = rand()%width;
  111.     int inc = 0;
  112.     while(spaces[eRandX][eRandY].getType() != 0)
  113.     {
  114.       eRandX = rand()%height;
  115.       eRandY = rand()%width;
  116.       ++inc;
  117.       if(inc>100)
  118.       {
  119.         cout << "Error: No free space for exit.";
  120.         break;
  121.       }
  122.     }
  123.     spaces[eRandX][eRandY] = new Exit(eRandX, eRandY);
  124.   }
  125.   void printGrid()
  126.   {
  127.     cout << "Initialize spaces of the grid!\n";
  128.     for(int i = 0; i < height; ++i)
  129.     {
  130.       for(int j = 0; j < width; ++j)
  131.       {
  132.         cout << spaces[i][j].print() << " ";
  133.       }
  134.       cout << endl;
  135.     }
  136.   }
  137.   void navigatePlayer()
  138.   {
  139.  
  140.   }
  141. };
  142.  
  143. int main()
  144. {
  145.   int x;
  146.   int y;
  147.   cout << "Enter size of array (x by y) where x is height and y is width: ";
  148.   cin >> x;
  149.   cin >> y;
  150.   Grid g(x, y);
  151.   g.generateSpaces();
  152.   g.generatePlayer();
  153.   g.generateExit();
  154.   g.printGrid();
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement