2014-09-24 26 views
0

我和我的一個朋友正在做一個小小的有趣的遊戲,在命令提示符下打開。我們無法獲得每回合的隨機傷害。每回合傷害不會改變。我們在每次攻擊時都希望擁有它,傷害是不同的,因爲此時爲傷害產生的第一個數字保持不變,直到遊戲結束。如果還有其他東西可以更好地隨意添加您的選項!感謝您花時間閱讀本文。初學者C#。在控制檯中造成隨機傷害

class Program 
{ 
    static void Main(string[] args) 
    { 
     Random rand = new Random(); 

     int playerHP = 30; 
     int playerDMG = rand.Next(1, 8); 
     string yourName; 

     int monsterHP = 30; 
     int monsterDMG = rand.Next(1, 10); 

     string[] monsterName = { "John Crawford", "Gaurav the Chicken" , "Satan" , "Temmo"}; 
     string[] phrase = { " ' Let the blood drip from your body!'" , " Evil comes from the soul!" , " I will eat your soul!"}; 
     int phrasewords = rand.Next(0, phrase.Length); 
     int mNames = rand.Next(0, monsterName.Length); 

     //Story 

     Console.WriteLine("Name your Hero: "); 
     yourName = Console.ReadLine(); 


     Console.ForegroundColor = ConsoleColor.Green; 
     Console.WriteLine("As you walk through the forest... " + monsterName[mNames] + " appears from the shadows \nand says," + phrase[phrasewords] + "\n"); 
     Console.WriteLine("Do you wish to <attack> " + monsterName[mNames] + ", <defend> yourself, or <run>?\n"); 
     Console.ResetColor(); 

     while (playerHP > 0 && monsterHP > 0) 
     { 
      bool defend = false; 
      string input = Console.ReadLine(); 

      //Inputs 

      switch (input) 
      { 
       case "attack": 
        Console.ForegroundColor = ConsoleColor.Cyan; 
        Console.WriteLine(yourName + " attacks " + monsterName[mNames] + " with his fist, which deals " + playerDMG + " damage\n"); 
        monsterHP -= playerDMG; 
        System.Threading.Thread.Sleep(500); 
        Console.ResetColor(); 
        break; 

       case "run": 
        Console.ForegroundColor = ConsoleColor.Cyan; 
        Console.WriteLine(monsterName[mNames] + " says, 'LOL NOPE' and you must remain on the battlefield.\n"); 
        Console.ResetColor(); 
        break; 

       case "defend": 
        defend = true; 
        Console.ForegroundColor = ConsoleColor.DarkBlue; 
        Console.WriteLine(yourName + " blocks " + monsterName[mNames] + "'s attack with his arms\n"); 
        Console.ResetColor(); 
        break; 

      } 

      //Damage & Health 

      if (!defend) 
      { 
       Console.WriteLine(monsterName[mNames] + " attacks " + yourName + " for " + monsterDMG + " damage because " + yourName + " doesn't know how to \ndefend himself.\n"); 
       playerHP -= monsterDMG; 
       System.Threading.Thread.Sleep(500); 
      } 
      else 
      { 
       Console.WriteLine(monsterName[mNames] + " attacks " + yourName + " for " + (monsterDMG/2) + " damage because " + yourName + " doesn't know how to \nfight like a man.\n"); 
       playerHP -= monsterDMG/2; 
       System.Threading.Thread.Sleep(500); 
      } 

      Console.WriteLine(yourName + " has " + playerHP + " health.\n"); 
      Console.WriteLine(monsterName[mNames] + " has " + monsterHP + " health.\n"); 

     } 

     //Win, Lose, Tie 

     if (playerHP <= 0) 
     { 
      Console.BackgroundColor = ConsoleColor.Red; 
      Console.WriteLine(yourName + " died! GG NO RE\n"); 
      Console.ResetColor(); 
     } 
     if (monsterHP <= 0 && (playerHP > 0)) 
     { 
      Console.ForegroundColor = ConsoleColor.Green; 
      Console.WriteLine("Wow, you win. Congratulations.\n"); 
      Console.ResetColor(); 

     } 
     else 
     { 
      Console.ForegroundColor = ConsoleColor.DarkRed; 
      Console.WriteLine("It was a tie!"); 
      Console.ResetColor(); 
     } 
     Console.ReadLine(); 
    } 
} 

回答

0

您的MonsterDMG和playeyDMG只計算一次。 只要將它移動到你的循環:)

