mercredi 15 juin 2016

My simple c++ text game doesn't work. i'm using random number generates and i'm confused

So basically when I compile and run, it will work all the way up until I "Begin combat!!" but I don't understand what is wrong with my loop. This code should work perfectly fine to my eyes, but I need some help figuring out why I have bad vision.

#include <iostream>
#include <ctime>
#include <random>

using namespace std;

int main() {
    mt19937 randGen(time(NULL));
    uniform_int_distribution<int> humanatt(4, 8);
    uniform_int_distribution<int> humanhp(50, 100);
    uniform_int_distribution<int> skelatt(12, 17);
    uniform_int_distribution<int> skelhp(70, 85);
    int hAtt, hHp, sAtt, sHp;
    unsigned long humans, skeletons, deadHumans = 0, deadSkeletons = 0;
    cout << "*** Skeletons vs Humans ***" << endl;
    cout << "Input the number of humans: ";
    cin >> humans;
    cout << "Input the number of skeletons: ";
    cin >> skeletons;
    cout << "Beginning combat!!" << endl << endl << "(combat noises)" << endl;

    while ((humans > 0) && (skeletons > 0)) {
        hAtt = humanatt(randGen);
        hHp = humanhp(randGen);
        sAtt = skelatt(randGen);
        sHp = skelhp(randGen);

        while ((hHp > 0) && (sHp > 0)) {
            if (hAtt >= sHp) {
                skeletons--;
                deadHumans++;
            }
            if (sAtt >= hHp) {
                humans--;
                deadHumans++;
            }
        }

    }
    cout << endl << "Combat has ended!" << endl << endl;
    if ((humans == 0) && (skeletons == 0)) {
        cout << "They all died!" << endl;
        cout << "This was a battle with no winner" << endl;
        cout << deadHumans + deadSkeletons << " men and bone-men, gave gave there lives this day";
    }
    else {
        if (humans == 0) {
            cout << "The Sketeletons Win!" << endl;
            cout << "They killed all " << deadHumans << " of the humans" << endl;
            cout << "However, at the cost of " << deadSkeletons << " of they're own" << endl;
            cout << "Only " << skeletons << " remain";
        }
        else {
            cout << "The Humans Win!" << endl;
            cout << "They killed all " << deadSkeletons << " of the humans" << endl;
            cout << "However, at the cost of " << deadHumans << " of they're own" << endl;
            cout << "Only " << humans << " remain";
        }
    }
    system("PAUSE");
    return 0;
}

oh and I'm sorry about the messy code.




Aucun commentaire:

Enregistrer un commentaire