0

Also im Grunde, wenn ich kompiliere und laufe, wird es den ganzen Weg arbeiten, bis ich "Kampf beginnen !!" aber ich verstehe nicht, was mit meiner Schleife falsch ist. Dieser Code sollte für meine Augen perfekt funktionieren, aber ich brauche Hilfe, um herauszufinden, warum ich schlechte Sicht habe.Mein einfaches C++ Textspiel funktioniert nicht. Ich benutze Zufallszahlen generiert und ich bin verwirrt

#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 Skeletons 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, und es tut mir leid wegen des unordentlichen Codes.

+3

Ich denke, Sie sollten lernen, wie man einen Debugger verwendet. Dies wäre weitaus effektiver und genauer, als wenn die Community rät, wo der Code kaputt ist. –

Antwort

1

Ihre Hauptschleife 'Kampf' wird niemals beendet, da nichts in der Schleife 'hHp' oder 'sHp' aktualisiert. Das heißt, wenn die Schleife 'while' startet, wird sie nie beendet. Auch wenn keine der Unterbedingungen innerhalb der Schleife wahr ist, macht die Schleife gar nichts. Ich denke, dass du einen "else" Fall für beide Unterbedingungen benötigst und auch die HP beider Kreaturen beim Tod auf 0 setzt. Etwas wie:

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

Dies wird für Sie arbeiten:

#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(4, 8); 
    uniform_int_distribution<int> skelhp(50, 100); 
    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) { 

     //Define for pair of units (one human, one skeleton) 
     hAtt = humanatt(randGen); 
     hHp = humanhp(randGen); 
     sAtt = skelatt(randGen); 
     sHp = skelhp(randGen); 

     /*cout << hAtt << endl; 
     cout << hHp << endl; 
     cout << sAtt << endl; 
     cout << sHp << endl; 

     system("PAUSE");*/ 

     //While (somebody in the pair win) 
     while (hHp > 0 && sHp > 0) { 

      //Human move 
      if ((sHp -= hAtt) <= 0) { 
       skeletons--; 
       deadSkeletons++; 

       /*cout << "skeletons: " << skeletons << endl; 
       cout << "deadSkeletons: " << deadSkeletons << endl; 
       system("PAUSE");*/ 
      } 
      //Skeleton move 
      else if ((hHp -= sAtt) <= 0) { 
       humans--; 
       deadHumans++; 

       /*cout << "humans: " << humans << endl; 
       cout << "deadHumans: " << deadHumans << endl; 
       system("PAUSE");*/ 
      } 
      //One more hits iteration 
      /*else { 

       cout << "Battle sounds... " << endl; 
      }*/ 
     } 

    } 

    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 SKELETONS" << endl; 
      cout << "However, at the cost of " << deadHumans << " of they're own" << endl; 
      cout << "Only " << humans << " remain"; 
     } 
    } 
    system("PAUSE"); 
    return 0; 
} 

immer schreiben, was Sie vor der Codierung tun möchten. Debuggen Sie Ihren Code Schritt für Schritt wie in kommentierten Regionen. Prost!

Verwandte Themen