Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
2.00/5 (2 votes)
See more:
I am programming a game in C++, the problem is, it isn't taking me to each room or giving me the room descriptions and also when I choose to take an item, it won't take the item.

I have been staring at this code and my eyes hurt. Please help.
C++
#include <iostream>
#include <string>
using namespace std;


int iFlag=0x00000000;
//Define Doors(nibble0)

#define DOOR_NORTH   0x00000001
#define DOOR_SOUTH   0x00000002	
#define DOOR_WEST    0x00000004
#define DOOR_EAST    0x00000008


//Define Treasure Items(nibble1)
#define BOOTS       0x00000010
#define COMPASS     0x00000020
#define ACCESS_CODE 0x00000040
#define ARMY_SUIT   0x00000080

//Define harmless Creatures

#define MOUSE       0x00000100
#define BUTTERCUP   0x00000200
#define MOCKING_JAY 0x00000400
#define LADY        0x00000800

	
//Define dangerous creatures
#define TRACKER_JACKER 0x00001000
#define SPIDERS        0x00002000
#define GORG           0x00004000
#define MONKEY         0x00008000

//Define Dangerous

#define DEADLY      0x00010000
#define DANGEROUS   0x00020000
#define CAUTION     0x00040000
#define FRIENDLY    0x00080000

//Prototypes
void BuildRooms(unsigned int rooms[][4]);
void DescribeRoomContents(int roomDesc, int row, int col);
void DescribeRoom00_00();
void DescribeRoom00_01();
void DescribeRoom00_02();
void DescribeRoom00_03();
void DescribeRoom00_04();
void DescribeRoom01_00();
void DescribeRoom02_00();
void DescribeRoom03_00();
void DescribeRoom04_00();
void DescribeRoom01_01();
void DescribeRoom02_02();
void DescribeRoom03_03();
void DescribeRoom04_04();
void DescribeRoom01_04();
void DescribeRoom01_05();
void DescribeRoom01_06();
int TakeItem(unsigned int rooms[4][4], int row, int col, string Object);

int main()

{

string cmdVerb;  // string object to hold players command verb
string cmdObj;    //string object to hold players command object
int iRow;
int iCol;
int iNumRows;
int iNumCols;
int idx;
int bQuitGame;
int iPlayer=0;
int endRow=2;
int endCol=2;



//array rooms variable assignments
unsigned int panem[4][4];


//Make all array initially zero

for(int row=0; row<4;row++)
for(int col=0; col<4;col++)
		panem[row][col]=0;


BuildRooms(panem);



// initialize the location variables

iRow=0;
iCol=0;
iNumRows=4;
iNumCols=4;
idx=0;


//-------------------
//end of room list
//----------------------------


//Print directions





	cout<<"HOW TO PLAY"<<endl;
	cout<<"                                   "<<endl;//line of space
	cout<<"                                   "<<endl;//line of space
	cout<<"                                   "<<endl;// line of space
	cout<<" It is your job to locate 4 treasure items located within the walls"<<endl;//part1
	cout<<"of District 13. Navigate through all sixteen rooms by using cardinal direction."<<endl;
	cout<<"                                       "<<endl;//line of space
	cout<<" Each room obtains a door, that door is found by correctly giving the cardinal"<<endl;//part2
	cout<<" direction to enter that are. For example, to get into the first room you would"<<endl;
	cout<<"choose North, to get into room 2 you would choose Southeast and so forth."<<endl;
	cout<<"                                       "<<endl;//line of space
	cout<<" Some rooms will have creatures, some good some bad, avoid getting too close"<<endl;//part3
	cout<<"to any of the genetically morphed animals, they are bad news. Some rooms will"<<endl;
	cout<<"also contain useless items. You need to disregard those items and only choose"<<endl;
	cout<<"the treasure items."<<endl;
	cout<<"                                       "<<endl;//line of space
	cout<<" After successfully gathering all 4 items,you need"<<endl;//part4
	cout<<"to find your way back to the elevator and escape District 13 before"<<endl;
	cout<<"President Coin catches you. Good luck, and remember to hurry!"<<endl;

	
	//--------------
	
	//get all users command
   //---------------------


	bQuitGame=false;
	while(!bQuitGame)


	{



	cout<<"\nWhat do you want to do?";   //Prompt User for input
	cin>>cmdVerb>>cmdObj;
	cout<<endl;  // skip

	// Converting to uppercase
		idx=0;

	while((unsigned)idx<cmdverb.length())>
		
 { 
	 cmdVerb[idx]=toupper(cmdVerb[idx]);
	 idx++;
 }
	idx=0;

	while((unsigned)idx<cmdobj.length())>
 { 
	 cmdObj[idx]=toupper(cmdObj[idx]);
	 idx++;
 }

	

	//*****************************************
 //start game loop//
	//****************************************




	cout<<"Command is:"<<cmdVerb<<"    "<<cmdObj<<endl;

	if(cmdVerb=="GO")
	{
		cout<<"command verb is GO"<<endl;
	

		 if(cmdObj=="NORTH")
			 // can we go that way
		 {
			 if(panem[iRow][iCol]&DOOR_NORTH)
			 {
				 iRow--;  //Move up a row
				 DescribeRoomContents(panem[iRow][iCol], iRow,iCol);
			 }
			 else 
				 cout<<"There is no door to the NORTH"<<endl;
		 }
	
	
		else if(cmdObj=="SOUTH")
			
		{
			// can we go that way
		
			if(panem[iRow][iCol]&DOOR_SOUTH)
			 {
				 iRow++;  //Move up a row
				 DescribeRoomContents(panem[iRow][iCol], iRow,iCol);
			 }
	
			else
			
			
				cout<<"There is no door to the SOUTH"<<endl;
			
	
	
	
			
			cout<<"Direction is South"<<endl;
		}
		
		else if(cmdObj=="WEST")
		{	
			// can we go that way
		
			if(panem[iRow][iCol]&DOOR_WEST)
			 {
				 iCol--;  //Move up a row
				 DescribeRoomContents(panem[iRow][iCol], iRow,iCol);
			 }
			
			 else 
				 cout<<"There is no door to the WEST"<<endl;
	
		
			
		
			cout<<"Direction is West"<<endl;
		}
		else if (cmdObj=="EAST")
		{
			// can we go that way
			 if(panem[iRow][iCol]&DOOR_EAST)
			 {
				 iCol++;  //Move up a row
				 DescribeRoomContents(panem[iRow][iCol], iRow,iCol);
			 }
			 else 
				 cout<<"There is no door to the EAST"<<endl;
	
			
		
		
			cout<<"Direction is East"<<endl;
		}
		else 
		{
			cout<<" I don't understand GO"<<cmdObj<<endl;
		}
		if((endRow==iRow)&& (endCol==iCol)) // are we there yet
		{
			//Check to see if player has all the items
			if((iPlayer & ACCESS_CODE)&&
			  (iPlayer & COMPASS)&&
			  (iPlayer & BOOTS)&&
			  (iPlayer & ARMY_SUIT))
			{
				//Yes, so tell the player
				cout<<"\n\nYou now have all the items to go to the elevator and leave PANEM.\n\n";
				cout<<"Congratulations Player, you have now obtained all four"<<endl;
				cout<<"treasure items; the Access Code, Boots, Compass and Army Suit."<<endl;
				cout<<"The game is now over."<<endl;
				bQuitGame=true; // set boolean flag to end
			}
		
	}
	else if   (cmdVerb=="TAKE")
	{
		if (cmdObj=="ACCESS_CODE")
		{
			int takeVal= TakeItem(panem,iRow,iCol, cmdObj);
			if(takeVal)
			{
				iPlayer|= takeVal;
				cout<<" You are now carrying the " <<cmdObj<<endl;
			}
			else
				cout<<" You cannot take the "<<cmdObj<<" from this location."<<endl;
		}

			if (cmdObj=="BOOTS")
		{
			int takeVal= TakeItem(panem,iRow,iCol, cmdObj);
			if(takeVal)
			{
				iPlayer|= takeVal;
				cout<<" You are now carrying the " <<cmdObj<<endl;
			}
			else
				cout<<" You cannot take the "<<cmdObj<<" from this location."<<endl;
		}
			if (cmdObj=="ARMY_SUIT")
		{
			int takeVal= TakeItem(panem,iRow,iCol, cmdObj);
			if(takeVal)
			{
				iPlayer|= takeVal;
				cout<<" You are now carrying the " <<cmdObj<<endl;
			}
			else
				cout<<" You cannot take the "<<cmdObj<<" from this location."<<endl;
		}
			if (cmdObj=="COMPASS")
		{
			int takeVal= TakeItem(panem,iRow,iCol, cmdObj);
			if(takeVal)
			{
				iPlayer|= takeVal;
				cout<<" You are now carrying the " <<cmdObj<<endl;
			}
			else
				cout<<" You cannot take the "<<cmdObj<<" from this location."<<endl;
		}
	}
	else if (cmdVerb=="QUIT")
	{
		if (cmdObj=="GAME")
		{
			cout<<"Command object is game"<<endl;
		}
	 else 
	 {
		 cout<<"I don't understand"<<cmdVerb<<""<<cmdObj<<endl;
	 }
	}
	else
	{
		cout<<"I don't understand"<<endl;
	}
	
	}
}



	
	

    //-------------------------
	//call rooms
	//--------------------------



/*
DescribeRoom00_03();
cout<<endl;

DescribeRoom00_04();
cout<<endl;

DescribeRoom01_01();
cout<<endl;



DescribeRoom03_03();
cout<<endl;

DescribeRoom04_04();
cout<<endl;

DescribeRoom01_00();
cout<<endl;

DescribeRoom02_00();
cout<<endl;

DescribeRoom03_00();
cout<<endl;

DescribeRoom04_00();
cout<<endl;

DescribeRoom01_05();
cout<<endl;

DescribeRoom01_06();
cout<<endl;

DescribeRoom01_04();
cout<<endl;
*/

//-----------------------------------
//includes all rooms here
//-----------------------------------


	return 0;
}

