Need help with script!

Hey! I'm trying to learn c++ and tried to create a combat simulator. This is the script:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include<iostream>
#include<string>
#include <random>
#include<ctime>

using namespace 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?
Last edited on
Line 52: You're using the assignment operator (=), not the comparison operator (==).

Line 35: Probably not significant, but your while condition is as follows:
 
  while (humanNumCurrent != 0 && skeletonNumCurrent > 0) 

if humanNumCurrent were to go negative, the first part of the condition would be true.

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?
What is the variable you are storing the final result to?
What is the random function you are using?
Random function:

mt19937 RNG(time(0));
Line 13

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

All of these variables are floats.
Last edited on
There is one more.

...the result is always inbetween 660 and 675 (I tested it about 100 times)

What variable stores that result?
humanNumCurrent
Last edited on
Enter the number of humans: 100
1
2
	cin >> humanNumPast;
	humanNumCurrent = humanNumPast;

So the result still stays the same regardless of the number of humans you have input?
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.
Last edited on
If you want the results to differ more why don't you use it :
1
2
3
std:: uniform_real_distribution humanDamage<float, float> (100.0f, 200.0f);

std:: uniform_real_distribution skeletonDamage<float, float> (80.0f, 160.0f);


1
2
3
4
5
6
7
if (AttackOdds(RNG) <= humanAttack) {
		skeletonHealth -= humanDamage(RNG);
		}

		if (AttackOdds(RNG) <= skeletonAttack) {
		humanHealth -= skeletonDamage(RNG);
		}


Hey, fyi : When writing this I notice humanDamage & skeletonDamage were never used!!
Topic archived. No new replies allowed.