Hi there,

I'm trying to teach a few students how to create a text adventure game. In the past, I've generally gone about the route of using nested if-statements because it seems a bit easier to visualize. Here's what one has done in the past, and ignore the crazy code-formatting problems:

int main()
	{
		cout << "The Bishop\n\n";
		cout << "Press 'x' to begin.\n\n";
		
		bool door1Loop = false;

		cin >> start;
		if(start == 'x')
		{
		wakingUp();
			cin >> answer;
		
			if(answer == 'a') //
			{
				investigateDarkOnes();
					cin >> answer;
					if(answer == 'a')
					{
						doSomething();
					}
					if(answer == 'b')
					{
						thinking();
							cin >> answer;
							if(answer == 'a')
							doSomething();
							
							
					}
							}
			else if(answer == 'b')
			{
				doSomething();
			}
			else if(answer == 'c')
			{
				panicAttack();
					cin >> answer;
					if(answer == 'a')
					{
						sobbingMess();
					}
					if(answer == 'b')
					{
						doSomething();
					}
			}
			
		
		system("pause"); 
}
}

	void doSomething()
		{		bool beast1Loop = false;
		investigateDoor1();
								cin >> answer;
								if(answer == 'a')
								{
									thinking();
										cin >> answer;      
										if(answer == 'a')
										{
												beast1();
													cin >> answer;
													if(answer == 'a')
													{
														beast1Smack();
															cin >> answer;
															if(answer == 'a')
															{
																meetArchdemon();
															}
															else if(answer == 'b')
															{
																fallToYourDeath();
																system("exit"); 
															}
													}
													else if(answer == 'b')
													{
														beast1Negotiate();
													}
													else if(answer == 'c')
													{
														beast1Bolt();
													}
										}
								}
							
								if(answer == 'b')
								{
									panicAttack();
										cin >> answer;
										if(answer == 'a')
										{
											sobbingMess();
										}
										if(answer == 'b')
										{
											doSomething();
										}	
								}
								if(answer == 'c')
								{
												beast1();
													cin >> answer;
													if(answer == 'a')
													{
														beast1Smack();
															cin >> answer;
																if(answer == 'a')
																{
																	meetArchdemon();
																}
																if(answer == 'b')
																{
																	fallToYourDeath();
																	system("exit");
																}
													}
													else if(answer == 'b')
													{
														beast1Negotiate();
													}
													else if(answer == 'c')
													{
														beast1Bolt();
													}
								}
		}

However, another instructor has also suggested using a multidimensional array, such as a 2D Array to have more of a grid structure in a text game. Let me know if you need clarification, but I was wondering if any of you have any ideas on which would be more efficient, and which would bring about the concepts of programming easier to students with less than a week of experience under their belts! If it is 2D arrays, do you think you could explain it a bit, I'm having trouble wrapping my head around it. Thanks for the help!

Recommended Answers

All 4 Replies

> and ignore the crazy code-formatting problems:
Sorry, that just gets your post ignored.

Nobody here can be bothered to wade through more than a page full of code that was indented by a dancing parrot.

Good indentation will tell you a lot about where it is going wrong without having to read the detail.

All right, let me rephrase. Is it better to have a text game, say starting in the woods, then picking an answer that would go to a dungeon, and then having corresponding choices to go in opposite ways, using a multidimensional array or nested if statements? That code above was an attempt at a basic example of nested if-statements calling functions.

if(answer == 'a')
           {
                  char c = goToDungeon(); 
                  if(c == 'a')
                  {
                          die(); 
                  }
                  else if(c == 'b')
                  { 
                          goToNextRoom(); 
                  }
          }

And so on. Is this clearer? I have trouble picturing a multidimensional array with that, especially with moving from different points in the 2D array. Any ideas?

This is my three room adventure game

You add more rooms by editing the enum, and adding an extra row in the room array, and changing a few doors in existing rooms to get to the new room.

You might then consider adding say

struct room {
    char    *name;
    int      next[4];   // N,S,W,E from this location
    object  *objects;   // List of things in this room
}

If this is NULL, there is nothing here.

But perhaps you have

struct object {
  struct object *next;
  enum objType;  // objLamp, objAxe, objKey, ...
  char *name;  // "lamp", "axe", "key"
};

Your player struct also has a list of objects collected, which is displayed when you type "inventory". Picking up and putting down is simply moving from the player object list to the room object list.

When you come to open a door, you can check whether the key exists in the player inventory, IF the door happens to be in the locked state.

And so on...

Interesting, I really like the idea of using structs, especially with an inventory. Thanks! Since the week is almost over, for future reference, if there had to be a decision between the efficiency and clarity for first-time programmers to use either nested if-statements and function or a 2D array, which might make more sense? I might even start using structs to teach with a text game next week.

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.