Text based RPG - Choice issue

Hello C++ Forums,

I have recently takes up learning C++ in my spare time. I have a small background in other types of coding, but have only been working with C++ for about a week. For my first project I have decided to write a small RPG game (as seems to be the case for most first timers).

Here is my code so far. Please note that I have tried to change as much as I can for what I am intending, but am thus far unable to find a solution, so the code may look a bit strange in places.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<ctime>

using namespace std;
int main()
{
    cout <<"Welcome to my Text Based game! ";

    char userName[100];

    cout <<" Please select a Username: ";
    cin >>userName;

    cout <<"Welcome: "<<userName<<endl;


//Pick Race
bool validInput=false;
int pickClass;
double Warrior_class;
double Wizzard_class;
while(!validInput)
{
    cout <<"Please pick your class: "<<endl;
    cout <<"1 - Warrior\n";
    cout <<"2 - Wizzard\n";
    cout <<"Pick your Class: ";
    cin >>pickClass;

    switch (pickClass)
    {
        case 1:
            cout <<"You picked the Warrior.\n";
            cout <<"You can choose from [S]word + sheild (4dmg) and [G]reat Axe (6dmg).\n";
            break;

        case 2:
            cout <<"You picked the Wizzard.\n";
            cout <<"You can choose from [I]ceblast (4dmg) and [F]ireball (6dmg).\n";
            break;

        default:
            cout <<"Error - Invalid input; only 1 or 2 allowed.\n";
            continue;
    }
    validInput = true;

    cin.sync();
    cin.get();
    system("cls");

cout <<"Welcome "<<userName<< ", ";
cout <<" There are bad guys everywhere, Be on your guard!"<<endl;
cout <<"You walk outside and notice a suspicouse character lurking around the shadows of the town."<<endl;

}

string choice1;
cout <<" do you: " << endl << "[1]Follow the character and find out what he is doing?"<<endl;
cout <<"[2] Return to your work?"<<endl;
cout << endl<< ":";
cin >>choice1;



/////////////////       Combat System for Warrior     //////////////////////////////////

if (choice1=="1")
{

    //Setting variables
    double Health = 50;
    double Sword_Sheild = 4;
    double Greataxe = 6;
    double Iceblast = 4;
    double Fireball = 6;
    double Health_Potion = 10;
    double Badguy_Health = 30;
    unsigned Badguy_Damage = time(0);

        while (Badguy_Health >=1 && Badguy_Health >= 1)


        {//master while in combat

            srand(static_cast<unsigned int>(time(0)));

            //Combat
            cout <<" The Badguy attacks!"<<endl;
            cout <<" Your HP: "<<Health;
            cout <<" Badguy HP: "<<Badguy_Health<<endl;

            srand(Badguy_Damage);

            Badguy_Damage = (rand() % 5) +1;

            //Clear screen before start of Combat
            cin.sync();
            cin.get();
            system("cls");

            cout <<" The Badguy did "<<Badguy_Damage<<" Damage"<<endl;

            Health = Health - Badguy_Damage;


            //Displaying HP after combat
            cout <<" Your HP: "<<Health<<endl;
            cout <<" Badguy HP: "<<Badguy_Health<<endl;

            system("pause");


            //Zero HP after combat
            char Attack;

                if(Badguy_Health > 0 && Badguy_Health < 1)
                {
                    cout <<" You are dead"<<endl;

                system("Title   Game Over   ");

                system ("pause");
                return 0;
                }

            cin.sync();
            cin.get();

            //Main Combat

            //Conditions of combat

//Warrior Attacks
    if (Warrior_class)

            cout << "Choose a weapon to fight or you will die!"<<endl;
            cout << "[S]word and Sheild: 4 attack"<<endl;
            cout << "[G]reataxe: 6 attack"<<endl;
            cout << "[H]ealth Pot"<<endl;
            cin >>Attack;


       {
                if(Attack == 'S' || Attack == 's')
                {
            Badguy_Damage = (rand() % 10) + 1;
            Badguy_Health = Badguy_Health - Sword_Sheild;
            cout<<" You choose Rusty Sword dealing 3 Damage! "<<endl;
                }



                if(Attack == 'G' || Attack == 'g')
                {
            Badguy_Damage = (rand() % 10) + 1;
            Badguy_Health = Badguy_Health - Greataxe;
            cout<<" You attack with your Greataxe dealing 4 Damage! "<<endl;
                }
       }

//Wizzard Attacks
       if (Wizzard_class)

            cout << "Choose a weapon to fight or you will die!"<<endl;
            cout << "[I]ceblast: 4 attack"<<endl;
            cout << "[F]ireball: 6 attack"<<endl;
            cout << "[H]ealth Pot"<<endl;
            cin >>Attack;

        {
                if(Attack == 'I' || Attack == 'i')
                {
            Badguy_Damage = (rand() % 10) + 1;
            Badguy_Health = Badguy_Health - Iceblast;
            cout<<" You cast Iceblast dealing 3 Damage! "<<endl;
                }



                if(Attack == 'F' || Attack == 'f')
                {
            Badguy_Damage = (rand() % 10) + 1;
            Badguy_Health = Badguy_Health - Fireball;
            cout<<" You attack with your Fireball dealing 6 Damage! "<<endl;
                }

        }

                if(Attack == 'H' || Attack == 'h')
                {
            Badguy_Damage = (rand() & 10) + 1;
            Health = Health + Health_Potion;
            cout << " You gain 10hp from Health Potion! "<<endl;
                }


            //If userName wins
                if(Badguy_Health <= 0 && Health > 0)
                {
            cout <<" You did it "<<userName<< " you are safe!"<<endl;
                }

/////////////////       End of Combat System      //////////////////////////////////////



//Choice 2 (Walk away)
if (choice1 == "2")
{
    system("cls");
    cout <<"You decide to go on about your buisness, unfortunatly the chacater from earlier decided to follow you!"<<endl;
    cout << endl <<"Press [Enter] to continue"<<endl;

    cin.sync();
    cin.get();

        cout <<"You finally get a good look at the creature, it was a..."<<endl;
        cout <<"Murloc!"<<endl;
        cout <<"You flail about, but without a weapon you fall victom to the Murlocs claws"<<endl;

    cin.sync();
    cin.get();
    system("cls");

        cout <<"You have died. Your quest ends here."<<endl;
        cout <<"Press [Enter] to quit";

        cin.sync();
        cin.get();

        return 0;

} //End of Choice 2 (End)

}

cout <<"After combat you take the time to search over the body. There is nothing of interest apart from";

}

}//End of Program 


