Hello. Currently I am working on a text based rpg game in C++. During an encounter I want a monster to give damage in between 1 and 4. Then I want this number to be subtracted from the players total hp. The problem is I need this number to change every time it is used.
Example:
Monster attacks player inflicting (random number between 1 and 4)
Player's total hp is now (player's hp - random number chosen)
Monster attacks player again inflicting (new random number between 1 and 4)
Player's total hp is now (player's hp - new random number)
How would I go about doing this. Any help would be much appreciated.
You can use the expression (rand() % 4) + 1 to get a random number from 1 to 4 inclusive. You will need to include the header cstdlib and seed the random number generator with srand() before using it.
#include <iostream>
#include <ctime>
usingnamespace std;
//Declare function
int RandomDamage(int,int);
int main()
{
//Pick a seed for random based off current
//time so each run will produce different results
srand(time(0));
int damage = RandomDamage(1,4);
cout << "Damage dealt: " << damage << "\n";
system("pause");
return 0;
}
int RandomDamage(int minDamage, int maxDamage)
{
int r = rand();
int range = (maxDamage - minDamage) + 1;
r = (r % range) + minDamage;
return r;
}
HellfireXP i tried your code and the problem is i need the random number to change after each use for instance the first time it is 4 and the second it is 1.
Each time you call the function RandomDamage() it should return a different number. The example above only runs one time, but if you restart it a few times, you should see different results.
I use Visual C++ 2010 Express as my IDE and the above code works just fine for me.
#include <iostream>
#include <cstdlib>
#include <ctime>
usingnamespace std;
//Declare function
int RandomDamage(int,int);
int main()
{
//Pick a seed for random based off current
//time so each run will produce different results
srand(time(0));
for (int i=0; i<10; i++) {
int damage = RandomDamage(1,4);
cout << "Damage dealt: " << damage << "\n";
}
system("pause");
return 0;
}
int RandomDamage(int minDamage, int maxDamage)
{
int r = rand();
int range = (maxDamage - minDamage) + 1;
r = (r % range) + minDamage;
return r;
}
Ok what if i want the damage to be dealt (between 1 and 4) then the player to attack and then a new damage dealt (between 1 and 4). Don't include the player attacking in the code. It is just used as an example to show that i need the attacks to be spaced out.
int RandomDamage() is a function. So it can be called anytime you need it in your code...
So you can call RandomDamage(1,4) in one location in your code, then call it again later... or let's say you encounter a stronger monster that does damage 5-8, then you'd simple call RandomDamage(5,8).
In other words, the RandomDamage() function is flexible for any damage amounts and can be called multiple times from anywhere in your code.
What i want is it so whenever i call RandomDamage () it is a different number. However since I do not know how many attacks it will make during a fight i can not name a variable every time i want to use RandomDamage(). I need a variable that each time it is used it changes numbers between 1 and 4.
Example:
int RandomNumber = RandomDamage(1,4);
int hp = 20;
while( hp>0){
Cout << "Monster deals " << RandomNumber << "damage.";
cout << "Your hp is now " << hp-RandomNumber << ".";
//lets say RandomNumber was equal to 2
hp=hp-RandomNumber
}
Next the player will attack and it will restart. However when the loop restarts the integer RandomNumber needs to be a random number between 1 and 4, not 2 again.
Everytime you call RandomDamage() you will get a new number. In the code you posted, a minor tweak (move location of int RandomNumber = RandomDamage(1,4)) and you'll have the answer you're looking for...
1 2 3 4 5 6 7 8 9
int hp = 20;
while( hp>0){
int RandomNumber = RandomDamage(1,4);
Cout << "Monster deals " << RandomNumber << "damage.";
cout << "Your hp is now " << hp-RandomNumber << ".";
//lets say RandomNumber was equal to 2
hp=hp-RandomNumber;
}
So each time the while() loop wraps around, the RandomDamage() function will pass a NEW number between 1 and 4 to the RandomNumber integer variable.
Sorry if I was being dumb and did not understand what you were going at before.
On the forums is there something i click to show the problem has been solved or do i just leave it as is.