2016-07-19 16 views
0

私のプログラムに問題があり、何か助けが必要です。私のプログラムは、誰が最初にやって来るのか、ユーザーか、コンピュータをランダムに選ぶことになっています。プログラムはちょうど良いことです。使用したい武器を入力すると、適切な計算が行われ、コンピュータが最初に行かれればランダムに生成されたダメージの量が減算されます。最初のユーザーかコンピュータかに関係なく、Enterキーを押すとプログラムが停止します。ユーザーまたはコンピュータの状態がゼロになるまで続行されるはずです。私は、私のコードがこれが起こらないように助けを得ることができるかどうか疑問に思っていました。どんな助けも高く評価されます。私はC++言語の初心者です。ありがとうございました!XCode C++プログラムが適切なループを実行していない

#include <cstdlib> 
#include <iostream> 
#include <cmath> 
#include <ctime> 

using namespace std; 

int main() // Setting the "stage" for the code to follow. 
{ 
    int Player_Health = 100; // Creating the necessary variables for the program. 
    int Comp_Health = 100; 
    int Turn; 
    int Weapon_Selection; 
    int Comp_Weapon; 
    int Cannon_Dmg; 
    int Grenade_Dmg; 
    int Rifle_Dmg; 
    int Player_Cannon_Ammo = 3; 
    int Player_Grenade_Ammo = 4; 
    int Comp_Cannon_Ammo = 3; 
    int Comp_Grenade_Ammo = 4; 

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

    Turn = rand() % 2; // Randomizing who gets to go first. 

    while(Player_Health >= 0 && Comp_Health >= 0) 
    { 
     if(Turn == 0) // Player gets to go first. 
     { 
      cout << "You get to go first!\n"; 
      cout << "Select a weapon using number keys 1, 2, or 3.\n"; 
      cout << "1. Cannon\n"; 
      cout << "2. Grenade\n"; 
      cout << "3. Rifle\n"; 
      cout << "\n"; 
      cout << "You select: "; 
      cin >> Weapon_Selection; 

      while((Weapon_Selection < 1 || Weapon_Selection > 3) || (Weapon_Selection == 1 && Player_Cannon_Ammo == 0) 
        || (Weapon_Selection == 2 && Player_Grenade_Ammo == 0)) 
      { 
       cout << "Please enter a valid option.\n"; 
       cout << "You select: "; 
       cin >> Weapon_Selection; 
      } 

      switch(Weapon_Selection) 
      { 
       case 1: // Player chooses to shoot the cannon. 
        Cannon_Dmg = 10 + rand() % 6; 
        Comp_Health = Comp_Health - Cannon_Dmg; 
        cout << "You caused " << Cannon_Dmg << " damage to your enemy.\n"; 
        cout << "Your health is " << Player_Health << endl; 
        cout <<" The computer's health is " << Comp_Health << endl; 
        Player_Cannon_Ammo = Player_Cannon_Ammo - 1; 
        break; 

       case 2: // Player chooses to lob a grenade. 
        Grenade_Dmg = 7 + rand() % 6; 
        Comp_Health = Comp_Health - Grenade_Dmg; 
        cout << "You caused " << Grenade_Dmg << " damage to your enemy.\n"; 
        cout << "Your health is " << Player_Health << endl; 
        cout << "The computer's health is " << Comp_Health << endl; 
        Player_Grenade_Ammo = Player_Grenade_Ammo - 1; 
        break; 

       case 3: // Player chooses to shoot the rifle. 
        Rifle_Dmg = 3 + rand() % 6; 
        Comp_Health = Comp_Health - Rifle_Dmg; 
        cout << "You caused " << Rifle_Dmg << " damage to your enemy.\n"; 
        cout << "Your health is " << Player_Health << endl; 
        cout << "The computer's health is " << Comp_Health << endl; 
        break; 


      } 


     } 

     else // Computer gets to go first. 
     { 
      Comp_Weapon = rand() % 3; 

      switch(Comp_Weapon) // Computer randomly selects a weapon. 
      { 
       case 1: 
        Cannon_Dmg = 10 + rand() % 6; 
        Player_Health = Player_Health - Cannon_Dmg; 
        cout << "Your enemy used a cannon and caused " << Cannon_Dmg << " damage to you.\n"; 
        cout << "Your health is " << Player_Health << endl; 
        cout << "The computer's health is " << Comp_Health << endl; 
        Comp_Cannon_Ammo = Comp_Cannon_Ammo - 1; 
        break; 

       case 2: 
        Grenade_Dmg = 7 + rand() % 6; 
        Player_Health = Player_Health - Grenade_Dmg; 
        cout << "Your enemy used a grenade and caused " << Grenade_Dmg << " damage to you.\n"; 
        cout << "Your health is " << Player_Health << endl; 
        cout << "The computer's health is " << Comp_Health << endl; 
        Comp_Grenade_Ammo = Comp_Grenade_Ammo - 1; 
        break; 

       case 3: 
        Rifle_Dmg = 3 + rand() % 6; 
        Player_Health = Player_Health - Rifle_Dmg; 
        cout << "Your enemy used a rifle and caused " << Rifle_Dmg << " damage to you.\n"; 
        cout << "Your health is " << Player_Health << endl; 
        cout << "The computer's health is " << Comp_Health << endl; 
        break; 




      } 


     } 


     if (Comp_Health < 0) 
      cout << "Congratulations, you beat your enemy!" << endl; 

     if (Player_Health < 0) 
      cout << "You have been defeated by your enemy!" << endl;; 



     cin.ignore(2); 
     return 0; 

    } 

} 
+1

デバッガを試しましたか? –

+0

エラーはありません。それはループの位置と関係がある。 – Zjm4192

+1

しかし、あなたのコードの動作はあなたの期待と異なりますか?このような状況では、デバッガは使用するツールです。 –

答えて

0
whileループの外 return声明を移動

デバッガを使用するとこれをキャッチします。

+0

XCodeでデバッガを使用するにはどうすればよいですか?私はreturn文を出したが、最初にランダムに選択して私の武器を選ぶと、最初にランダムに選んだときに得られるコードと同じコードが返される。 – Zjm4192

1

いくつかの問題のようです。

Turnはどのように初期値から変更されますか?最初に行く人は、他のプレイヤーが健康状態にならなくなるまで行くでしょう。

rand%3は0,1,2です。スイッチは1,2,3です。 0の場合は無視されます。

return文は、whileループ内で、プログラムがcin.ignore(2)ライン

私はこれらの問題を修正し、プログラムは、多かれ少なかれ、作業のように見えるの上にハングアップします。

- Mat

+0

私はreturn文をwhileループから外してプログラムを続行しますが、あなたが正しいと判断して、最初に選択した後、選択した後、最初にランダムに選択したときに同じコードを実行しました。これをどうすれば解決できますか? – Zjm4192

+0

一番上のセクションでTurn = 1、一番下のセクションでTurn = 0を設定しました。これは交代する。最初に行くことについてのコメントはおそらくループの外側に移動するべきです –

+0

プレーヤーが最初に行くかのように、あなたは "Turn == 1"を入れますか?トップセクションを定義するものと、ボトムセクションを定義するものは不明です。D。私は最初に行くことについての出力を削除しました。また、コンピュータが無作為に武器を選択するためには、0,1,2とは対照的に、1、2、または3だけを選択できるようにランダムなステートメントを作成する必要がありますか? – Zjm4192

関連する問題