2017-03-09 73 views
0

我在我的遊戲中有這個boss戰,我正在爲一個班級工作,並且它不按預期工作,在老闆或玩家死後打印各種攻擊和死亡消息。C#雖然循環不按預期工作

我很新的代碼,今年剛開始爲一類

在此先感謝!

這裏的boss戰:

public static void redKeeperBattle() 
    { 
      if (boss.redKeeperHealth <= 0 && (player.playerHealth <= 0)) 
      { 
       Console.Write("You have defeated "); 
       Console.ForegroundColor = ConsoleColor.Red; 
       Console.WriteLine("The Red Keeper!"); 
       Console.ResetColor(); 
       Console.ReadLine(); 
       Console.Write("But you have also "); 
       Console.ForegroundColor = ConsoleColor.Red; 
       Console.WriteLine("Died!"); 
       Console.ResetColor(); 
       Console.ReadLine(); 
       playerDeath(); 
      } 

      if (player.playerHealth <= 0) 
      { 

       boss.redKeeperAlive = false; 
       Console.ForegroundColor = ConsoleColor.Red; 
       Console.Write("The Red Keeper "); 
       Console.ResetColor(); 
       Console.WriteLine("has killed {0}, how sad...", player.playerName); 
       Console.ReadLine(); 
       playerDeath(); 
      } 

      if (boss.redKeeperHealth <= 0) 
      { 
       boss.redKeeperAlive = false; 
       Console.Write("You have defeated "); 
       Console.ForegroundColor = ConsoleColor.Red; 
       Console.WriteLine("The Red Keeper!"); 
       Console.ResetColor(); 
       Console.ReadLine(); 
       redKeeperRoom(); 
      } 


      while (boss.redKeeperAlive == true) 
      { 


       //Player damage 
       Random pDmg = new Random(); 
       int playerDmg = pDmg.Next(5, 16); 

       //Player dodge 
       Random pDodge = new Random(); 
       int playerDodge = pDodge.Next(1, 11); 

       //Player crit 
       Random pCrit = new Random(); 
       int playerCrit = pCrit.Next(10, 16); 

       //Red keeper damage 
       Random rDmg = new Random(); 
       int redDmg = rDmg.Next(1, 21); 

       Console.Clear(); 
       Console.ForegroundColor = ConsoleColor.Red; 
       Console.WriteLine("--------------------------------------------------------------------------------"); 
       Console.WriteLine(""); 
       Console.WriteLine("THE RED KEEPER: {0} HP", boss.redKeeperHealth); 
       Console.ResetColor(); 
       Console.WriteLine(""); 
       Console.Write("{0}: ", player.playerName); 
       if (player.playerHealth > 50) 
       { 
        Console.ForegroundColor = ConsoleColor.Green; 
       } 
       else if (player.playerHealth > 20) 
       { 
        Console.ForegroundColor = ConsoleColor.Yellow; 
       } 
       else { 
        Console.ForegroundColor = ConsoleColor.Red; 
       } 
       Console.WriteLine("{0}", player.playerHealth); 
       Console.WriteLine(""); 
       Console.WriteLine(""); 
       Console.ForegroundColor = ConsoleColor.Magenta; 
       Console.WriteLine("What would you like to do?"); 
       Console.WriteLine("1) Attack"); 
       Console.WriteLine("2) Dodge"); 
       Console.WriteLine(""); 
       Console.ResetColor(); 

       string attackChoice = Console.ReadLine(); 


       if (attackChoice == "1" && (playerCrit == 15)) 
       { 
        playerDmg += playerCrit; 
        Console.WriteLine(""); 
        Console.Write("You critically struck "); 
        Console.ForegroundColor = ConsoleColor.Red; 
        Console.WriteLine("The Red Keeper "); 
        Console.ResetColor(); 
        Console.WriteLine("for {0} HP!", playerDmg); 
        Console.ForegroundColor = ConsoleColor.Gray; 
        Console.WriteLine("Press Enter to end your turn..."); 
        boss.redKeeperHealth -= playerDmg; 
        Console.ReadLine(); 
        Console.ForegroundColor = ConsoleColor.Red; 
        Console.Write("The Red Keeper "); 
        Console.ResetColor(); 
        Console.WriteLine("hit you for {0}", redDmg); 
        Console.ForegroundColor = ConsoleColor.Gray; 
        Console.WriteLine("Press Enter to end his turn..."); 
        Console.ResetColor(); 
        player.playerHealth -= redDmg; 
        Console.ReadLine(); 
        redKeeperBattle(); 
       } 

       if (attackChoice == "1") 
       { 
        Console.WriteLine(""); 
        Console.WriteLine("You hit The Red Keeper for {0}", playerDmg); 
        Console.ForegroundColor = ConsoleColor.Gray; 
        Console.WriteLine("Press Enter to end your turn..."); 
        boss.redKeeperHealth -= playerDmg; 
        Console.ReadLine(); 
        Console.ForegroundColor = ConsoleColor.Red; 
        Console.Write("The Red Keeper "); 
        Console.ResetColor(); 
        Console.WriteLine("hit you for {0}", redDmg); 
        Console.ForegroundColor = ConsoleColor.Gray; 
        Console.WriteLine("Press Enter to end his turn..."); 
        Console.ResetColor(); 
        player.playerHealth -= redDmg; 
        Console.ReadLine(); 
        redKeeperBattle(); 
       } 

       if (attackChoice == "2" && (playerDodge > 5)) 
       { 
        Console.WriteLine(""); 
        Console.ForegroundColor = ConsoleColor.DarkGray; 
        Console.WriteLine("You attempt to dodge!"); 
        Console.ResetColor(); 
        Console.ReadLine(); 
        Console.ForegroundColor = ConsoleColor.Green; 
        Console.WriteLine("You successfully dodged and gained 10 HP!"); 
        Console.ReadLine(); 
        player.playerHealth += 10; 
        Console.ForegroundColor = ConsoleColor.Red; 
        Console.Write("The Red Keeper "); 
        Console.ResetColor(); 
        Console.WriteLine("would have hit you for {0} HP!", redDmg); 
        Console.ReadLine(); 
        redKeeperBattle(); 
       } 

       if (attackChoice == "2") 
       { 
        Console.WriteLine(""); 
        Console.ForegroundColor = ConsoleColor.DarkGray; 
        Console.WriteLine("You attempt to dodge!"); 
        Console.ResetColor(); 
        Console.ReadLine(); 
        Console.ForegroundColor = ConsoleColor.Red; 
        Console.WriteLine("Dodge failed!"); 
        Console.ResetColor(); 
        Console.ReadLine(); 
        Console.ForegroundColor = ConsoleColor.Red; 
        Console.Write("The Red Keeper "); 
        Console.ResetColor(); 
        Console.WriteLine("hit you for {0}", redDmg); 
        Console.ForegroundColor = ConsoleColor.Gray; 
        Console.WriteLine("Press Enter to end his turn..."); 
        Console.ResetColor(); 
        player.playerHealth -= redDmg; 
        Console.ReadLine(); 
        redKeeperBattle(); 

       } 
       else { 

        Console.WriteLine(""); 
        Console.ForegroundColor = ConsoleColor.Red; 
        Console.WriteLine("An error occurred! Please try again!"); 
        Console.ForegroundColor = ConsoleColor.Gray; 
        Console.WriteLine("Press Enter to continue..."); 
        Console.ResetColor(); 
        Console.ReadLine(); 
        redKeeperBattle(); 
       } 

      } 

    } 
    public static void redKeeperRoom() 
    { 
     //Continue game 
     Console.WriteLine ("The boss has died!"); 
     Console.ReadLine(); 
    } 


    public static void playerDeath() 
    { 
     //End game 
     Console.WriteLine ("You have Died!"); 
     Console.ReadLine(); 
    } 