void DescribeRoom00_04()
{
 cout<<"This is the cafeteria. Lunch is not being served at this time, the only thing in the"<<endl;
 cout<<"room besides you is an empty cafeteria tray. The room is "<<endl;
cout<<" very large and lined with multiple tables."<<endl;
}

void DescribeRoom01_00()
{
cout<<" You have stumbled into the clothing closet. The room"<<endl;
cout<<"is small and lined with grey suits that are the required"<<endl;
cout<<"attire of all who live in District 13"<<endl;
}

void DescribeRoom02_00()
{
cout<<"This is President Coin’s room. Normally this room is off limits but"<<endl;
cout<<" President Coin appears to have left the door open."<<endl;
cout<<"Her dog Lady is lounging on the floor."<<endl;
}

void DescribeRoom03_00()
{
cout<<"This is the weapons room. The room is huge and filled"<<endl;
cout<<"with guns and knives and killing machines. You notice a long"<<endl;
cout<<"line if spiders eerily crawling across the barrel of a gun."<<endl;
}

void DescribeRoom04_00()
{
cout<<"This is the storage room, the room is mostly empty except for an empty box."<<endl;
cout<<" The room is regular in sized. Made of stone and of course no windows."<<endl;
cout<<"There is a small mouse that runs across your toes."<<endl;
}

void DescribeRoom01_01()
{
cout<<"You have arrived in the Holding Room where the disobedient are sent"<<endl;
cout<<"It appears, since the room is empty, that everyone is on their best behavior"<<endl;
}

void DescribeRoom02_02()

{
cout<<"This room seems to have come out of thin air, it is large and filled with piles"<<endl;
cout<<"of papers. In the corner you spot a Monkey, best not stay here long"<<endl;
}

void DescribeRoom00_03()
{
cout<<"Now you are in the History room. The room Is small and filled with"<<endl;
cout<<"desks for students. There are history books lined in the bookshelf and"<<endl;
cout<<" apparently some tracker jackers have made their way in the room with you"<<endl;
}

void DescribeRoom03_03()

{
cout<<"This is the command center. The room has dozens of computer"<<endl;
cout<<"screens monitoring District 13. A broken compass lays in front of you"<<endl;
}

void DescribeRoom04_04()
{
cout<<"This is the English room, lined with student desks and tons of dictionarys"<<endl;
cout<<"along with Englsih books. Jabber jays flock in the room above your head"<<endl;
}

void DescribeRoom01_04()
{
cout<<"You arrive in the conference room, again to find no one, the room is empty"<<endl;
cout<<" the only thing that is there is a long table where meetings are held"<<endl;
}

void DescribeRoom01_05()
{
cout<<"Now you have reached the door to the elevator, if only you could get"<<endl;
cout<<"inside you could escape from the wretched place."<<endl;
}

void DescribeRoom01_06()
{
	cout<<" You are now in the target practice room. This room is like a jumbo sized"<<endl;
cout<<"school gymnasium. Except instead of having basketball goals, there are"<<endl;
cout<<"dart boards and targets."<<endl;
}

//=======================Build Rooms in First Row=================//
void BuildRooms(unsigned int rooms[][4])
{

//Elevator
rooms[0][0]=DOOR_EAST|DOOR_SOUTH;
//Cafeteria
rooms[0][1]=DOOR_EAST|DOOR_NORTH;
//Closet
rooms[0][2]=DOOR_WEST|DOOR_NORTH|DOOR_SOUTH|MOUSE|CAUTION|ARMY_SUIT;
//PresidentCoinsRoom
rooms[0][3]=DOOR_NORTH|DOOR_SOUTH|LADY|ACCESS_CODE;
//Gym
rooms[1][0]=DOOR_WEST|DOOR_NORTH|SPIDERS|DEADLY;
// Holding
rooms[1][1]=DOOR_WEST|DOOR_EAST|TRACKER_JACKER|DEADLY;
//Englsih
rooms[1][2]=DOOR_NORTH|DOOR_SOUTH|DOOR_EAST;
// History
rooms[1][3]=DOOR_NORTH|DOOR_SOUTH;
//Mysteryroom
rooms[1][4]=DOOR_WEST|DOOR_SOUTH|DOOR_EAST;
//Conference
rooms[2][0]=DOOR_EAST|DOOR_SOUTH|MOCKING_JAY|FRIENDLY;
//Commandcenter
rooms[2][1]=DOOR_NORTH|DOOR_WEST|BUTTERCUP|FRIENDLY;
//Weapons
rooms[2][2]=DOOR_NORTH|DOOR_SOUTH|GORG|DEADLY|COMPASS;
// Storage
rooms[2][3]=DOOR_EAST|BOOTS|MONKEY|CAUTION;

}


//Describe room contents based on the int flag
//roomDesc
// row, col

void DescribeRoomContents(int roomDesc, int row, int col)

