Help with Text Based RPG!

9343bee56dfd1467d4bacc66c193bcb1
0
Myles108 101 Jul 22, 2013 at 19:20 c++

I need help with my Text Based RPG. I just started making it so I barely have anything done, but I would like to know how to make it so if you accidentally put in the wrong number for choosing a race, or difficulty, the file will not make you exit the game. Here is what I have done so far.

#include <iostream>
#include <cstdlib>

using namespace std;

int main(void)
{
    system("TITLE Cool Game");
    system("COLOR B2");
    cout <<"Hello and welcome to my old fashioned text based game!!!!!\n";
    char myUserName[30];
    cout <<"Please enter the name of your character (no spaces): ";
    cin >>myUserName;
    cout <<"Welcome, "<<myUserName<<"!\n\n";

    cout << "Please pick your character's race: \n";
    cout << "The availabe races are listed below \n\n\n";
    cout << "1: Elf\n";
    cout << "2: Human\n";
    cout << "3: Alien\n";
    cout << "4: Orc\n";
    cout << "5: Wolf-man\n";
    cout << "6: Dwarf\n";
    cout << "7: Dragonborn\n";
    cout << "8: Doppelganger\n";
    cout << "9: Owl-Man\n";
    cout << "10: Ape-Man\n";
    int pickRace;
    cout <<"Choose one of the races above(To choose a race, enter the number next the race.)\n";
    cin >> pickRace;
    if (pickRace <=1 && pickRace >=10);
    else
            cout << "Invalid race selected.\n";

switch (pickRace)
    {
    case 1:
        cout <<"You picked the Elf race.\n";
        break;
    case 2:
        cout <<"You picked the Human race.\n";
        break;
    case 3:
        cout <<"You picked the Alien race.\n";
        break;
    case 4:
        cout <<"You picked the Orc race.\n";
        break;
    case 5:
        cout <<"You picked the Wolf-Man race.\n";
        break;
    case 6:
        cout <<"You picked the Dwarf race.\n";
        break;
    case 7:
        cout <<"You picked the Dragonborn race.\n";
        break;
    case 8:
        cout <<"You picked the Doppelganger race.\n";
        break;
    case 9:
        cout <<"You picked the Owl-Man race.\n";
        break;
    case 10:
        cout <<"You picked the Ape-Man race.\n";
        break;
    default:
        cout <<"Error - Invalid input; only 1 through 10 allowed.\n";
    }

    int difficulty;
    cout << "\nPlease choose the difficulty of your game:\n";
    cout << "1 - Easy\n";
    cout << "2 - Normal\n";
    cout << "3 - Hard\n";
    cout << "\nPlease choose the difficulty of your game:\n";
    cin >> difficulty;

    switch (difficulty)

    {
    case 1:
        cout << "You have picked Easy.\n\n";
        break;
    case 2:
        cout << "You have picked Normal.\n\n";
        break;
    case 3:
        cout << "You have picked Hard.\n\n";
        break;
    default:
        cout <<"Error - Invalid input; only 1, 2, or 3 are allowed.\n";
    }

    system("PAUSE");
    return 0;
}

This was done in Code::Blocks compiler, using C++. Please help me!

9 Replies

Please log in or register to post a reply.

A8433b04cb41dd57113740b779f61acb
0
Reedbeta 167 Jul 22, 2013 at 20:24

Hi Myles, please use [ code ] … [ /code ] tags (without the spaces around the brackets) when posting code in the forum, to keep the formatting intact. I added the tags for you but unfortunately I think some of your code already got garbled (see the “cout << “Welcome, “<< myusername” etc. line). Could you edit the post and fix that? Thanks.

As for your question, one way to approach this would be to use a loop, and break out only when a valid selection has been made. For example,

for (;;)
{
    cout << "Choose your race: ";
    int race;
    cin >> race;

    // Check that race is in the required range
    if (race >= 1 && race <= 10)
        break;
    else
        cout << "Invalid race selected.\n";
}

That way it will repeatedly ask for a race until you give it a number between 1 and 10.

9343bee56dfd1467d4bacc66c193bcb1
0
Myles108 101 Jul 22, 2013 at 22:34

@Reedbeta

Hi Myles, please use [ code ] … [ /code ] tags (without the spaces around the brackets) when posting code in the forum, to keep the formatting intact. I added the tags for you but unfortunately I think some of your code already got garbled (see the “cout << “Welcome, “<< myusername” etc. line). Could you edit the post and fix that? Thanks.

As for your question, one way to approach this would be to use a loop, and break out only when a valid selection has been made. For example,

