# A multi-class, multi-racial RPG with magical combat [closed]

This is my first attempt at making a program in C++, I was wondering if my code was good or should it be improved upon. Here is the code below. Any help debugging would be greatly appreciated. Some things do not work as intended, such as the battle. The grunt would still continue to attack after his health goes to zero. I just want to make sure my code is good.

    #include <iostream>
using namespace std;

int main()
{
cout <<"\tIt is the END OF TIME AND SPACE!\n";
cout <<"\tA Darkness looms in the Horizon\n";
cout <<"\tA Ahead of you is a bleak, dark wasteland\n";

cout  <<userName<<" Journey forth, nothing remains!\n\n";

cout <<"Who are you?: \n";
cout <<"1 - Mage\n";
cout <<"2 - Warrior\n";
cout <<"3 - Ninja\n";
int pickRace;
cin >>pickRace;

int Attack;
int Defence;
int Magic;
int Speed;
int HP;
int x;
int Reason;
int What;
int Difficulty;
int Health;

switch (pickRace)
{
case 1:
cout <<"You picked the Warrior race.\n";
Attack=107;
Defence=122;
Magic=74;
Speed=64;
HP=88;
Health=88;
x=0;

cout <<" Your Attack is " <<Attack;
cout <<" Your Defence is " <<Defence;
cout <<" Your Magic is " <<Magic;
cout <<" Your Speed is " <<Speed;
cout <<" Your HP is " <<HP;
break;
case 2:
cout <<"You picked the Mage race.\n";
Attack=69;
Defence=72;
Magic=114;
Speed=104;
HP=75;
Health=75;
x=1;

cout <<" Your Attack is " <<Attack;
cout <<" Your Defence is " <<Defence;
cout <<" Your Magic is " <<Magic;
cout <<" Your Speed is " <<Speed;
cout <<" Your HP is " <<HP;
break;

case 3:
cout <<" You picked the Ninja race.\n";
Attack=95;
Defence=67;
Magic=103;
Speed=122;
HP=72;
Health=72;
x=0;
cout <<" Your Attack is " <<Attack;
cout <<" Your Defence is " <<Defence;
cout <<" Your Magic is " <<Magic;
cout <<" Your Speed is " <<Speed;
cout <<" Your HP is " <<HP;

break;
default:
cout <<"You IDIOT, all is lost\n";
return main();
}

int difficulty;
cout <<"\nPick your level difficulty: \n";
cout <<"1 - Casual\n";
Difficulty=1;
cout <<"2 - Medium\n";
Difficulty=2;
cout <<"3 - Hardcore\n";
Difficulty=3;

cout <<"Pick your level difficulty: ";
cin >>difficulty;

switch (difficulty)
{
case 1:
cout <<"You picked Casual.\n\n";
difficulty=1;
break;
case 2:
cout <<"You picked Medium.\n\n";
difficulty=2;
break;
case 3:
cout <<"You picked Hardcore.\n\n";
difficulty=3;
break;
default:
cout <<"Error - Wow, aren't you sooo smart?.\n";
return main();
}
{
cout <<"\tTime and Space are irrelevant!\n";
cout <<"\tA  lone person journeys through the end of time\n";

cout <<"\nWHY ARE YOU HERE?: \n";
cout <<"1 - I Don't Know\n";
cout <<"2 - Help ME\n";
cout <<"3 - Where am I?\n";

cout <<"Why are you here?: ";
cin >>Reason;

switch (Reason)
{
case 1:
cout <<"You Started walking North.\n\n";

break;
case 2:
cout <<"You have lost all sainity.\n\n";
break;
case 3:
cout <<"You are nowhere to be found.\n\n";
break;
default:
cout <<"YOU WILL DIE.\n";
}
cout <<"\nWHAT DO YOU DO?: \n";
cout <<"1 - Run\n";
cout <<"2 - Fight\n";
cout <<"3 - Look\n";

cout <<"What do you do?: ";
int a;
cin >>a;

switch (a)
{
case 1:
cout <<"What are you running from?.\n\n";
break;
case 2:
cout <<"You try to attack, but realise there is nothing to attack.\n\n";
break;
case 3:
cout <<"Nothing.exe.\n\n";
break;
default:
cout <<"ƃuᴉɥʇoN.\n";

}
cout <<"\t An Enemy appears!\n";
cout <<"\t He is lost in Time\n";
cout <<"\t The enemy confuses you, your eyes don't understand what is happening;\n";

int EnemyAttack;
int EnemyDefence;
int EnemyMagic;
int EnemySpeed;
int EnemyDamage;
int EnemyHP;
int EnemyFire;
int A;

EnemyHP=HP/2*(difficulty/2);
EnemyDamage=Attack-EnemyDefence;
EnemyFire=Magic-EnemyDefence;
EnemySpeed=Speed/2*(difficulty/2);
EnemyMagic=Magic/2*(difficulty/2);
EnemyDefence=Defence/2*(difficulty/2);
EnemyAttack=Attack/2*(difficulty/2);

cout <<"\t The enemy has\n" << EnemyHP <<" Health remaining\n";

do

{cout <<"\nWHAT DO YOU DO?: \n";
cout <<"1 - Fight\n";
cout <<"2 - Magic\n";
cout <<"3 - Examine\n";
cout <<"4 - Run\n";

cout <<"What do you do?: ";
cin >>What;

switch (What)

{
case 1:
cout <<"You flail around until you hit something... Sucess\n";
cout <<" You deal " << EnemyDamage<< " Damage\n";
cout <<"The grunt has" <<EnemyHP-EnemyDamage<< " Health Left";
break;
case 2:
cout <<"You try to conjure up a spell.\n";
if (x==1)
{cout <<"You set the foe on fire\n";
cout <<"You deal" <<EnemyFire<< " Damage\n";
cout <<"The grunt has" << EnemyHP-EnemyFire << " Health Left";
}

else if (x==0)
{cout <<"You fail at summoning a spell \n";
cout <<"JUST LIKE YOU FAIL AT LIFE";}
if (EnemyHP > 0);
{cout <<"The grunt attacks you ";
cout <<"The grunt deals " <<HP-(EnemyAttack-Defence) << "Damage";
cout <<"You have"<< HP<< "Health Remaining";
HP=HP-EnemyDamage;}
if (HP<=0)
{cout<<"Game OVER\n";
cout<<"A small voice calls out\n";}
else if (HP>0){cout<<"You find the strength to keep fighting";}

if (EnemyHP <=0)
{ cout <<"The enemy fades away";
cout <<"You win";
cout <<"level a deniag uoY";
cout <<"Your attack  is now" <<Attack-5<< "points ";
Attack=Attack-5;
cout <<"A new foe materializes in place of the grunt";
cout <<"IT IS A SPAWN OF HELLFIRE AND BRIMESTONE";
HP=Health;
goto RealBattle;}

case 3:
cout <<"The Grunt seems to be confused. He has " << EnemyHP <<" Health remaining \n";
if (EnemyHP > 0);
{cout <<"The grunt attacks you ";
cout <<"The grunt deals " <<HP-(EnemyAttack-Defence) << "Damage";
cout <<"You have"<< HP<< "Health Remaining";}
HP=HP-EnemyDamage;
if (HP<=0)
{cout<<"Game OVER\n";
cout<<"A small voice calls out\n";}
else if (HP>0){cout<<"You find the strength to keep fighting";}

break;
case 4:
cout<<"You escaped successfully \n";
default:
cout <<"What are you trying to prove'.\n";
break;
cin >>A;

return main();

break;

} }while(HP > 0 && EnemyHP > 0);
RealBattle:do{  cout <<"Nothing remains all is lost, there is nothing programed here";}while(HP>0&&EnemyHP>0);
system("PAUSE");
return 0;}

• When you say you want help debugging, does that mean that there is something not working properly in the code? – Michael Brandon Morris Jun 16 '16 at 14:22
• It could be my computer and very old g++, but when I entered 1 for Mage I was made a Warrior. – pacmaninbw Jun 16 '16 at 14:30
• I was trying to make it work, but some choices don't work in the combat. Some choices display more than one result. Also since I am new at C++, I don't know if the code works perfectly, or if there is some function I could add into the code that would make it work better. – Mattboo64 Jun 16 '16 at 14:31
• Oh yeah, that is supposed to happen, The game is supposed to subtly make you confused and work against you. The game is supposed to be broken, but working if that makes any kind of sense. – Mattboo64 Jun 16 '16 at 14:33
• Upon further review, it probably would be best for the choices to make sense. I will fix this and add some other suggestions to this code. I decided I want it to work perfectly, not as tool to confuse people and cause chaos. – Mattboo64 Jun 16 '16 at 19:26

On my opinion, it is a good homework job for a high schooler.

If you would like to go further:

1. You should segment your code into smaller parts. So you will be able much easier to change things and extend it.

2. Avoid redundant code. For example, these "Your attack is", "Your defense is" things you programmed many times independently. What if you want to add a new attribute to the player, you will have to change it everywhere.

3. You should create classes, and collect the algorithms and data structures into them.

Anyways, it would be quite useful if you would search for minor, similar software on the net (for example, see github), and see how do they work.

Some (untested) examples:

1.

Collecting the things into a "Player" class:

class Player {
public:
int attack;
int defence;
int magic;
int speed;
int hp;
int difficulty;
int health;

Player(int attack, int defence, int magic, int speed, int hp, int reason, int health) {
this->attack = attack;
this->defence = defence;
this->magic = magic;
this->speed = speed;
this->hp = hp;
this->reason = reason;
this->health = health;
}

void showStat() {
cout <<" Your Attack is " <<Attack;
cout <<" Your Defence is " <<Defence;
cout <<" Your Magic is " <<Magic;
cout <<" Your Speed is " <<Speed;
cout <<" Your HP is " <<HP;
}
};


With it, you can write the code of the printing of the player stats only once.

And, which is more important: you can extend later the functionality of this class, while you don't elevate the complexity of your main program.

2.

Later, you can even use this class for the handling of the monsters with them the player fights. The monsters could be also describe by a "Player" object, around so:

Player firstEnemy(10, 25, ...<stats here>);
firstEnemy.doCombat(playerCharacter);


...where the playerCharacter is the object describing your player. Of course the doCombat() method should be developed by you (probably from calling RealBattle.do() );

3.

I've found a "How to develop an RPG game in C++" tutorial for you here.

4.

This question on the site can be also helpful for you.

5.

Anyways, what I also see, that the problem you've conflicted is the dealing with the code complexity. You have already around 30 local variables in the main() function, and you start to not remember, what is for what. This is a well-known problem in the programming, the whole C++ (and most programming languages) were developed to be able to handle this problem easily. The machine looks everything very well, but you are a human and you don't. You need structures in your code, to understand everything well.

• Downvoted because you listed three very generic things without providing any examples. The user mentioned this was their first program, so "segmenting", "classes", "algorithms", "data structures", etc. may not mean much to them. "Go look at other code elsewhere" is also not an answer IMO. – Michael Brandon Morris Jun 16 '16 at 14:36
• Thanks, I will try that when I get home. I will put the code into smaller parts and avoid redundancy. I have no idea how to create classes and collect the algorithms. Would I be able to find examples of that to make it work? – Mattboo64 Jun 16 '16 at 14:40
• @MichaelBrandonMorris I've extended the question, how about now? – peterh - Reinstate Monica Jun 16 '16 at 14:58
• @Mattboo64 Yes, I've inserted one in the answer. – peterh - Reinstate Monica Jun 16 '16 at 14:59