所以會發生什麼是當人死後,它只是像修建垃圾郵件:

你打紅守護者12 按Enter鍵結束輪到你了.. 。

紅色守護打你14 按Enter鍵結束輪到他...

你已經打敗了紅色的守護者!

老闆死了!

發生錯誤!請再試一次! 按Enter繼續...

您已擊敗The Red Keeper!

老闆死了!

你打紅守護者29 按Enter鍵結束輪到你...

紅色守護打你19 按Enter鍵結束輪到他...

你已經打敗了的紅色的守護者!

但你也死了!

你已經死了!

+1

你有沒有考慮時會發生什麼'redKeeperBattle()'因爲你是遞歸(這你可能不希望做調用它的回報... )。即'redKeeperBattle()'調用調用'redKeeperBattle()'的redKeeperBattle()'。現在,當第三個人回來時,控制權交還給第二個人。第二個電話是否正確?您可以使用靜態int來計算呼叫號碼並打印一些調試信息,以告訴您哪個呼叫正在輸出意外數據。 – John3136

+0

:/代碼牆 –

+0

對不起@ J.SMTBCJ15我真的不知道我在做什麼。 :/ – Josh

回答

0

您使用遞歸循環和while循環,它太複雜了。這是很難理解你到底是什麼問題。你必須選擇使用什麼

1)循環

function P(){ 
    while(boss.redKeeperAlive == true){ 

     if(condition) 
      boss.redKeeperAlive = false; 
    } 
} 

2)遞歸

function P(){ 
     if(boss.redKeeperAlive == true){ 
      P() 
     } 
} 

但不能同時在同一時間。

+0

嘿,非常感謝你的工作!我只是拿出了一些時間,並且使用了一些if和else,如果是,我就完成了! – Josh

+0

喬希,我很高興它幫助。不用謝。 –

0

這裏是代碼的修改版本。由於它是基於你的代碼,它仍然需要更多的工作,但我注意到的一些事情是:

  1. 沒有使用函數。
  2. 沒有正確使用「if」語句。
  3. 沒有正確使用遞歸和循環。

我建議讀一讀這些。

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 

namespace ConsoleApplication1 
{ 
public class Boss 
{ 
    public int redKeeperHealth = 100; 
} 

public class Player 
{ 
    public int playerHealth = 100; 
    public string playerName = "Ace"; 
} 

class Program 
{ 
    public static Boss boss = new Boss(); 
    public static Player player = new Player(); 

    static void Main(string[] args) 
    { 
     redKeeperBattle(); 
    } 

    public static void redKeeperBattle() 
    { 
     //Player damage 
     Random pDmg = new Random(); 
     int playerDmg; 

     //Player dodge 
     Random pDodge = new Random(); 
     int playerDodge; 

     //Player crit 
     Random pCrit = new Random(); 
     int playerCrit; 

     //Red keeper damage 
     Random rDmg = new Random(); 
     int redDmg; 

     while (boss.redKeeperHealth > 0 && player.playerHealth > 0) 
     { 
      PrintToScreen(); 
      int playerChoice = ReadPlayerChoice(); 

      if (playerChoice == 1) 
      { 
       playerDmg = pDmg.Next(5, 16); 
       playerCrit = pCrit.Next(10, 16); 
       if (playerCrit == 15) 
       {       
        playerDmg += playerCrit; 
        Console.WriteLine(""); 
        Console.Write("You critically struck "); 
       } 

       Console.WriteLine(""); 
       Console.WriteLine("You hit The Red Keeper for {0}", playerDmg); 
       boss.redKeeperHealth -= playerDmg; 

       redDmg = rDmg.Next(1, 21); 
       Console.ForegroundColor = ConsoleColor.Red; 
       Console.Write("The Red Keeper "); 
       Console.ResetColor(); 
       Console.WriteLine("hit you for {0}", redDmg); 
       Console.ForegroundColor = ConsoleColor.Gray; 
       redDmg = rDmg.Next(1, 21); 
       player.playerHealth -= redDmg; 

       Console.WriteLine("Press Enter to continue"); 
       Console.ReadLine(); 
       Console.ResetColor();      
      } 

      else if (playerChoice == 2) 
      { 
       Console.WriteLine(""); 
       Console.ForegroundColor = ConsoleColor.DarkGray; 
       Console.WriteLine("You attempt to dodge!"); 
       Console.ResetColor(); 
       Console.ReadLine(); 

       playerDodge = pDodge.Next(1, 11); 
       if (playerDodge > 5) 
       { 
        redDmg = rDmg.Next(1, 21); 
        Console.ForegroundColor = ConsoleColor.Green; 
        Console.WriteLine("You successfully dodged and gained 10 HP!"); 
        Console.ReadLine(); 
        player.playerHealth += 10; 
        Console.ForegroundColor = ConsoleColor.Red; 
        Console.Write("The Red Keeper "); 
        Console.ResetColor(); 
        Console.WriteLine("would have hit you for {0} HP!", redDmg); 
        Console.ReadLine(); 
       } 
       else 
       { 
        redDmg = rDmg.Next(1, 21); 
        Console.ForegroundColor = ConsoleColor.Red; 
        Console.WriteLine("Dodge failed!"); 
        Console.ResetColor(); 
        Console.ReadLine(); 
        Console.ForegroundColor = ConsoleColor.Red; 
        Console.Write("The Red Keeper "); 
        Console.ResetColor(); 
        Console.WriteLine("hit you for {0}", redDmg); 
        Console.ForegroundColor = ConsoleColor.Gray; 
        Console.WriteLine("Press Enter to end his turn..."); 
        Console.ResetColor(); 
        player.playerHealth -= redDmg; 
        Console.ReadLine(); 
       } 

      }     
      else 
      { 

       Console.WriteLine(""); 
       Console.ForegroundColor = ConsoleColor.Red; 
       Console.WriteLine("An error occurred! Please try again!"); 
       Console.ForegroundColor = ConsoleColor.Gray; 
       Console.WriteLine("Press Enter to continue..."); 
       Console.ResetColor(); 
       Console.ReadLine(); 
      } 


      if (boss.redKeeperHealth <= 0 && player.playerHealth <= 0) 
      { 
       Console.Write("You have defeated "); 
       Console.ForegroundColor = ConsoleColor.Red; 
       Console.WriteLine("The Red Keeper!"); 
       Console.ResetColor(); 
       Console.ReadLine(); 
       Console.Write("But you have also "); 
       Console.ForegroundColor = ConsoleColor.Red; 
       Console.WriteLine("Died!"); 
       Console.ResetColor(); 
       Console.ReadLine(); 
       playerDeath(); 
      } 

      if (player.playerHealth <= 0 && boss.redKeeperHealth > 0) 
      { 

       Console.ForegroundColor = ConsoleColor.Red; 
       Console.Write("The Red Keeper "); 
       Console.ResetColor(); 
       Console.WriteLine("has killed {0}, how sad...", player.playerName); 
       Console.ReadLine(); 
       playerDeath(); 
      } 

      if (boss.redKeeperHealth <= 0 && player.playerHealth > 0) 
      { 
       Console.Write("You have defeated "); 
       Console.ForegroundColor = ConsoleColor.Red; 
       Console.WriteLine("The Red Keeper!"); 
       Console.ResetColor(); 
       Console.ReadLine(); 
       redKeeperRoom(); 
      } 

     } 

    } 
    public static void redKeeperRoom() 
    { 
     //Continue game 
     Console.WriteLine("The boss has died!"); 
     Console.ReadLine(); 
    } 


