Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- //
- //
- #include <iostream>
- #include <cstdlib>
- using namespace std;
- const int EMPTY = 0;
- const int PLAYER = 1;
- const int EXIT = 2;
- const int WALL = 3;
- class Space
- {
- private:
- int x;
- int y;
- int type;
- public:
- Space(){}
- Space(int a, int b)
- {
- x = a;
- y = b;
- type = EMPTY;
- }
- int setX(int a){x = a;}
- int setY(int b){y = b;}
- int setType(int t){type = t;}
- int getX(){return x;}
- int getY(){return y;}
- int getType(){return type;}
- virtual string print(){return "O";}
- };
- class Player : public Space
- {
- public:
- Player(){}
- Player(int a, int b)
- {
- setX(a);
- setY(b);
- setType(PLAYER);
- }
- string print(){return "P";}
- };
- class Exit : public Space
- {
- public:
- Exit(){}
- Exit(int a, int b)
- {
- setX(a);
- setY(b);
- setType(EXIT);
- }
- string print(){return "X";}
- };
- class Grid
- {
- private:
- int height;
- int width;
- //Space** spaces;
- public:
- Grid(){}
- Grid(int h, int w)
- {
- height = h;
- width = w;
- }
- ~Grid()
- {
- for(int i = 0; i < width; ++i)
- {
- delete[] spaces[i];
- }
- delete[] spaces;
- }
- int getHeight(){return height;}
- int getWidth(){return width;}
- Space** spaces;
- void generateSpaces()
- {
- spaces = new Space*[width];
- for(int i = 0; i < height; ++i)
- {
- spaces[i] = new Space[height];
- }
- }
- void generatePlayer()
- {
- int pRandX = rand()%height;
- int pRandY = rand()%width;
- /*cout << pRandX << " " << pRandY;
- Player p;
- p.setX(pRandX);
- p.setY(pRandY);
- cout << p.print() << endl;
- spaces[pRandX][pRandY] = p;
- cout << spaces[pRandX][pRandY].print() << endl;*/
- spaces[pRandX][pRandY] = new Player(pRandX, pRandY);
- }
- void generateExit()
- {
- int eRandX = rand()%height;
- int eRandY = rand()%width;
- int inc = 0;
- while(spaces[eRandX][eRandY].getType() != 0)
- {
- eRandX = rand()%height;
- eRandY = rand()%width;
- ++inc;
- if(inc>100)
- {
- cout << "Error: No free space for exit.";
- break;
- }
- }
- spaces[eRandX][eRandY] = new Exit(eRandX, eRandY);
- }
- void printGrid()
- {
- cout << "Initialize spaces of the grid!\n";
- for(int i = 0; i < height; ++i)
- {
- for(int j = 0; j < width; ++j)
- {
- cout << spaces[i][j].print() << " ";
- }
- cout << endl;
- }
- }
- void navigatePlayer()
- {
- }
- };
- int main()
- {
- int x;
- int y;
- cout << "Enter size of array (x by y) where x is height and y is width: ";
- cin >> x;
- cin >> y;
- Grid g(x, y);
- g.generateSpaces();
- g.generatePlayer();
- g.generateExit();
- g.printGrid();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement