2015-09-20 32 views
0

我正在創建地下城遊戲。我希望玩家有機會發生變異,並隨着時間的推移造成健康損失。我的問題是,當序列被循環並且戰鬥階段結束時,突變再次被隨機化。意思是說,如果我發生突變並受到傷害並且敵人還活着,那麼它會重新執行「while」聲明並再次爲突變隨機化一個值。在循環中創建隨機值保持該值

if(input.equals("1")) { 
    int damageDealt = rand.nextInt(attackDamage); 
    int damageTaken = rand.nextInt(enemyAttackDamage); 
    int taken = rand.nextInt(mutation); //variable to see if a mutation occurs. Integer for mutation is 100. 
    if (taken < 10) { 
// How do I tell the program to keep the number for taken? 
// it will just re-loop and find a new rand.nextInt(mutation); 
     enemyHealth -= damageDealt; //damage taken for enemy 
     health -= damageTaken; //damage taken for player 
     health -= rand.nextInt(maxMutationDamage); //damage taken from mutation. maxMutationDamage is 15 
     JOptionPane.showMessageDialog(null, "You hit the " + enemy + " for " + damageDealt + " damage." + "\nYou receieved " + damageTaken + " damage." + "\nyour mutation did " + taken + " damage."); 
    } 
    else { 
     // I added this so that if the mutation didn't occur 
     // the calculations for the fight still renders. 
     enemyHealth -= damageDealt; 
     health -= damageTaken; 
     JOptionPane.showMessageDialog(null, "You hit the " + enemy + " for " + damageDealt + " damage." + "\nYou receieved " + damageTaken + " damage."); 
    } 
    if(health < 1) { 
     JOptionPane.showMessageDialog(null, "you have taken to much damage!"); 
     break; 
    } 
} 
else if (input.equals("2")) { 
    if(numBandages > 0) { 
     health += bandagesHealAmount; 
     numBandages --; 
     JOptionPane.showMessageDialog(null, "You bandaged yourself for " + bandagesHealAmount + "." 
       +"\nYou now have " + health + " HP." 
       +"\nYou have " + numBandages + " bandages left."); 
    } 
    else { 
     JOptionPane.showMessageDialog(null, "You have no bandages in inventory to heal!"); 
    } 
} 
else if (input.equals("3")) { 
    JOptionPane.showMessageDialog(null, "You ran away from the " + enemy + "!"); 
    continue GAME; //it resets the sequence of the dungeon to a new fight 
} 

還,如果你們能幫助,我怎樣才能使它即使玩家跑掉,並且不癒合,他仍然得到的通過突變受傷?

只有在癒合完成後,突變纔會停止。

+0

那麼你什麼時候需要指定一個新的隨機值?顯然不是每次執行輸入「1」的動作 - 但是何時? – laune

+0

while循環在哪裏? – chenchuk

+0

使用[編輯]進行編輯。 –

回答

0

一些代碼顯示如何設置和保持狀態變量。不知道什麼時候回到初始狀態。

int taken = -1; 
while(...){ // continue game 
    switch(input){ 
    case "1": 
     if(taken == -1) taken = rand.nextInt(mutation); 
     if(taken < 10){ 
      ... 
     } 
     ... 
     break; 
    case "2": 
     ... 
     // Does healing happen here? 
     taken = -1; // return to initial state 
     break; 
    case "3": 
     ... 
     break; 
    } 
} 
+0

我會把這個放在哪裏?對於案例和切換的概念,我還是個新手。 – AhhDoom

+0

該交換機旨在替代您的級聯。你必須填補空白(...),主要是從那裏複製代碼。 – laune