Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I have a C++ assignment where I have to use inheritance and pointers to create a

ID: 666446 • Letter: I

Question

I have a C++ assignment where I have to use inheritance and pointers to create a text based adventure game. Most of the game details are up to me. The game must consist of rooms that the player must navigate through. The main reuirement for this assignment is that I create a base class for the rooms and then at least 3 subclasses. In the base class I have to have 4 pointers to be used for navigation through the game. I need someone to show me an example of how I could get this to work so that I can start making the game. Please show me how the base and subclasses might work as well as the pointers.

Explanation / Answer

here it is :

****************a basic idea**********************

class AbstractItem {
public:
virtual const string &getName(GameState &state) = 0;
virtual void doInspect(GameState &state) = 0;
virtual bool canGet(GameState &state) = 0;
virtual bool doGet(GameState &state) = 0;
virtual bool doDrop(GameState &state) = 0;
};

class Room {
public:
ItemCollection items;
const int getId() const { return this->id; }
void setExit(DirectionType type, const int roomId);
bool hasExit(DirectionType type) const;
const int getExit(DirectionType type);
void addItem(AbstractItem *item);

virtual const string &getSortDesc();
virtual const string &getLongDesc();
virtual void display();
virtual bool processActions(GameState &state);
};


int main(int argc, char *argv[]) {
GameState gameState;
Rooms *rooms = new Rooms();

//gameState.currentRoomId = 11;
//gameState.currentRoomId = 3;
gameState.currentRoomId = 1;
while(gameState.deathCount< MAX_DEATH) {
  Room *room = rooms->getRoom(gameState.currentRoomId);
  room->display();
  getUserEntry(gameState);
  debugUserEntry(gameState);
  room->processActions(gameState);
}

return EXIT_SUCCESS;
}

*****************just an example********************
class Vehicle {
public:
     explicit
     Vehicle( int topSpeed )
     : m_topSpeed( topSpeed )
     {}
     int TopSpeed() const {
        return m_topSpeed;
     }

     virtual void Save( std::ostream& ) const = 0;

private:
     int m_topSpeed;
};

class WheeledLandVehicle : public Vehicle {
public:
     WheeledLandVehicle( int topSpeed, int numberOfWheels )
     : Vehicle( topSpeed ), m_numberOfWheels( numberOfWheels )
     {}
     int NumberOfWheels() const {
       return m_numberOfWheels;
     }

     void Save( std::ostream& ) const; // is implicitly virtual

private:
     int m_numberOfWheels;
};

class TrackedLandVehicle : public Vehicle {
public:
    TrackedLandVehicle ( int topSpeed, int numberOfTracks )
    : Vehicle( topSpeed ), m_numberOfTracks ( numberOfTracks )
    {}
    int NumberOfTracks() const {
       return m_numberOfTracks;
    }
    void Save( std::ostream& ) const; // is implicitly virtual

private:
    int m_numberOfTracks;
};

********************example********

enum Dir {
    NORTH, EAST, SOUTH, WEST,
    UP, DOWN,               // easily handles other directions
    NUMDIRECTIONS
};


class Item {
    string        m_descrip;
public:
    void describe() { cout << descrip << endl; }
};


class Room {
    vector<Room*> m_exits; // Vector of size Dir::NUMDIRECTIONS.
                            // m_exits[Dir::NORTH] is either NULL
                            // (no exit in that direction) or
                            // a ptr to the room to the north.
                            // Same idea for other directions.
    vector<Item*> m_items; // items in room
    string        m_descrip;
public:
    Room* getRoom(Dir dir) { return m_exits[dir]; }
    void describe() { cout << descrip << endl; }
};


class Player {
    Room*         m_location;
    vector<Item*> m_items;
    int           m_health;
public:
    void move(Dir dir) {
        Room* room = m_location->getRoom(dir);
        if (!room)
            cout << "You can't go that way. ";
        else
            m_location = room;
    }
};