What I am trying to achieve at the moment is: The person selects one of two classes (Warrior or Wizzard). Once they have chosen, they will be shown their class specific abilities (self explanatory). During combat I would like for only their class abilities to be shown in the text. As it stands I have seperated them with if tags, but I am unsure how to complete this. When the program runs it is only showing the Warrior abilities as they are first in the list.

If anyone has any feedback, it would be greatly appreciated. As I already mentioned I am only new, but I understand most of the basics already.

Kind Regards,
Nihilite (Matt)
Last edited on
On line 137 and 165 you have:
1
2
    if (Warrior_class)
    if (Wizzard_class)


These are doubles and you haven't initialized them. That means that both if statements will be entered.

What you should do is:
- have a single int value represent your class. If it is 1, then you're a warrior, if it is 2, then you're a wizard. We often call this type of varible a switch.
- You should set this value in your switch on line 32.
- Instead of using if-else statements every time you want to look at your fighting attributes, have a set of variables permanently represent the player's attributes. Set these to the default warrior or wizard attributes in the switch on line 32.
Stewbond,

I thank you for you response. I am aware that I had done that incorrectly, I just had to put something there until I knew what do do :). I really appreciate your help, I will add in those changes and see if I can sort anything else out myself.

Thank you again,
Nihilite
If it is 1, then you're a warrior, if it is 2, then you're a wizard. We often call this type of variable a switch


me = 1|2;

Look at me! I'm a warzard!

Arr. I couldn't resist.
Try using a switch statement. Here is some psudo code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
switch(which class?)
{
case warrior class:
{
code here
break;
}
case wizard class:
{
code here
break;
}
default:
{
what happens with invalid input
break;
}
}
Last edited on
I am really trying to understand, but unfortunatly I am still rather confused with it all! I am the sort of person who learns by disecting other code, and as I have been unsuccessful with finding what I am after I am having a hard time getting my head around it all. I get what a switch does, multiple options and depending on what the person inputs, they can get either 1 or 2 (for example).

The issue I am having is, how do I adapt:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    switch (pickClass)
    {
        case 1:
            cout <<"You picked the Warrior.\n";
            cout <<"You can choose from [S]word + sheild (4dmg) and [G]reat Axe (6dmg).\n";
            break;

        case 2:
            cout <<"You picked the Wizzard.\n";
            cout <<"You can choose from [I]ceblast (4dmg) and [F]ireball (6dmg).\n";
            break;

        default:
            cout <<"Error - Invalid input; only 1 or 2 allowed.\n";
            continue;
    }
    validInput = true;


So I can add in that, yes they chose Warrior, therefore only Warrior abilities will show.
Thank you all again for your input. I really am trying to understand.

-Nihilite
Add as many case input: as you need

Try the tutorial as well. It is much more clear than I can be:
http://www.cplusplus.com/doc/tutorial/control/
I have re-read over that part of the tutorial. From what I can see if I add more cases to my switch it will just give the user other options instead of only chosing the Warrior or Wizard.

From my understanding (which of C++ is little, but I know what I am trying to achieve) I need something like this: (no this isnt code :) )

switch (pickClass)
{
1 - Warrior.. you chose 1, warrior, you get Y attacks only
2 - Wizard.. you chose 2, Wizard, you get X attacks only
}

Combat as Warrior (no X attacks available)
or
Combat as Wizard (no Y attacks available)

Does this seem right? The issue now is, how do I link that switch to the rest of my code so only the correct attacks are available? (I can press F for Fireball as Warrior atm).

Sorry if I am just confusing everyone more... This really is a massive learning curve for me.

- Nihilite
I strongly suggest you use classes.

http://www.cplusplus.com/doc/tutorial/classes/
Topic archived. No new replies allowed.