2016-04-09 7 views
1

私はC++を初めて使用しており、現在は割り当てのための "Death Battle"ゲームに取り組んでいます。C++「Death Battle」ゲームで武器の使用回数が正しく記録されない

基本的に、プレイヤーは、各武器を一定回数使用するようになります:キヤノン3回、グレネード4回。ライフルは無限です。

キャノンを3回使用するとうまく動作し、キャノンが外れてから手榴弾を選ぶと1回動作します。私が手榴弾をもう一度使ってみると、私は大砲の外にいるというメッセージを私に伝えます。私の2番目のライフルの使用でも同じことが起こります。

これはなぜ、またはそれを修正する方法がわかりません。誰かが私にこれを正しい方向に向けることができたら、私は大変感謝しています!

は、ここに私のコードです:

また
#include <iostream> 
#include <ctime> 
#include <cstdlib> 
#include <iomanip> 

using namespace std; 


int main() 
{ 
    int PlayerHealth = 100, 
    CompHealth = 100, 
    Turn = 0, 
    WeaponChoice, 
    CompWeapon, 
    Cannon_Damage = 0, 
    Grenade_Damage = 0, 
    Rifle_Damage = 0; 
    int PlayerCanonUse = 3; 
    int PlayerGrenadeUse = 4; 
    int CompCanonUse = 3; 
    int CompGrenadeUse = 4; 


    srand(static_cast<int>(time(0))); 

    char yn; 
    do 
    { 

     do 
     { 

      if(Turn == 0)// Player Turn 
      { 

       cout << "\nWhich weapon will you use? Choose 1, 2, or 3.\n"; 
       cout << "1. Cannon\n"; 
       cout << "2. Grenade\n"; 
       cout << "3. Rifle\n\n"; 
       cin >> WeaponChoice; 

       //Validate weapon choice 

       if ((WeaponChoice < 1 || WeaponChoice > 3)){ 
        cout << "\nSilly you. That's not one of the options. Try again.\n"; 
        cin >> WeaponChoice; 
       } 

       if (PlayerCanonUse <= 0){ 
        cout << "Sorry, you have used all of your canons. Choose a different weapon.\n"; 
       cin >> WeaponChoice; 
       } 

       if (PlayerGrenadeUse <= 0){ 
        cout << "Sorry, you have used all of your grenades. Choose a different weapon.\n"; 
        cin >> WeaponChoice; 
       } 

       switch(WeaponChoice) 
       { 

        case 1: // player chooses to attack with cannon 
         Cannon_Damage = (10 + rand() % 6); 
         cout << "\nYou chose a cannon." << endl; 
         CompHealth = CompHealth - Cannon_Damage; 
         cout << setw(10) << Cannon_Damage << " damage!" << endl; 
         cout << setw(10) << "YOUR HEALTH: " << PlayerHealth << endl; 
         cout << setw(10) << "OPPONENT HEALTH: " << CompHealth << endl; 
         PlayerCanonUse = PlayerCanonUse - 1; 
         break; 


        case 2: 
         Grenade_Damage = (7 + rand() % 13); 
         cout << "\nYou chose a grenade." << endl; 
         CompHealth = CompHealth - Grenade_Damage; 
         cout << Grenade_Damage << " damage!" << endl; 
         cout << "YOUR HEALTH: " << PlayerHealth << endl; 
         cout << "OPPONENT HEALTH: " << CompHealth << endl; 
         PlayerGrenadeUse = PlayerGrenadeUse - 1; 

         break; 

        case 3: 
         Rifle_Damage = (3 + rand() % 9); 
         cout << "\nYou chose a Rifle." << endl; 
         CompHealth = CompHealth - Rifle_Damage; 
         cout << Rifle_Damage << " damage!" << endl; 
         cout << "YOUR HEALTH: " << PlayerHealth << endl; 
         cout << "OPPONENT HEALTH: " << CompHealth << endl; 

         break; 
       } 



      } 



      Turn == 1; // Computer Turn 

      CompWeapon = rand() % 3; 


      switch(CompWeapon) 
      { 

       case 1: 

        Cannon_Damage = (10 + rand() % 6); 
        cout<<"\nYour opponent used a cannon, and you lost " << Cannon_Damage << " HP." << endl; 
        PlayerHealth = PlayerHealth - Cannon_Damage; 
        cout << "YOUR HEALTH: " << PlayerHealth << endl; 
        cout << "OPPONENT HEALTH: " << CompHealth << endl; 
        CompCanonUse = PlayerCanonUse - 1; 
        break; 

       case 2: 
        Grenade_Damage = (7 + rand() % 6); 
        cout<<"\nYour opponent used a grenade, and you lost " << Grenade_Damage << " HP." << endl; 
        PlayerHealth = PlayerHealth - Grenade_Damage; 
        cout << "YOUR HEALTH: " << PlayerHealth << endl; 
        cout << "OPPONENT HEALTH: " << CompHealth << endl; 
        CompGrenadeUse = CompGrenadeUse - 1; 
        break; 

       case 3: 
        Rifle_Damage = (3 + rand() % 10); 
        cout<<"\nYour opponent used a rifle, and you lost " << Rifle_Damage << " HP." << endl; 
        PlayerHealth = PlayerHealth - Rifle_Damage; 
        cout << "YOUR HEALTH: " << PlayerHealth << endl; 
        cout << "OPPONENT HEALTH: " << CompHealth << endl; 
        break; 


      } 

     } 
     while(PlayerHealth >= 0 && CompHealth >= 0); //loops while both players are alive 
     if (CompHealth < 0) 
      cout << "Congratulations! You won!" << endl; 

     if (PlayerHealth < 0) 
      cout << "Darn, you lost. Game over." << endl;; 

     std::cout << "That was fun. Would you like to play again? Enter Y or N:" << std::flush; 
    } while(std::cin >> yn && (yn == 'Y' || yn == 'y')); 
} 

、私はC++の規則に従わない午前なら、私に言い訳してください、私はまだそれに新たなんです!その上の任意のポインタも良いでしょう。

+1

プレーヤーが実際に大砲を選んだ場合、 'PlayerCanonUse <= 0'をチェックするだけです。 –

+0

ありがとう!私はそれが単純なものだったように感じました。これは私が必要だったものです。 –

答えて

1

ここに問題があります。

   if (PlayerCanonUse <= 0){ 
        cout << "Sorry, you have used all of your canons. Choose a different weapon.\n"; 
       cin >> WeaponChoice; 
       } 

       if (PlayerGrenadeUse <= 0){ 
        cout << "Sorry, you have used all of your grenades. Choose a different weapon.\n"; 
        cin >> WeaponChoice; 
       } 

この2つのif文は、武器を選択しなくてもチェックされます。 スイッチ内部には、このチェックを行うには単純だろう

  if (WeaponChoice == 1 && PlayerCanonUse <= 0){ 
       cout << "Sorry, you have used all of your canons. Choose a different weapon.\n"; 
      cin >> WeaponChoice; 
      } 

      if (WeaponChoice == 2 && PlayerGrenadeUse <= 0){ 
       cout << "Sorry, you have used all of your grenades. Choose a different weapon.\n"; 
       cin >> WeaponChoice; 
      } 

のようなもので固定することができます。

+0

以下も修正が必要です: "CompCanonUse = PlayerCanonUse - 1;" – MikeC

関連する問題