for (;;)
{
    cout << "Choose your race: ";
    int race;
    cin >> race;

    // Check that race is in the required range
    if (race >= 1 && race <= 10)
        break;
    else
        cout << "Invalid race selected.\n";
}

That way it will repeatedly ask for a race until you give it a number between 1 and 10.

I’m sorry Reedbeta, I wasn’t clear enough when I was stating my problem (even though your reply did help me immensely.). What I intended to ask was how do I make it so if, for example, some one types the word Elf when picking their race instead of entering a number between 1 and 10, the game will say what the player did wrong and it will start at the race selection part of the game.
Thanks a lot!

A638aa42130293f319eda7fa4ba121f4
0
fireside 141 Jul 22, 2013 at 23:35

You can use a boolean variable like wrongchoice;
bool wrongchoice = false;

for(;;)
{
if(wrongchoice)
cout<<"invalid race selected";

cout<<"please choose a race";

//race choosing code 
if(pickrace >0 && pickrace < 11)
break;
else wrongchoice = true;
}
9343bee56dfd1467d4bacc66c193bcb1
0
Myles108 101 Jul 23, 2013 at 00:59

@fireside

You can use a boolean variable like wrongchoice;
bool wrongchoice = false;

for(;;)
{
if(wrongchoice)
cout<<"invalid race selected";

cout<<"please choose a race";

//race choosing code
if(pickrace >0 && pickrace < 11)
break;
else wrongchoice = true;
}

I wrote down what you said, but now it says “Error: ‘wrongchoice’ was not declared in this scope.
Sorry to keep asking for help, and thanks for the help so far.

A638aa42130293f319eda7fa4ba121f4
0
fireside 141 Jul 23, 2013 at 01:07

Did you put the

bool wrongchoice = false;

in there? I didn’t get it in the code blocks but it should go right before the loop.

Actually, Reedbeta’s code should have done the same thing, I just didn’t look at it when I read your comment. He just didn’t include a comment for race types to be added which might be what you wanted to be in the loop so it would be repeated if it was the wrong answer. Anyway, just play around and you’ll figure it out. I generally use a while loop for that kind of thing like this, so I’m used to using a boolean:

bool done = false;
while(!done)
{
cout<<"choose race;"
cout<<"choice 1 blah blah"
cin>>answer;
if(answer > 0 && answer < 11)
done = true;
else cout<<"wrong selection"
}

That might be why it was out of scope also, but I would think that would have been all right.

Fd80f81596aa1cf809ceb1c2077e190b
0
rouncer 103 Jul 23, 2013 at 06:28

good going, this is the perfect way to start coding c++ instead of going for an mmo straight away. :P

9343bee56dfd1467d4bacc66c193bcb1
0
Myles108 101 Jul 25, 2013 at 23:52

Hello again! I basically rewrote a lot of stuff in my RPG, but I wanted to see how you could add features to make it so if a person typed L into the program, it would respond by looking around the room. You might be able to see at the bottom of this post that I was trying to do just that, but it didn’t work! All it did was say “That is impossible. Type HELP for details.” every time you would get to a specific part in the story!
Much thanks, Myles108 :D

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

