Help with battle sim.
Dec 9, 2015 at 7:19pm UTC
Could someone show me what I've done wrong in my battle simulation game. The health of the skeletons and humans will not decrease.
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 63 64 65 66 67 68 69
#include <iostream>
#include <string>
#include <ctime>
#include <random>
using namespace std;
int main()
{
cout << "Welcome to war simulator, please input the amount of troops\n" ;
int skeletons = 0;
int humans = 0;
int skeletonDeath = 0;
int humanDeath = 0;
int skeletonHealth = 100;
int humanHealth = 100;
int skeletonDamage = 50;
int humanDamage = 100;
cout << "how many skeletons are there?\n" ;
cin >> skeletons;
cout << "how many humans are there?\n" ;
cin >> humans;
cout << "there are " << skeletons << " skeletons, and " << humans <<" humans" << endl;
mt19937 rng(time(0));
uniform_real_distribution<float >allRoll(0.0f, 1.0f);
while ((humans > 0) || (skeletons > 0))
{
int humanHealth = 100;
if (skeletonHealth < 100) {
int skeletonHealth = 50;
}else {
int skeletonHealth = 100;
};
float sattack = allRoll(rng);
if (sattack <= 0.6f) {
humanHealth - skeletonDamage;
}
else {
skeletonHealth - humanDamage;
};
float hattack = allRoll(rng);
if (hattack <= 0.5f) {
skeletonHealth - humanDamage;
}
else {
humanHealth - skeletonDamage;
};
cout << skeletonHealth << humanHealth << endl;
if (humanHealth == 0) {
humans - 1;
};
if (skeletonHealth == 0) {
skeletons - 1;
};
system("PAUSE" );
};
if (humans == 0){
cout << "Skeletons win!" << endl;
}else {
cout << "humans win!" << endl;
};
return 0;
}
Dec 9, 2015 at 7:35pm UTC
humanHealth - skeletonDamage
is just a calculation. You need an assignment as well. Use humanHealth = humanHealth - skeletonDamage
, or humanHealth -= skeletonDamage
, for all such occurrences.
Dec 9, 2015 at 8:10pm UTC
i've updated it, I did realize that i needed to change it to " -=" but now when i run it, it will always say that humans win and it wont count the deaths
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 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
#include <iostream>
#include <string>
#include <ctime>
#include <random>
using namespace std;
int main()
{
cout << "Welcome to war simulator, please input the amount of troops\n" ;
int skeletons = 0;
int humans = 0;
int skeletonDeath = 0;
int humanDeath = 0;
int skeletonHealth = 100;
int humanHealth = 100;
int skeletonDamage = 50;
int humanDamage = 100;
cout << "how many skeletons are there?\n" ;
cin >> skeletons;
cout << "how many humans are there?\n" ;
cin >> humans;
cout << "there are " << skeletons << " skeletons, and " << humans <<" humans" << endl;
mt19937 rng(time(0));
uniform_real_distribution<float >allRoll(0.0f, 1.0f);
while ((humans <= 0) || (skeletons <= 0))
{
int humanHealth = 100;
if (skeletonHealth < 100) {
int skeletonHealth = 50;
}else {
int skeletonHealth = 100;
};
float sattack = allRoll(rng);
if (sattack <= 0.6f) {
humanHealth=(humanHealth - skeletonDamage);
}
else {
skeletonHealth=(skeletonHealth- humanDamage);
};
if (humanHealth <= 0) {
humans -= 1;
};
if (skeletonHealth <= 0) {
skeletons -= 1;
};
float hattack = allRoll(rng);
if (hattack <= 0.5f) {
skeletonHealth =(skeletonHealth - humanDamage);
}
else {
humanHealth =(humanHealth- skeletonDamage);
};
if (humanHealth <= 0) {
humans -= 1;
humanDeath=(humanDeath + 1);
};
if (skeletonHealth <= 0) {
skeletons -= 1;
skeletonDeath=(skeletonDeath + 1);
};
cout <<"skeleton" << skeletonHealth <<"human" << humanHealth <<"sleft" <<skeletons<<"hleft" <<humans<< endl;
};
if (humans <= 0){
cout << "Skeletons win!" << endl;
}else {
cout << "humans win!" << endl;
};
cout << skeletonDeath << " skeleton's died and " << humanDeath << " human's died" << endl;
system("PAUSE" );
return 0;
}
Dec 9, 2015 at 8:33pm UTC
I had my signs backwards boys and girls. thank you for your help everyone.
Topic archived. No new replies allowed.