{
	switch (row)
	{
	case 0:
		switch(col)
		{
		
			case 0:
			DescribeRoom00_03();
			break;
			case 2:
			DescribeRoom00_04();
			break;
			case 3:
			DescribeRoom01_00();
			break;
			case 4:
			DescribeRoom02_00();
			break;
			case 5:
			DescribeRoom03_00();
			break;
			case 6:
			DescribeRoom04_00();
			break;
			case 7:
			DescribeRoom01_01();
			break;
			case 8:
			DescribeRoom02_02();
			break;
			case 9:
			DescribeRoom03_03();
			break;
			case 10:
			DescribeRoom04_04();
			break;
			case 11:
			DescribeRoom01_04();
			break;
			case 12:
			DescribeRoom01_05();
			break;
			case 13:
			DescribeRoom01_06();
			break;
		}
	}
	

	//Describe the creatures if any

	if(roomDesc & MONKEY)
		cout<<"You see a mutant monkey lurking in the corner"<<endl;
	if(roomDesc & TRACKER_JACKER)
		cout<<"You see a swarm of Tracker Jackers ready to attack"<<endl;
	if(roomDesc & SPIDERS)
		cout<<"You have stumbled across a pack of spiders.\n";
	if(roomDesc & GORG)
		cout<<"Gorg stands guarding the door, he will kill before he lets you through.\n";
	if(roomDesc & BUTTERCUP)
		cout<<" What is that disgusting looking creature? Oh it is just Buttercup, poor cat is hideous.\n";
	if(roomDesc & LADY)
		cout<<" Lady, President Coins dog is almost as lazy as she is.\n";
	if(roomDesc & MOCKING_JAY)
		cout<<"You spot in the distance a Mocking Jay, what a beautiful creature.\n";
	if(roomDesc & MOUSE)
		cout<<"Ugh another mouse, those awful little squeaky creatures.\n";

	// Tell if they are dangerous creatures

	if(roomDesc & DEADLY)
		cout<<"This creature is Deadly RUN!\n";
	if(roomDesc & CAUTION)
		cout<<"This creature could be dangerous.\n";
	if(roomDesc & DANGEROUS)
		cout<<"This creature is Dangerous, be very careful.\n";
	if(roomDesc & FRIENDLY)
		cout<<"This creature is harmless.\n";

	// Check for treasure Items

	if(roomDesc & ARMY_SUIT)
		cout<<" You see the army sui!\n";
	if(roomDesc & COMPASS)
		cout<<"You spot the compass in the corner.\n";
	if(roomDesc & ACCESS_CODE)
		cout<<"Could that be the access code?\n";
	if(roomDesc & BOOTS)
		 cout<<"ah, a pair of boots perfect!\n";

//Check the doors


	if(roomDesc & DOOR_NORTH)
		cout<<"There is a door to the North.\n";
	if(roomDesc & DOOR_SOUTH)
		cout<<"There is a door to the South.\n";
	if(roomDesc & DOOR_WEST)
		cout<<"There is a door to the West.\n";
	if(roomDesc & DOOR_EAST)
		cout<<"There is a door to the East.\n";
	

}
	int TakeItem(unsigned int rooms [4][4], int row, int col, string Object)
	{
		// Is player trying to take the ACCESS CODE

		if((Object=="ACCESS_CODE")&&(rooms[row][col]& ACCESS_CODE))
		{
			rooms[row][col]^=ACCESS_CODE; 
			return ACCESS_CODE;
		}
		else if((Object=="COMPASS")&&(rooms[row][col]& COMPASS))
		{
			rooms[row][col]^=COMPASS; 
			return COMPASS;
		}

		else if ((Object=="ARMY_SUIT")&&(rooms[row][col]& ARMY_SUIT))
		{
			rooms[row][col]^=ARMY_SUIT; 
			return ARMY_SUIT;
		}
		else if ((Object=="BOOTS")&&(rooms[row][col]& BOOTS))
		{
			rooms[row][col]^=BOOTS; 
			return BOOTS;
		}

		return 0;

	}
Posted
Updated 29-Nov-11 7:27am
v3
Comments
Addy Tas 29-Nov-11 13:22pm    
You posted a serious amount of code, can't you narrow it down a bit? What exactly is happening?
RaisKazi 29-Nov-11 13:25pm    
Edited: 1) Formatting 2) Added "pre" tag.
Richard MacCutchan 29-Nov-11 13:29pm    
When formatting you also need to change the < to &lt; and > to &gt; in #include statements.
RaisKazi 29-Nov-11 13:33pm    
Thanks Richard. Was not aware of it.
Albert Holguin 29-Nov-11 15:08pm    
It's a bit of an extra pain but the browser will confuse those characters with HTML markups...

1 solution

I copy'n'pasted and fixed a few typos (in cases of variable names) and run it and as far as I can see it's working, I was able to go to the room with the compass and take it.

I did not continue to get the other items.

You should print out more debugging information as you go along ( inventory, ... )

Some suggestions:
- make the inventory at least a vector of items instead of a bitfield (IMO it makes things easier).
- make variable names significant (iPlayer is the inventory ?)
...

anyway, fun little thing you get there, very zork-ish ...
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 29-Nov-11 14:33pm    
I would agree with you if the OP's code wasn't so hopeless. Your advice (respectfully agree with right principles you pointed out) looks like an attempt to add condiment to a rotten egg. If someone wanted to write a game like that, not a single line from this code should be used.
--SA
Maximilien 29-Nov-11 14:48pm    
well, it's a good exercise, not perfect, but a good start.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900