I am in a c++ programming class in my High School, and I am having some trouble with the final I was assigned. We were assigned to make a video game, so I decided to make a text adventure. My code is basically a bunch of if statements telling the program what to display when a user moves from room to room by typing in a different direction. I have 2 "worlds" with 6 rooms to each. The program works for the most part, but after typing in directions 3 or 4 times or repeating a direction (I'm still having trouble figuring out what exactly is the determining factor), the program terminates. I'm not sure if there is a hidden function out there to help me, but I am pretty inexperienced in c++ coding (I only take the course for 18 weeks), so if this could be answered in the simplest terms as possible, that would be great, but if there is some complicated procedure I have to follow, I can. This is the entire code I have so far: (the ShellExecute function is a little Visual Basic map I am experimenting with and also the dialogue is very minimal; I'm planning on polishing the dialogue and room descriptions later. I just wanted to get the code working first)

//Final

#include <iostream.h>
#include <string.h>
#include <windows.h>
#include <stdio.h>
#include <fstream.h>

//map code
void showMap();
//code to begin world 1
void startWorld1();
// code to begin world 2
void startWorld2();


//Intro code
void main()
{
	char start[6] = "";
	

	cout << "Dungeon Master v1.1" << endl;
	cout << " " << endl;
	cout << "Welcome to the Dungeon Master video game" << endl;
	cout << " " << endl;
	cout << "How to play: " << endl;
	cout << "To move, type in North(Up), South(Down), East(Right), West(Left)" << endl;
	cout << "Type 'map' at any time to view a visual map to view your location" << endl;
	cout << " " << endl;
	cout << "Type 'world1' to begin World 1 or 'world2' to begin World 2 (password required)" << endl;
	cin >> start;

	if(stricmp(start, "world1") == 0)
	{
		startWorld1();
	}
	if(stricmp(start, "world2") == 0)
	{
		startWorld2();
	}


}

//Code to launch Visual Basic map
void showMap()
{	
	HWND handle1;
	ShellExecute(handle1, "open", "H:\\test.exe", NULL, NULL, SW_SHOWNORMAL);
	
}

//Code for World 1
void startWorld1()
{ 
	char direction[5] = "";
	int room;
	char item[10] = "";
	int item1;
	int item2;
	int item3;
	int item4;
	int item5;
	int item6;

	cout << "*****************************************************************************" << endl;
	
	cout << "Type 'yes' to enter the dungeon or 'no' to chicken out and flee the dungeon: ";
	cin >> direction;

	if(stricmp(direction, "yes") == 0)
	{
		room = 1;
		cout << "You are in room A1" << endl;
		cout << " " << endl;
		cout << "Your vision returns as you slowly open your eyes. You don't remember much" << endl;
		cout << "except for being kidnapped on the street. You come to your senses as you" << endl;
		cout << "realize that you are in a damp, dark dungeon. You see a door behind" << endl;
		cout << "you and a door to the right of you." << endl;
		cout << " " << endl;
		cout << "Available movements: South and East" << endl;
		cout << " " << endl;
		cout << "What will you do: ";
		cin >> direction;
	}
	if(stricmp(direction, "no") == 0)
	{
		cout << "Session terminated" << endl;
	}
	

	if((room == 1) && (stricmp(direction, "North") == 0))
	{
		room = 1;
		cout << "Invalid command" << endl;
		cout << "What will you do: ";
		cin >> direction;
		cout << " " << endl;
	}
	if((room == 1) && (stricmp(direction, "East") == 0))
	{
		room = 2;
		cout << "You are in room 2" << endl;
		cout << "Available movements: 'West', 'East', and 'South'" << endl;
		cout << "What will you do: ";
		cin >> direction;
		cout << " " << endl;
		
	}
	if((room == 1) && (stricmp(direction, "South") == 0))
	{
		room = 4;
		cout << "You are in room 4" << endl;
		cout << "Available movements: 'North' and 'East'" << endl;
		cout << "What will you do: ";
		cin >> direction;
		cout << " " << endl;
		
	}
	if((room == 1) && (stricmp(direction, "West") == 0))
	{
		room = 1;
		cout << "Invalid command" << endl;
		cout << "What will you do: ";
		cin >> direction;
		cout << " " << endl;
	}


	if((room == 2) && (stricmp(direction, "North") == 0))
	{
		room = 2;
		cout << "Invalid command" << endl;
		cout << "What will you do: ";
		cin >> direction;
		cout << " " << endl;
	}
	if((room == 2) && (stricmp(direction, "East") == 0))
	{
		room = 3;
		cout << "You are in room 3" << endl;
		cout << "Available movements: 'South' and 'West'" << endl;
		cout << "What will you do : ";
		cin >> direction;
		cout << " " << endl;
		
	}
	if((room == 2) && (stricmp(direction, "South") == 0))
	{
		room = 5;
		cout << "You are in room 5" << endl;
		cout << "Available movements: 'North', 'East', and 'West'" << endl;
		cout << "What will you do: ";
		cin >> direction;
		cout << " " << endl;
		
	}
	if((room == 2) && (stricmp(direction, "West") == 0))
	{
		room = 1;
		cout << "You are in room 1" << endl;
		cout << "Available movements: 'South' and 'East'" << endl;
		cout << "What will you do: ";
		cin >> direction;
		cout << " " << endl;
		
	}


	if((room == 3) && (stricmp(direction, "North") == 0))
	{
		room = 3;
		cout << "Invalid command" << endl;
		cout << "What will you do: ";
		cin >> direction;
		cout << " " << endl;
	}
	if((room == 3) && (stricmp(direction, "East") == 0))
	{
		room = 3;
		cout << "Invalid command" << endl;
		cout << "What will you do: ";
		cin >> direction;
		cout << " " << endl;
	}
	if((room == 3) && (stricmp(direction, "South") == 0))
	{
		room = 6;
		cout << "You are in room 6" << endl;
		cout << "Available movements: 'North' and 'West'" << endl;
		cout << "What will you do: ";
		cin >> direction;
		cout << " " << endl;
		
	}
	if((room == 3) && (stricmp(direction, "West") == 0))
	{
		room = 2;
		cout << "You are in room 2" << endl;
		cout << "Available movements: 'East', 'West' and 'South'" << endl;
		cout << "What will you do: ";
		cin >> direction;
		cout << " " << endl;
		
	}


	if((room == 4) && (stricmp(direction, "North") == 0))
	{
		room = 1;
		cout << "You are in room 1" << endl;
		cout << "Available movements: 'South' and 'East'" << endl;
		cout << "What will you do: ";
		cin >> direction;
		cout << " " << endl;
		
	}
	if((room == 4) && (stricmp(direction, "East") == 0))
	{
		room = 5;
		cout << "You are in room 5" << endl;
		cout << "Available movements: 'North', 'East' and 'West'" << endl;
		cout << "What will you do: ";
		cin >> direction;
		cout << " " << endl;
		
	}
	if((room == 4) && (stricmp(direction, "South") == 0))
	{
		room = 4;
		cout << "Invalid command" << endl;
		cout << "What will you do: ";
		cin >> direction;
		cout << " " << endl;
	}
	if((room == 4) && (stricmp(direction, "West") == 0))
	{
		room = 4;
		cout << "Invalid command" << endl;
		cout << "What will you do: ";
		cin >> direction;
		cout << " " << endl;
	}


	if((room == 5) && (stricmp(direction, "North") == 0))
	{
		room = 2;
		cout << "You are in room 2" << endl;
		cout << "Available movements: 'East' and 'West'" << endl;
		cout << "What will you do: ";
		cin >> direction;
		cout << " " << endl;
		
	}
	if((room == 5) && (stricmp(direction, "East") == 0))
	{
		room = 6;
		cout << "You are in room 6" << endl;
		cout << "Available movements: 'West' and 'North'" << endl;
		cout << "What will you do: ";
		cin >> direction;
		cout << " " << endl;
		
	}
	if((room == 5) && (stricmp(direction, "South") == 0))
	{
		room = 5;
		cout << "Invalid command" << endl;
		cout << "What will you do: ";
		cin >> direction;
		cout << " " << endl;
	}
	if((room == 5) && (stricmp(direction, "West") == 0))
	{
		room = 4;
		cout << "You are in room 4" << endl;
		cout << "Available movements: 'North' and 'East'" << endl;
		cout << "What will you do: ";
		cin >> direction;
		cout << " " << endl;
		
	}


	if((room == 6) && (stricmp(direction, "North") == 0))
	{
		room = 3;
		cout << "You are in room 3" << endl;
		cout << "Available movements: 'West' and 'South'" << endl;
		cout << "What will you do: ";
		cin >> direction;
		cout << " " << endl;
		
	}
	if((room == 6) && (stricmp(direction, "East") == 0))
	{
		room = 6;
		cout << "Invalid command" << endl;
		cout << "What will you do: ";
		cin >> direction;
		cout << " " << endl;
	}
	if((room == 6) && (stricmp(direction, "South") == 0))
	{
		room = 6;
		cout << "Invalid command" << endl;
		cout << "What will you do: ";
		cin >> direction;
		cout << " " << endl;
	}
	if((room == 6) && (stricmp(direction, "West") == 0))
	{
		room = 5;
		cout << "You are in room 5" << endl;
		cout << "Available movements: 'North', 'East' and 'West'" << endl;
		cout << "What will you do: ";
		cin >> direction;
		cout << " " << endl;
		
	}

}

		

void startWorld2()
{

}

Recommended Answers

All 4 Replies

What's making your program seem to end early is the fact that you have no loop to keep it playing. You fall from one if clause to the next till you get to the bottom.

With a processing loop, you can have just one input block, then determine what to do with that input.

Perhaps a structure along the lines of:

set initial room
  
while (game_continues)
   
     switch( room )
    {
        describe room, give available directions
    }
  
   get direction
   convert direction string to single char N E S W

   switch(room)
   {
      for curent room, test invalid directions
           update room or give error messaage
    }
//end loop

You can use a set of function calls for each room. I have changed in some segments of your code and written a function room1(); you can do that for all your six rooms such that the program will run until the game ends. You can however keep a quit command so that anyone can leave the game in the middle.

int room1()
{
cout << " Available Directions Are East and South";
cout << "What will you do: ";
		cin >> direction;
		cout << " " << endl;
if(stricmp(direction, "North") == 0)
	{
		cout << "Invalid command" << endl;
		room1();
	}
	else if(stricmp(direction, "East") == 0)
	{
                 room2();
			
	}
	else if(stricmp(direction, "South") == 0)
	{
		room4();		
	}
	else if(stricmp(direction, "West") == 0)
	{
		cout << "Invalid command" << endl;
		room1();
	}
}

And then you can add a quit function

if (strcmp(direction,"Quit")==0)
{
//Quit the Game Code
}

Use functions like toupper from <cctype> so that you pass through case sensitivity.

You can use if , elseif and then define an else so that

all invalid commands are handled by else.

I mean the above function can become

int room1()
{
cout<<"Available Directions are East And South";
cout << "What will you do: ";
		cin >> direction;
		cout << " " << endl;
       if(stricmp(direction, "East") == 0)
	{
                 room2();
			
	}
	else if(stricmp(direction, "South") == 0)
	{
		room4();		
	}
	else
        {
		cout << "Invalid command" << endl;
		room1();
	}
}

Thanks so much for the help Sky Diploma. It worked perfectly.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.