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.
#include<iostream>
#include<cmath>
#include<cstdlib>
#include<ctime>
usingnamespace 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<unsignedint>(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.
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.
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.
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;
}
}
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.
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.