Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8869379
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T17:28:16+00:00 2026-06-14T17:28:16+00:00

I’m working on an Adventure Game for a C++ project. The goal is basically

  • 0

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;
        }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T17:28:17+00:00Added an answer on June 14, 2026 at 5:28 pm

    You need:

    room* possibleNewRoom = currentRoom->getLinked(exitChoice);
    
    if (possibleNewRoom != 0)
    {
        currentRoom = possibleNewRoom;
    }
    else
    {
        std::cout << "Ouch - you cannot move that way" << std::endl;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
I have just tried to save a simple *.rtf file with some websites and
this is what i have right now Drawing an RSS feed into the php,
I have a small JavaScript validation script that validates inputs based on Regex. I
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.