2016-11-22 2 views
-3

Wenn ich also das Ende des Programms erreiche, wo es ein Pokémon aus dem Nichts erscheint, wiederholt es immer wieder dasselbe Pokémon. z.B. Ich begegne Mr.Mime ich Mr.Mime besiegen ich sicher bin Ich bin sicher ich Mr.Mime begegne ich sterben begegne ich Mr.Mime ich sterbe Ich bin sicher Ich begegne Mr.Mime ich Niederlage Mr.MimeC# Konsolenanwendung -

Wie würde ich das stoppen?

Mein Code:

using System; 
using System.Linq; 
using System.Threading; 

public class Program 
{ 
    public static void Main() 
    { 
     // Due to dotnetfiddle.net limitations I cannot store the pokémons data to a file 
     Console.WriteLine("---------------------------------------------------------------------------------------------------------------------"); 
     Console.WriteLine("|   Hello there! Welcome to the world of pokémon! My name is Oak! People call me the pokémon Prof!   |");     
     Console.WriteLine("|This world is inhabited by creatures called pokémon! For some people, pokémon are pets. Others use them for fights.|"); 
     Console.WriteLine("|         Myself...I study pokémon as a profession.          |"); 
     Console.WriteLine("---------------------------------------------------------------------------------------------------------------------"); 
     Console.WriteLine("     |       Would you like to start (S)?      |"); 
     Console.WriteLine("     |                    |"); 
     Console.WriteLine("     -------------------------------------------------------------------------------"); 
     String fog = Console.ReadLine(); 
     String [] random = {"Pidgeot", "Jigglypuff", "Beedrill", "Caterpie", "Squirtle", "Charizard", "Charmander", "Bulbasaur", "Rattata", "Diglett", "Meowth", "Psyduck", "Dugtrio", "Magnemite", "Mr. Mime", "Gyarados", "Magikarp", "Onix", "Drowzee"}; 
     String [] gen = {"♂", "♀"}; 
     String [] name = {"Charmander", "Bulbasaur", "Squirtle"}; 
     if (fog == "s" || fog == "S" || fog == "start" || fog == "Start") 

     { 
      Random rnd = new Random(); 
      int HP = rnd.Next(20, 20); 
      int Atk = rnd.Next(20, 20); 
      int Def = rnd.Next(20, 20); 
      int Lvl = rnd.Next(5, 5); 
      int PN = rnd.Next(1, 721); 
      Console.WriteLine("You have chosen to generate a pokémon's!"); 
      Console.WriteLine(" Your pokémon's Name is: " + name[new Random().Next(0, name.Length)]); 
      Console.WriteLine(" Your pokémon's ㏋ is: " + HP); 
      Console.WriteLine(" Your pokémon's Attack is: " + Atk); 
      Console.WriteLine(" Your pokémon's Defense is: " + Def); 
      Console.WriteLine(" Your pokémon's Lvl is: " + Lvl); 
      Console.WriteLine(" Your pokémon's Gender is: " + gen[new Random().Next(0, gen.Length)]); 
      Console.WriteLine(" Your pokémon's Pokédex number is: " + PN); 

      Console.Write("Loading"); 
      for(int i = 0; i < 10; i++) 
      { 

      Console.Write("."); 
       Thread.Sleep(200); 
      } 
     Console.WriteLine(" "); 
     Console.WriteLine("---------------------------------------------------------------------------------------------------------------------"); 
     Console.WriteLine("|      You exit the proffesors lab to journey into the world of pokemon       |");     
     Console.WriteLine("|    Throughout your journey you may encounter wild pokemon that wish to fight and trainers    |"); 
     Console.WriteLine("|                             |"); 
     Console.WriteLine("---------------------------------------------------------------------------------------------------------------------"); 
     Console.WriteLine("So your journey begins... 30 paces to the gym type (s)"); // Loop 30 times with random chance of battleing 
      string lf = Console.ReadLine(); 
      if (lf == "s" || lf == "S" || lf == "start" || lf == "Start") 

      Enumerable.Repeat<Action>(() => 
      { 
       int Find = rnd.Next(1, 3); 
       if (Find == 1) 
       { 
       Console.WriteLine("You Encountered a pokémon"); 
      int HP2 = rnd.Next(1, 100); 
      int Atk2 = rnd.Next(1, 60); 
      int Def2 = rnd.Next(1, 40); 
      int Lvl2 = rnd.Next(1, 100); 
      int PN2 = rnd.Next(1, 721); 

      if (Atk >= Def2) 
      { 
       Console.WriteLine("You defeated " + random[new Random().Next(0, random.Length)]); 
      } 
      else 
      { 
      Console.WriteLine("Your pokémon died... Luckily a stranger appeared out of nowhere and revivded it for you so you can continue to battle"); 
      } 
       } 
       else 
       {  
      Console.WriteLine("You are safe this time"); 
       } 
      }, 30).ToList().ForEach(x => x()); 

     } 


     else 
     { 
     Console.WriteLine("Sorry to see you go so soon. I hope to meey you one day ~Oak"); 
     } 
    } 
} 

Antwort

0

Das Problem liegt auf dieser Linie.

Console.WriteLine("You defeated " + random[new Random().Next(0, random.Length)]); 

Sie erhalten die gleiche jedes Mal Zufallswert, da der Random() Konstruktor auf der Systemzeit basiert, und es ist nicht genug, um zwischen Anrufen wechseln.

Siehe Random Class Dokumentation unter

zitierte

... Da jedoch die Uhr endliche Auflösung, mit dem parameterlosen Konstruktor auf verschiedene zufällige Objekte in der Nähe Abfolge erzeugt Zufallszahlen-Generatoren zu erzeugen, die identisch produzieren Sequenzen von Zufallszahlen. Das folgende Beispiel zeigt, wie zwei Zufallsobjekte, die in unmittelbarer Folge instanziiert werden, eine identische Reihe von Zufallszahlen generieren. Auf den meisten Windows-Systemen haben zufällige Objekte, die innerhalb von 15 Millisekunden zueinander erstellt wurden, wahrscheinlich identische Startwerte.

dies die folgenden

Console.WriteLine("You defeated " + random[rnd.Next(0, random.Length)]); 
+0

Danke –

+0

@Arron Davies Kein Problem für die Hilfe ComradeJoecool zu beheben versuchen! – ComradeJoecool

Verwandte Themen