0

基本的に私がコンパイルして実行すると、 "Begin combat !!"私のループに何が間違っているのか分かりません。このコードは、私の目にはまったく問題なく動作するはずですが、なぜ私に悪いビジョンがあるのか​​を理解する助けが必要です。私の単純なC++テキストゲームは動作しません。私は乱数を生成して使用しています。私は混乱しています

#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; 
} 

ああ、私は汚いコードについて申し訳ありません。

+3

私はあなたがデバッガを使用する方法を学ぶべきだと思います。これはコミュニティがあなたのコードがどこで壊れているかを推測させるよりもずっと効果的で正確です。 –

答えて

1

メイン '戦闘'ループは、ループ内の何もないので、 'hHp'または 'sHp'を更新しません。これは、 'while'ループが開始しても終了しないことを意味します。また、ループ内のいずれのサブ条件も真でない場合、ループは何もしません。私はあなたが両方のサブコンディションのために「else」ケースを必要としていると思っています。また、両方の生き物のhpを0に設定して死にました。ような何か:

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

これはあなたのために動作します:

#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; 
} 

は常にあなたがコーディングの前に何をしたいのか書きます。コメントされた地域のように、コードをステップバイステップでデバッグしてください。 乾杯!

関連する問題