    public static void playerDeath() 
    { 
     //End game 
     Console.WriteLine("You have Died!"); 
     Console.ReadLine(); 
    } 

    private static void PrintToScreen() 
    { 
     Console.Clear(); 
     Console.ForegroundColor = ConsoleColor.Red; 
     Console.WriteLine("--------------------------------------------------------------------------------"); 
     Console.WriteLine(""); 
     Console.WriteLine("THE RED KEEPER: {0} HP", boss.redKeeperHealth); 
     Console.ResetColor(); 
     Console.WriteLine(""); 
     Console.Write("{0}: ", player.playerName); 
     if (player.playerHealth > 50) 
     { 
      Console.ForegroundColor = ConsoleColor.Green; 
     } 
     else if (player.playerHealth > 20) 
     { 
      Console.ForegroundColor = ConsoleColor.Yellow; 
     } 
     else 
     { 
      Console.ForegroundColor = ConsoleColor.Red; 
     } 
     Console.WriteLine("{0}", player.playerHealth); 
     Console.WriteLine(""); 
     Console.WriteLine(""); 
     Console.ForegroundColor = ConsoleColor.Magenta; 
     Console.WriteLine("What would you like to do?"); 
     Console.WriteLine("1) Attack"); 
     Console.WriteLine("2) Dodge"); 
     Console.WriteLine(""); 
     Console.ResetColor(); 
    } 

    private static int ReadPlayerChoice() 
    { 
     return int.Parse(Console.ReadLine()); 
    } 
} 

}

-Enjoy