So, I used the code for level * 10 and so forth to get a random number, but every time I run the program, the damage that the player does and the monster does are the same.
For example, I run it and it says:(making up numbers, just an example)
Player does 12 damage.
Monster does 5 damage.
Player does 10 damage.
Monster does 8 damage.
and so forth until one dies. Then I run the program and fight again and get this:
Player does 12 damage.
Monster does 5 damage.
Player does 10 damage.
Monster does 8 damage.
Its the same exact every time. Why is it this way?(Edit: it seems any random number is the same every time I run the program)
Also, after it asks your name in the main function, I put a for loop, and then ended it right before the main function ends. When I do the fight, it runs, then when the menu comes back up again, I try to do fight again, but when I type 1 for fight, it just brings the menu again. The shop and use items both work fine.
Here is my code:
I'm obviously no expert so my code might look messy.
- Code: Select all
#include "stdafx.h"
#include <iostream>
#include <string>
#include <windows.h>
using namespace std;
int GetRand(int low, int high)
{
return (int)(rand() / (RAND_MAX + 1.0) * (high - low) + low);
}
class player
{
private:
int health;
int pdamage;
int mdamage;
int healthLoss;
int goldDrop;
int monsterHealth;
int exp;
public:
int level;
int smallPotions;
int largePotions;
int gold;
player():
health(100), gold(500), monsterHealth(50), smallPotions(0), largePotions(0), level(1)
{
}
void useSmallPotion()
{
cout << "You use a Small Healing Potion.\n";
health += 25;
cout << "Your health is now " << health << "!\n";
smallPotions--;
}
void useLargePotion()
{
cout << "You use a Large Healing Potion.\n";
health += 50;
cout << "Your health is now " << health << "!\n";
largePotions--;
}
void attack()
{
while(monsterHealth > 0 && health > 0)
{
pdamage = GetRand((level * 10) - 10, (level * 10) + 10);
cout << "You attack the Forest Bear for " << pdamage << " health.\n";
monsterHealth -= pdamage;
Sleep(1000);
mdamage = GetRand((level * 10) - 15, (level * 10) + 5);
cout << "Forest Bear bites you for " << mdamage << " health!\n";
health -= mdamage;
Sleep(1000);
if(monsterHealth <= 0)
{
cout << "Forest Bear dies.\n";
Sleep(1000);
goldDrop = GetRand(0, 50);
cout << "You loot the bear and find " << goldDrop << " gold!\n";
gold += goldDrop;
Sleep(1000);
cout << "You now have " << health << " health and " << gold << " gold.\n";
}
if(health <= 0)
{
cout << "You have died.\n";
Sleep(1000);
}
}
}
};
int main()
{
int a;
string name;
player one;
cout << "Hello and welcome to Combat v1.0\n";
Sleep(1000);
cout << "May I ask, what is your name?\n";
cin >> name;
cout << "Hello " << name << "!\n";
for(;;)
{
cout << "What would you like to do?\n";
cout << "1. Fight\n" << "2. Shop\n" << "3. Use Items\n";
cin >> a;
switch(a)
{
case 1:
{
one.attack();
break;
}
case 2:
{
cout << "Welcome to A Warrior's Supply Shop\n\n";
cout << "Would you like to buy:\n";
cout << "1. Small Healing Potion (for 50 gold)\n";
cout << "2. Large Healing Potion (for 100 gold)\n";
cin >> a;
switch(a)
{
case 1:
{
one.gold -= 50;
one.smallPotions += 1;
Sleep(1000);
cout << "You now have " << one.gold << " gold left.";
break;
}
case 2:
{
one.gold -= 100;
one.largePotions += 1;
Sleep(1000);
cout << "You now have " << one.gold << " gold left.";
break;
}
}
break;
}
case 3:
{
cout << "Which item would you like to use?\n\n";
cout << "1. Small Healing Potion (You own " << one.smallPotions << ")\n";
cout << "2. Large Healing Potion (You own " << one.largePotions << ")\n";
cin >> a;
switch(a)
{
case 1:
{
if(one.smallPotions > 0)
{
one.useSmallPotion();
}
else
{
cout << "Sorry, you do not have any left.";
}
break;
}
case 2:
{
if(one.largePotions > 0)
{
one.useLargePotion();
}
else
{
cout << "Sorry, you do not have any left.";
}
break;
}
}
}
}
}
return 0;
}
