Question

I'm working on an Adventure Game for a C++ project. The goal is basically to have a bunch of rooms, defined by a class. Then link them all together using a map. I have everything working except checking to see if there is no room and making sure that input isn't taken for an empty room. For instance it would say "There is nothing there" and then repromt the user for a direction to move. Currently my program just crashes if there is no room in a direction and that direction is selected. I have it setup currently to at least make sure a valid direction (North, South, East or West) is entered, but it doesn't check if that direction is even available. Anyone know a good way to do that?

main.cpp

    #include <iostream>
    #include <string>
    #include <map>

    #include "room.h"

    using namespace std;

    int main()
    {
        /* ---------- Variable Declerations ---------- */
        string exitChoice;

        /* ---------- Room Intialization ---------- */
        room *kitchen = new room("Kitchen", "You are in the Kitchen. Pots and pans dangle above your head as you look across the room.");
        room *diningRoom = new room("Dining Room", "You are in the Dining Room. You see a large table in the center of the room complete with a set of chairs. It seems no one has ate here in quite som time.");
        room *garage = new room("Garage", "You are in the Garage. There are tools spread across the concerte floor complete with a Jeep Grand Cherokee on jack stands.");
        room *masterBed = new room("Master Bed Room", "You are in the Bed Room. A large Master Bed greets you as you walk into the room. You can see a large master bath as weel in the backround");
        room *hallway = new room("Hallway", "You are in the Hallway. A large set of stairs leads to the second floor, complete with a set to the basement. You also see a grand front door.");
        room *familyRoom = new room("Family Room", "You are in the Family Room. You see a dark leather couch in front of you as well as a brand new LCD TV. It aappears South Park is on TV.");
        room *bathRoom = new room("Bath Room", "You are in the Bath Room. A small room containing just a toilet is in front of you.");
        room *frontLawn = new room("Front Lawn", "You are in the Front Lawn. You are on a pathway and observe freshly cut grass as well as several trees scattered across the yard.");
        room *backLawn = new room("Back Lawn", "You are in the Back Lawn. You see 'Spot' running around chasing a tennis ball, as well as his dog house. A large wooden fence keeps him in the yard.");

        /* ----------Room Links---------- */

        /* Kitchen */
        kitchen->link(diningRoom, "North");
        kitchen->link(garage, "East");
        kitchen->link(masterBed, "South");
        kitchen->link(hallway, "West");

        /* Dining Room */
        diningRoom->link(kitchen, "South");
        diningRoom->link(familyRoom, "West");

        /* Master Bed Room */
        masterBed->link(kitchen, "North");
        masterBed->link(bathRoom, "West");

        /* Garage */
        garage->link(kitchen, "West");
        garage->link(backLawn, "East");

        /* Back Lawn */
        backLawn->link(garage, "West");

        /* Family Room */
        familyRoom->link(diningRoom, "East");
        familyRoom->link(hallway, "South");

        /* Hallway */
        hallway->link(familyRoom, "North");
        hallway->link(kitchen, "East");
        hallway->link(bathRoom, "South");
        hallway->link(frontLawn, "West");

        /* Front Lawn */
        frontLawn->link(hallway, "East");

        /* Bath Room */
        bathRoom->link(hallway, "North");
        bathRoom->link(masterBed, "East");

        /* ----------Gameplay---------- */
        room *currentRoom = kitchen;

        while (exitChoice != "quit")
        {
            currentRoom->printRoom();
            cout << endl;

            currentRoom->printLiked();

            cout << "Which exit? (Or 'quit'):";
            cin >> exitChoice;

            if(exitChoice != "quit" && exitChoice != "North" && exitChoice != "South" && exitChoice != "East" && exitChoice != "West")
            {
                cout << "Invalid Entry!" << endl;
                cout << "Which exit? (Or 'quit'):";
                cin >> exitChoice;
            }

            cout << "You move to the " << exitChoice << "..." << endl;
            currentRoom->getLinked(exitChoice);

            currentRoom = currentRoom->getLinked(exitChoice);
        }
    }

room.h

#ifndef ROOM_H
#define ROOM_H

using namespace std;

#include <string>
#include <iostream>
#include <map>

class room
{
    private:
        string name;
        string description;

    public:

    /* Constructor Prototypes */
    room(string, string);
    room(string);

    /* Get Name */
    string getName()
    {
        return name;
    }

    /* Get Description */
    string getDescription()
    {
        return description;
    }

    /* Print Room Information */
    void room :: printRoom()
    {
        cout << "--" << getName() << "--" << endl;
        cout << getDescription() << endl;
    }

    /* Map */
    map<string, room*> exits;

    /* Link Function*/
    void link(room *room, string direction)
    {
        exits[direction] = room;
    }

    /* Print Linked Rooms */
    void printLiked()
    {
        map<string, room*> :: iterator it;

        cout << endl;

        for(it = exits.begin(); it != exits.end(); ++it)
        {
            cout << "Exit: ";
            cout << it->first << " (" << it->second->getName() << ")" << endl;

        }

        cout << endl;
    }

    /* Get linked room */
    room* getLinked(string direction)
    {
        map<string, room*> :: iterator it;

        it = exits.find(direction);

        if(it != exits.end())
        {
            return it->second;
        }
        else
        {
            return NULL;
        }


    }





};

#endif

room.cpp

 #include <iostream>
    #include <string>
    #include <map>

    using namespace std;

    #include "room.h"

        /* Constructor with Name and Description */
        room :: room(string _name, string _description)
        {
            name = _name;
            description = _description;
        }


        /* Contrsuctor with Name */
        room :: room(string _name)
        {
            name = _name;
        }

new main.cpp snipet

        room* possibleNewRoom = currentRoom->getLinked(exitChoice);

        if (possibleNewRoom != 0)
        {
            currentRoom = possibleNewRoom;

            cout << "You move to the " << exitChoice << "..." << endl;
            currentRoom->getLinked(exitChoice);

            currentRoom = currentRoom->getLinked(exitChoice);
        }
        else
        {
            cout << "There are no exits in that direction." << endl;
        }
Was it helpful?

Solution

You need:

room* possibleNewRoom = currentRoom->getLinked(exitChoice);

if (possibleNewRoom != 0)
{
    currentRoom = possibleNewRoom;
}
else
{
    std::cout << "Ouch - you cannot move that way" << std::endl;
}

OTHER TIPS

The most common way to do this is to introduce directional members into your Room class:

class Room {
    // ...
    Room *north, *south, *east, *west;
    // ...
    Room() : north(NULL), south(NULL), east(NULL), west(NULL)
    { /* ... */ }
};

You then connect rooms to each other like this:

Room* kitchen = new Room // ...
Room* hallway = new Room // ...
kitchen->north = hallway;
hallway->south = kitchen;

When you try to travel to a direction, you simply check whether the member for that direction is NULL:

// Player wants to go north.
if (this->north == NULL) {
    // Tell the player that's not possible.
} else {
    // OK, we can go to this->north.
}

This setup will allow you to set up more advanced connections. For example in cases where going north and then south again should not take you to where you started (maybe it's a magic portal or something among those lines.)

You can take a few design ideas on how to design this by looking at TADS (Text Adventure Development System).

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top