2015-09-07 254 views
-6

在戰鬥遊戲上練習,現在保持基本狀態,我試圖讓用戶繼續輸入「A」進行攻擊,並且我希望循環讓用戶繼續攻擊,直到怪物的生命值是0或者低於0.問題是,它只會攻擊一次,它會降低生命值並顯示其剩餘的生命值,但不允許用戶再次攻擊。這是我目前堅持的作品。做while循環不正常循環

if (level ==1) 

    { 
     do 
     { 
      cout<<"\n\n"; 
      cout<<"Level "<<level<<" Defeat the guard\n\n"; 
      cout<<"Guard Stats\n"; 
      cout<<"Power: "<<monster.displaystrength()<<endl; 
      cout<<"Defense: "<<monster.displaydefense()<<endl; 
      cout<<"Health: "<<monster.displayhealth()<<"\n\n"<<endl; 

      cout<<"Enter A) Attack\n"; 
      cout<<"Enter D) Defend\n"; 
      cout<<"Enter S) Special\n"; 
      cout<<"Enter N) Do Nothing\n\n"; 
      cin>>battleChoice; 


      if(battleChoice =='A' || battleChoice == 'a') 
      { 
       srand(time(NULL)); 
       dps = rand()%10+1; 
       cout<<"Damage done: " << dps << endl; 
       monsterHealth = monster.displayhealth(); 
       totalMonsterHealth = monsterHealth - dps; 
       cout<<"Monster health remaining: "<<totalMonsterHealth<<"\n\n"<<endl; 

       return totalMonsterHealth; 
      } 

     }while (totalMonsterHealth!=0); 

      if(battleChoice == 'D' || battleChoice == 'd') 
      { 
       cout<<"Defending\n"; //prototype 
      } 

      if(battleChoice == 'S' || battleChoice == 's') 
      { 
       cout<<"Using special attack\n"; //prototype 
      } 
      if(battleChoice == 'N' || battleChoice == 'n') 
      { 
       cout<<"Do nothing, Loop again.\n"; //prototype 
      } 

    } 
+1

'return'從函數返回返回totalMonsterHealth。 – alain

+2

我猜測它的運行直到'return totalMonsterHealth'? ;-)該行終止函數,因此也是循環。 –

回答

3

你在

if(battleChoice =='A' || battleChoice == 'a') 

因此,當用戶點擊Aa將退出循環

+0

有道理。讓我看看還有什麼我可以嘗試。 – Palermox

+0

也許你想把'if-statement'全部放在'do-while循環中,因爲我認爲你需要每次檢查用戶想要做什麼。 – Astinog