while (playerHP > 0 && monsterHP > 0) 
    { 
     bool defend = false; 
     string input = Console.ReadLine(); 
     monsterDMG = rand.Next(1, 10); //for example here 
     playerDMG = rand.Next(1, 8); // :) 

PS。提示未來的:如果你要生成時間使用短量更隨機量這個隨機數生成器:)

Random random = new Random(Guid.NewGuid().GetHashCode()); 
+0

非常感謝您的幫助!它非常完美! – AlphaTechnical 2014-09-26 18:43:48

+0

如果有幫助,請將此答案標記爲已接受。 StackOverflow unanswered-answers-counter會幫你; )我也是,先生:P – Toumash 2014-11-24 21:51:33

0

你需要計算新MonsterDMG和PlayerDMG在while循環

結束
int playerHP = 30; 
     int playerDMG = rand.Next(1, 8); 
     string yourName; 

     int monsterHP = 30; 
     int monsterDMG = rand.Next(1, 10); 

     string[] monsterName = { "John Crawford", "Gaurav the Chicken" , "Satan" , "Temmo"}; 
     string[] phrase = { " ' Let the blood drip from your body!'" , " Evil comes from the soul!" , " I will eat your soul!"}; 
     int phrasewords = rand.Next(0, phrase.Length); 
     int mNames = rand.Next(0, monsterName.Length); 

     //Story 

     Console.WriteLine("Name your Hero: "); 
     yourName = Console.ReadLine(); 


     Console.ForegroundColor = ConsoleColor.Green; 
     Console.WriteLine("As you walk through the forest... " + monsterName[mNames] + " appears from the shadows \nand says," + phrase[phrasewords] + "\n"); 
     Console.WriteLine("Do you wish to <attack> " + monsterName[mNames] + ", <defend> yourself, or <run>?\n"); 
     Console.ResetColor(); 

     while (playerHP > 0 && monsterHP > 0) 
     { 
      bool defend = false; 
      string input = Console.ReadLine(); 

      //Inputs 

      switch (input) 
      { 
       case "attack": 
        Console.ForegroundColor = ConsoleColor.Cyan; 
        Console.WriteLine(yourName + " attacks " + monsterName[mNames] + " with his fist, which deals " + playerDMG + " damage\n"); 
        monsterHP -= playerDMG; 
        System.Threading.Thread.Sleep(500); 
        Console.ResetColor(); 
        break; 

       case "run": 
        Console.ForegroundColor = ConsoleColor.Cyan; 
        Console.WriteLine(monsterName[mNames] + " says, 'LOL NOPE' and you must remain on the battlefield.\n"); 
        Console.ResetColor(); 
        break; 

       case "defend": 
        defend = true; 
        Console.ForegroundColor = ConsoleColor.DarkBlue; 
        Console.WriteLine(yourName + " blocks " + monsterName[mNames] + "'s attack with his arms\n"); 
        Console.ResetColor(); 
        break; 

      } 

      //Damage & Health 

      if (!defend) 
      { 
       Console.WriteLine(monsterName[mNames] + " attacks " + yourName + " for " + monsterDMG + " damage because " + yourName + " doesn't know how to \ndefend himself.\n"); 
       playerHP -= monsterDMG; 
       System.Threading.Thread.Sleep(500); 
      } 
      else 
      { 
       Console.WriteLine(monsterName[mNames] + " attacks " + yourName + " for " + (monsterDMG/2) + " damage because " + yourName + " doesn't know how to \nfight like a man.\n"); 
       playerHP -= monsterDMG/2; 
       System.Threading.Thread.Sleep(500); 
      } 

      Console.WriteLine(yourName + " has " + playerHP + " health.\n"); 
      Console.WriteLine(monsterName[mNames] + " has " + monsterHP + " health.\n"); 

      **playerDMG = rand.Next(1, 8); 
      monsterDMG = rand.Next(1, 10);** 
     } 

     //Win, Lose, Tie 

     if (playerHP <= 0) 
     { 
      Console.BackgroundColor = ConsoleColor.Red; 
      Console.WriteLine(yourName + " died! GG NO RE\n"); 
      Console.ResetColor(); 
     } 
     if (monsterHP <= 0 && (playerHP > 0)) 
     { 
      Console.ForegroundColor = ConsoleColor.Green; 
      Console.WriteLine("Wow, you win. Congratulations.\n"); 
      Console.ResetColor(); 

     } 
     else 
     { 
      Console.ForegroundColor = ConsoleColor.DarkRed; 
      Console.WriteLine("It was a tie!"); 
      Console.ResetColor(); 
     } 
     Console.ReadLine(); 

    } 
}