Help with code while loop
Oct 23, 2015 at 9:07pm UTC
I recently started a tutorial and the following is my take on the first challenge episode. For some reason one of two things is happening. Either it doesn't detect when one of the humans/skeletons reaches 0 or it makes the numHumans you enter numSkeletons or vice versa. I know it sounds confusing but if you can tell me if you see anything weird let me know!
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 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
#include <iostream>
#include <string>
#include <random>
#include <ctime>
using namespace std;
int numHumans;
int numberSkeletons;
int humanHealth = 100;
int skeletonHealth = 80;
double humanAttack;
double skeletonAttack;
int humanDamage;
int skeletonDamage;
int deadHumans;
int deadSkeletons;
string firstAttack;
char turn = 'H' ;
int main() {
// Intro
cout << "*** Humans vs Skeletons ***\n" ;
cout << "***************************\n" ;
// Get info
cout << "\nHow many humans:\n" ;
cin >> numHumans;
cout << "\nHow many skeletons:\n" ;
cin >> numberSkeletons;
// Who should go first?
cout << "\nWho will fire the first shot? (Skeletons or Humans)\n" ;
cin >> firstAttack;
// Set who goes first based on input
if (firstAttack == "humans" || firstAttack == "Humans" || firstAttack == "H" || firstAttack == "h" ) {
turn = 'H' ;
}
else {
turn = 'S' ;
}
//Beginnning Battle Text
cout << "***************************\n" ;
cout << "**** Battle Beginning! ****\n" ;
cout << "***************************\n" ;
cout << "\n*Battle Noises*\n" ;
//Battle Simulation
mt19937 randomGenerator(time(NULL)); //Random number Generator
uniform_real_distribution<float > attackChance(0.0, 1.0); // Attack chance generator
while ((numberSkeletons > 0) || (numHumans > 0)) {
if (turn == 'H' ) {
uniform_int_distribution<int > damageAmount(70, 90); // Damage dealt generator
humanAttack = attackChance(randomGenerator);
if (humanAttack <= 0.6) {
humanDamage = damageAmount(randomGenerator);
skeletonHealth -= humanDamage;
if (skeletonHealth <= 0) {
numberSkeletons--;
deadSkeletons++;
skeletonHealth = 80;
turn = 'S' ;
}
else {
turn = 'S' ;
}
}
}
else {
uniform_int_distribution<int > skeletonDamageAmount(40, 65); // Damage dealt generator
skeletonAttack = attackChance(randomGenerator);
if (skeletonAttack <= 0.5) {
skeletonDamage = skeletonDamageAmount(randomGenerator);
humanHealth -= skeletonDamage;
if (humanHealth <= 0) {
numHumans--;
deadHumans++;
humanHealth = 100;
turn = 'H' ;
}
else {
turn = 'H' ;
}
}
}
}
// Battle over
// End battle information
cout << "\n***************************\n" ;
cout << "*** Battle Over ***\n" ;
cout << "***************************\n" ;
// Determine & output the winner
if (numHumans == 0) {
cout << "\nHumans lost!\n" ;
cout << "\nNumber of dead humans: " << deadHumans << endl;
cout << "\nNumber of dead skeletons: " << deadSkeletons << endl;
cout << "***************************\n" ;
}
else if (numberSkeletons == 0) {
cout << "\nHumans won!\n" ;
cout << "\nNumber of dead skeletons: " << deadHumans << endl;
cout << "\nNumber of dead humans: " << deadSkeletons << endl;
cout << "***************************\n" ;
}
system("PAUSE" );
return 0;
}
Oct 23, 2015 at 10:43pm UTC
Perhaps a clue as to which line detection occurs would help us help you.The second item you mention also needs expanding on by you. :)
Topic archived. No new replies allowed.