#include<iostream>
#include<string>
#include <random>
#include<ctime>
usingnamespace std;
int humanNumCurrent, humanNumPast, skeletonNumCurrent, skeletonNumPast;
float humanAttack, humanHealth, humanDamage, skeletonAttack, skeletonHealth, skeletonDamage, AttackOdds;
int main()
{
mt19937 RNG(time(0));
uniform_real_distribution<float> AttackOdds(0.0f, 1.0f);
humanAttack = 0.8f;
humanHealth = 300.0f;
humanDamage = 200.0f;
skeletonAttack = 0.5f;
skeletonHealth = 100.0f;
skeletonDamage = 80.0f;
cout << "*** Humans versus Skeletons ***\n\n";
cout << "Enter the number of humans: ";
cin >> humanNumPast;
cout << "Enter the number of skeletons: ";
cin >> skeletonNumPast;
humanNumCurrent = humanNumPast;
skeletonNumCurrent = skeletonNumPast;
while (humanNumCurrent && skeletonNumCurrent > 0) {
if (AttackOdds(RNG) <= humanAttack) {
skeletonHealth -= humanAttack;
}
if (AttackOdds(RNG) <= skeletonAttack) {
humanHealth -= skeletonAttack;
}
if (humanHealth <= 0) {
humanNumCurrent = humanNumCurrent - 1;
humanHealth = 300.0f;
}
if (skeletonHealth <= 0) {
skeletonNumCurrent = skeletonNumCurrent - 1;
skeletonHealth = 100.0f;
}
}
if (humanNumCurrent = 0) {
cout << "\nSkeletons win.\nThere are " << skeletonNumCurrent << " skeletons left.\n" << humanNumPast - humanNumCurrent << " humans died and " << skeletonNumPast - skeletonNumCurrent << " skeletons died.\n\n";
}
else {
cout << "\nHumans win.\nThere are " << humanNumCurrent << " humans left.\n" << humanNumPast - humanNumCurrent << " humans died and " << skeletonNumPast - skeletonNumCurrent << " skeletons died.\n\n";
}
system("PAUSE");
system("cls");
main();
}
How the script is supposed to work:
The player inserts values for the number of humans and skeletons and then with some preset attack, health and hit chance values I want the two armies to battle.
After the battle I want to output the result of the battle.
The error I'm having is that both armies die no matter what number I put in, so I must made a mistake in the code, but I can't find it.
Can anyone see the problem?
EDIT:
Second question: Right now I have the random number generator outside of the while loop. Does this make it so that the same number is generated every time?
Thanks, the (=) on line 52 was the problem. I'm having problem with the the randomness though. The result of the same variables doesn't really differ that much. For example, if i enter the value 1000 for humanNumPast and 1000 for skeletonNumPast the result is always inbetween 660 and 675 (I tested it about 100 times). Shouldn't it differ more than that?
uniform_real_distribution<float> AttackOdds(0.0f, 1.0f);
Line 15
I'm using the float "skeletonNumCurrent" and "humanNumCurrent" as well as "skeletonNumPast - skeletonNumCurrent" and "humanNumPast - humanNumCurrent.
Line 52 to 56
No, I'm using humanNumPast to calculate the number of humans who died. The variable that changes is humanNumCurrent, and humanNumCurrent is the final result.
If you have the time you could copy the script and try to use it. That probably explains what I want it to do and what it does better than when I try to explain it.
Also, about the random difference thing, I think the reason it doesn't differate that much is because the number of "units" that I'm applying the random effect on, so it averages pretty well.