I'm trying to create a text based RPG, like pokemon.
At the moment i'm trying to make a store to buy health and stuff in.
Everytime you kill a monster you get a random amount of money.
But how do i make the program say "You can't afford that" if you don't have enough money?
And, why does the program always generate the same number?
#include <cstdlib>
#include <iostream>
#include <windows.h>
using namespace std;
class HJALTEN//the player
{
public:
int liv;//life
int pengar;//pengar
void merliv()// more life
{
liv++;
}
void mindreliv()//less life
{
liv--;
}
void merpengar()
{
pengar++;
}
void mindrepengar()
{
pengar--;
}
};
class MONSTER// the opponent
{
public:
int liv;
void merliv2()
{
liv++;
}
void mindreliv2()
{
liv--;
}
};
int main()
{
int random;
int random2;
int random3;
int random4;
int vinst;
int val;
int val3;
int val2;
HJALTEN t;
MONSTER h;
h.liv = 5;
t.liv = 10;
t.pengar = 10;
system("CLS");
cout << "\a";
Sleep(400);
cout << "\a";
cout << "\a";
cout << "\a";
Sleep(800);
cout << "\a";
Sleep(800);
cout << "\a";
Sleep(400);
cout << "\a";
start:
system("CLS");
cout << "What do you want to do?" << endl;
cout << "1. Find an opponent" << endl;
cout << "2. Buy stuff in the store" << endl;
cout << "3. Quit" << endl;
cin >> val2;
switch (val2)
{
case 1:
if(t.liv <=0)
{
cout << "\nyou're dead" << endl;
system("pause");
}
cout << "\a";
system("CLS");
cout << "you've encountered a monster!" << endl << endl;
Sleep(300);
mini:
system("CLS");
cout << " you have " << t.liv << " HP" ;
cout << " monster has " << h.liv << " HP";
cout <<"\nWhat do you want to do?" << endl;
cout << "1. Hit monster" << endl;
cout << "2. Escape" << endl;
cin >> val;
srand( (unsigned)time( NULL ) );
for(int randomm=0; randomm<2; randomm++){
random2 = (rand()%10)+1;
if(randomm=1)
{
if(h.liv <= 0)
{
system("CLS");
cout << "you won!" << endl;
Sleep(500);
for(int rando2=0; rando2<2; rando2++){
random = (rand()%10)+1;
if(rando2 == 1)
{
cout << "du har fatt 8 pengar!" << endl;
t.merpengar();
t.merpengar();
t.merpengar();
t.merpengar();
t.merpengar();
t.merpengar();
t.merpengar();
t.merpengar();
}
else if (rando2 == 2)
{
cout << "du har fatt 12 pengar!" << endl;
t.merpengar();
t.merpengar();
t.merpengar();
t.merpengar();
t.merpengar();
t.merpengar();
t.merpengar();
t.merpengar();
t.merpengar();
t.merpengar();
t.merpengar();
t.merpengar();
}
else{cout <<"du har fatt 4 pengar!" << endl;
t.merpengar();
t.merpengar();
t.merpengar();
t.merpengar();
}
system("pause");
goto start;
}
}
}
switch(val)
{
case 1:
{
for(int rando3=0; rando3<2; rando3++){
random4 = (rand()%10)+1;
if(rando3==1)
{
cout << "monster lost 2 hp!" << endl;
h.mindreliv2();
h.mindreliv2();
goto mini;
}
else if (rando3=2)
{
cout << "monster lost 1 hp!" << endl;
h.mindreliv2();
goto mini;
}
}
case 2:
{
cout << "COWARD" << endl;
Sleep(500);
goto start;
}
}
}
}
case 2: {shop:
system("CLS");
cout << " You have " << t.pengar << " money and " << t.liv <<" HP" << endl;
cout << "\n\n\n1.buy 5 HP for 4 money" << endl;
cout << "2.buy 10 HP for 8 money" << endl;
cout << "3. go back to the menu" << endl;
cin >> val3;
switch (val3)
{
case 1:
{ if(t.pengar<=0)
{
cout <<"you can't afford that" << endl;
cout << "\a";
goto shop;
}
else{
cout << "thanks" << endl;}
t.merliv();
t.merliv();
t.merliv();
t.merliv();
t.merliv();
t.mindrepengar();
t.mindrepengar();
t.mindrepengar();
t.mindrepengar();
goto shop;}
case 2:
{ if(t.pengar<=0)
{
cout <<"you can't afford that!" << endl;
Sleep(500);
cout << "\a";
goto shop;
}
else{cout << "thanks" << endl;
Sleep(500);
t.merliv();
t.merliv();
t.merliv();
t.merliv();
t.merliv();
t.merliv();
t.merliv();
t.merliv();
t.merliv();
t.merliv();
t.mindrepengar();
t.mindrepengar();
t.mindrepengar();
t.mindrepengar();
t.mindrepengar();
t.mindrepengar();
t.mindrepengar();
t.mindrepengar();
goto shop;}
}
}
case 3:
{
goto start;
}
}
}
}
i'm swedish, but i've translated the important stuff to english.
Any tips on how i can improve it?
Please give me some advice too, i'm new to C++.
Oh yeah, is there any other way than using goto?
Cuz, for example, when i used goto after killing the opponent, next time you encounter an enemy,
it's health is at 0.