int main(void)
{

{
    system("TITLE The Tree of Gold");
    system("COLOR B2");
    string first_name;
    string last_name;

    cout << "Are you a male or a female?\n(To choose a gender, enter the number next the gender of choice.)\n";
    cout << "1: Male\n";
    cout << "2: Female\n";
    int pickgender;
    cin >> pickgender;

    switch (pickgender)
    {
    case 1:
        cout <<"You are a Male.\n\n";
        break;
    case 2:
        cout <<"You are a Female.\n\n";
        break;
    }



    cout << "Please enter your first name(no spaces):";
    cin >> first_name;
    cout << "Please enter your last name: ";
    cin >> last_name;
    string full_name = first_name + " "  + last_name;
    cout << "Your name is: " << full_name << "\n\n\n";
}
    cout << "Please pick your character's race: \n";
    cout << "The available races are listed below \n\n\n";
    cout << "1: Elf\n";
    cout << "2: Human\n";
    cout << "3: Alien\n";
    cout << "4: Orc\n";
    cout << "5: Wolf-man\n";
    cout << "6: Dwarf\n";
    cout << "7: Dragonborn\n";
    cout << "8: Doppelganger\n";
    cout << "9: Owl-Man\n";
    cout << "10: Ape-Man\n";
    int pickRace;
    cout <<"Choose one of the races above\n";
    cin >> pickRace;

    switch (pickRace)
    {
    case 1:
        cout <<"You picked the Elf race.\n\n\n";
        break;
    case 2:
        cout <<"You picked the Human race.\n\n\n";
        break;
    case 3:
        cout <<"You picked the Alien race.\n\n\n";
        break;
    case 4:
        cout <<"You picked the Orc race.\n\n\n";
        break;
    case 5:
        cout <<"You picked the Wolf-Man race.\n\n\n";
        break;
    case 6:
        cout <<"You picked the Dwarf race.\n\n\n";
        break;
    case 7:
        cout <<"You picked the Dragonborn race.\n\n\n";
        break;
    case 8:
        cout <<"You picked the Doppelganger race.\n\n\n";
        break;
    case 9:
        cout <<"You picked the Owl-Man race.\n\n\n";
        break;
    case 10:
        cout <<"You picked the Ape-Man race.\n\n\n";
        break;
    default:
        cout <<"Error - Invalid input; only 1 through 10 allowed.\n";
    }
    cout << "Welcome to The Tree of Gold, a text based adventure RPG made by *************.\n Get ready to be amazed...\n\n";
    cout << "CHAPTER 1\n";
    cout << "All you can remember is your name. You try to think back, but your head starts \nto pound, and so you close your eyes and try to drive the burning sensation out of your head.";
    cout << "The pain passes, and so you open your eyes.\n\n";
    {

    string look;

    getline( cin, look, '\n' );
    if (look == "L")
    {
        cout << "You are sitting on a small wooden chair, with ropes tied around your wrists.\n The room is small. At least, it seems small. It is much to dark to be able to \ntell for sure. ";
    }
    else
    {
        cout << "That is impossible. Type HELP for details.";
    }
    }

    system("PAUSE");
    return 0;
}
A8433b04cb41dd57113740b779f61acb
0
Reedbeta 167 Jul 26, 2013 at 00:41

Hi Myles, again, please use the [ code ] … [ /code ] tags around code pasted into a post, to preserve the formatting. I added it for you.

As for your problem, I wonder if it’s due to case-sensitivity? You’re comparing the string to capital L, but are you typing a capital L or a lowercase L? You might try comparing with both capital L and lowercase L if you don’t care which one the player types.

B20d81438814b6ba7da7ff8eb502d039
0
Vilem_Otte 117 Jul 26, 2013 at 00:48

It will need a bit deeper explanation. You probably want to implement some sort of state machine.

So the game should be a state machine, it has to have some sort of state - e.g. the game state. It could be specified by some sort of integer. Now for each state you should have defined some sort of operations, F.e. under “L” it prints room details, under A, B or C you switch states depending on in which way you want to go, etc. etc.

So let’s say you have some file format that will describe room, like this:

# File - State 1
A 2
B 3
PRINT You're in a dark room with 2 exits, you can't go back as you've fallen into this room. You can go to left (A) or to right (B).
DETAILS The room is dark with stone walls. The walls are wet, you can't see anything else in the room.

# File - State 2
DEAD
PRINT You went through dark corridor, where sadly you stepped on the trap. A spike emerged with the speed faster than the speed of sounder and pierced through your head, your brain has been splashed all over and you've died.

# File - State 3
WIN
PRINT You've succesfully went through dark corridor, in the end you've found really long and old ladder, even though it looked like it will be defragmented in next 37 seconds, you've successfully climbed up. You thank all good and bad gods, and of course any gods in between, for saving your epic life. Once looked around a beautiful princess appeared, you married her, had 8 children with her and ruled half of the solar system (or kingdom). You're living happily together forever, as you both became immortal. Good job, well played.

Now you start in state 1, you read the file, store the state ids for state transitions (the A and B), read PRINT (and print that one out), and read DETAILS. Upon recieving L you print details, upon recieving A or B, you change your state and again print what is in that state’s PRINT, along with printing out VICTORY or DEFEAT once you hit DEAD or WIN in a file.

The loop could look like, in pseudo code:

playing = true;
state = 1;

while(playing)
{
     StateInfo info = ReadFile(string(state) + ".txt");

     cout << info.PRINT << endl;

     if(info.isDead) { playing = false; cout << "DEFEAT" << endl; break; }
     else if(info.isWin) { playing = false; cout << "WIN" << endl; break; }

     string tmp;
     cin >> tmp;

     if(trimWhitspace(tmp) == "L") cout << info.DETAILS << endl;
     else if(trimWhitespace(tmp) == "A") state = info.A;
     else if(trimWhitespace(tmp) == "B") state = info.B;
     else cout << "Unknown command" << endl;
}