Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   C++ Text Adventure assistance? (http://www.go4expert.com/forums/cpp-text-adventure-assistance-t25768/)

JArruda 13May2011 06:25

C++ Text Adventure assistance?
 
Hey everyone. I've got this project I am doing for class and have run into an issue. I'm trying to make my own, relatively basic text adventure.

As of right now I have the body of the game set up simply just giving the user some choices to make.

You run through the forest when before you know it you come face to face with a bear.
You:
1. Sneak around the bear
2. Play dead
3. Karate chop the bear and he kills you

and each choice obviously has a different outcome, at least that's the plan.

One main issue I've run into is how can I slowly delve each choice made into a slightly different outcome?

For instance if I choose any of the options above "1, 2 or 3" the next part of the story would be read the same exact way. If I choose "1" the outcome can be "You Win". If I choose "3" the outcome will still be "You Win".

I apologize if it's still a bit foggy to understand what I'm attempting to explain. I'd like story branching in my basic text adventure and am unsure of how to go about doing so.

Here's what I have so far so you can see.

Code:

#include <iostream>
#include <string>
using namespace std;

int main()
{
    cout <<"\tWelcome to my text based game!\n";
    char userName[100];
    cout <<"\nPlease enter your character name: ";
    cin >>userName;
    cout <<"Hello, "<<userName<<"!\n\n";
   
    cout <<"Please pick your race: \n";
    cout <<"1 - Human\n";
    cout <<"2 - Orc\n";
    cout <<"3 - Elf\n";
    int pickRace;
    cout <<"Pick your race: ";
    cin >>pickRace;
   
    switch (pickRace)
    {
          case 1:
                cout <<"\nYou picked the Human race.\n";
                break;
          case 2:
                cout <<"\nYou picked the Orc race.\n";
                break;
          case 3:
              cout <<"\nYou picked the Elf race.\n";
              break;
          default:
                  cout <<"Error - Invalid input; only 1, 2 or 3 allowed.\n";
    }

   
    int pickClass;
    cout <<"\nPick your class: \n";
    cout <<"1 - Wizard\n";
    cout <<"2 - Warrior\n";
    cout <<"3 - Assassin\n";

    cout <<"Pick your class: ";
    cin >> pickClass;
   
    switch (pickClass)
    {
          case 1:
                cout <<"\nYou chose to be a Wizard.\n\n";
                break;
          case 2:
                cout <<"\nYou chose to be a Warrior.\n\n";
                break;
          case 3:
                cout <<"\nYou chose to be a Assassin.\n\n";
                break;
          default:
                  cout <<"Error - Invalid input; only 1, 2 or 3 allowed.\n";
    }

    {
cout<<"You awake from a deep sleep and slowly become aware of your surroundings. "<<endl;
cout<<"You try to move your arms and legs, but  it's useless as you realize you've been chained to a wall inside what appears to be a dungeon. "<<endl;
cout<<"Everything is dark, until you see a distant light source slowly getting bigger \n and bigger as it comes towards you."<<endl;
cout<<"You begin to panic, but regain your composure and:\n"<<endl;
cout<<"1. You close your eyes and focus your mind as you transform into a rat."<<endl;
cout<<"2. You try using brute strength to bust loose."<<endl;
cout<<"3. You're intrigued by the light and patiently wait until it reaches you.\n"<<endl;
string answer;
cout << "Selection: ";
cin >> answer;
if (answer == "1")
{
cout<<"\nYou carefully scamper away unharmed, avoiding the light source as you find the \nentrance and escape"<<endl;

}
else
{
if (answer == "2")
{
cout<<"\nYou've managed to break free, but you're exhausted and the light source is \ncoming at you even faster now"<<endl;
}
else
{
if (answer == "3")
{
cout<<"\nYou notice a rather large figure covered in a black robe carrying a sconce \napproaching you. "<<endl;
cout<<"The unworldly creature lets out a maniacle laugh as it lifts a necklace over \nyour head and places it around your neck. "<<endl;
cout<<"The creature vanishes into thin air. The necklace immediately begins to glow, \nand you feel empowered as the chains become loose. "<<endl;
cin.get();
cin.ignore();
}
else
{
cout<<"That wasn't a choice."<<endl;
}


xpi0t0s 13May2011 10:59

Re: C++ Text Adventure assistance?
 
Well if you want later behaviour to depend on earlier choices then you have to store those earlier choices one way or another. Probably you'll need an array of some sort, cos I guess you'll want to do that several times, so one slot for each time. e.g.:
Code:

int Choices[MAX_CHOICES]; // where we store the result of the bear encounter and...
...
printf("You meet a bear etc\n");
do {
printf("What are you going to do?\n");
cin>>Choices[BEAR];
switch (Choices[BEAR])
{
case 1: ...; break;
case 2: ...; break;
case 3: ...; break;
default: ...; printf("Enter 1-3\n");break;
}
} // end do, and thus loop until a valid answer is given

so at this point we now have the bear choice stored in Choices[BEAR] (BEAR is a const of some sort, probably an enum) and we can use that later:
Code:

// This bit depends on the bear choice
printf("You meet a bear's mum\n");
switch (Choices[BEAR])
{
case 1: printf("I see you haven't met my son. Here's an amulet of some kind for no obvious reason.\n");
addInventory(BEAR_AMULET);
break;
case 2: printf("Oh it's you. My son told me about a lame adventurer who just played dead. Well I don't give anything to losers\n"); break;
case 3: printf("You karate'd my son and he killed you!  Why are you still alive?\n");
printf("Well I'm going to kill you now\n");
dead=1;
break;
}


JArruda 13May2011 23:01

Re: C++ Text Adventure assistance?
 
I apologize if I'm not understanding something but perhaps I'm inputting the code incorrectly within my program? Would it be a bother if you could input what you mean within my code just so I can see exactly how you have it set up. If I have that working I shouldn't have an issue creating more following that setup on my own.

JArruda 13May2011 23:04

Re: C++ Text Adventure assistance?
 
to reiterate, I'm getting multiple errors trying to incorporate that myself.

xpi0t0s 17May2011 11:38

Re: C++ Text Adventure assistance?
 
What's the code, and what are the errors?


All times are GMT +5.5. The time now is 18:24.