**************************another example************************

#include <iostream>
#include <conio.h>
#include <time.h>

using namespace std;

int main()
{
srand(time(NULL));

char dir='a';
char combat='a';
bool flag=false;

int x=0,y=0,w=4,attack=0,hp=6;//global variables

cout << "***Welcome to Adventure***";//welcome text
cout << endl;

char board[9][9];//playing board area

for (int i=0; i<9; i++)//array board area
{
  for (int j=0; j<9; j++)
  {
   board[i][j]='.';
  }
}

board[4][4]='C';//draw character symbol
board[7][3]='M';//draw monster symbol
board[4][6]='M';//draw monster symbol
board[8][8]='T';//draw treasure symbol

for (int i=0; i<9; i++)//draw initial board
{
  for (int j=0; j<9; j++)
  {
   cout << board[i][j];
  }
cout << endl;
}

while(dir !=' ')//check for return key
{
cout << endl;

cout << " Your location is " << x << ", " << y;//location text
cout << " Enter direction (n,s,e,w): ";

  dir=getche();

  switch(dir)
  {
  case 'n':
   y++;
   
   if(y>=4)//check for the edge of grid
   {
   y=4;
   }
   
   board[4-y][x+4]='C';//draw character symbol
   board[5-y][x+4]='.';//draw grid symbol

  cout << endl;   

for (int i=0; i<9; i++)//draw grid
{
  for (int j=0; j<9; j++)
  {
   cout << board[i][j];
  }
cout << endl;
}
   break;

  case 's':
   y--;

   if(y<=-4)//check for edge of grid
   {
   y=-4;
   }

   board[4-y][x+4]='C';//draw character symbol
   board[3-y][x+4]='.';//draw grid symbol

  cout << endl;   

for (int i=0; i<9; i++)
{
  for (int j=0; j<9; j++)//draw grid
  {
   cout << board[i][j];
  }
cout << endl;
}

   break;

  case 'e':
   x++;
   
   if(x>=4)//check for edge of grid
   {
   x=4;
   }

   board[4-y][4+x]='C';//draw character symbol
   board[4-y][3+x]='.';//draw grid symbol
cout << endl;   

for (int i=0; i<9; i++)//draw grid
{
  for (int j=0; j<9; j++)
  {
   cout << board[i][j];
  }
cout << endl;
}
   break;

  case 'w':
   x--;
   
   if(x<=-4)//check for edge of grid
   {
   x=-4;
   }

   board[4-y][4+x]='C';//draw character symbol
   board[4-y][5+x]='.';//draw grid symbol
cout << endl;   

for (int i=0; i<9; i++)//draw grid
{
  for (int j=0; j<9; j++)
  {
   cout << board[i][j];
  }
cout << endl;
}
   break;
  }

if(x==2 && y==0 || x==-1 && y==-3)//check for monster symbol
{
cout << endl;
cout << "You encountered a orc!" << endl;//monster encounter text
cout << endl;

while(combat != 'r')//check for fighting monster
{
cout << "Do you want to (f)ight or (r)un away?";//fight or run text
cin >> combat;

if(combat=='f')//fight monster
{
attack=rand()%6+1;//attack die

if(attack<=4)
{
cout << "You hit the orc!" << endl;//hit the orc text
hp--;//decrease hit points
}

if(hp==0)//check for monster death
{
cout << "You kill the orc!" << endl;//kill monster text
break;
}

else if(attack>4)//check for missing the monster
{
cout << "You miss the orc!" << endl;//miss monster text
}
}

else if(combat=='r')//check for running away from monster
{
cout << "You run away!" << endl;//run away text
}
}
}

if(x==4 && y==-4)//check for treasure
{
cout << endl;
cout << "You found a +1 magic sword!" << endl;//treasure found text
flag=true;
}

}

cout << endl;

system ("pause");//pause game

return 0;

}