2017-10-13 116 views
0

我正在使用項目,商店等在控制檯上進行基於文本的冒險。在C#中使用其他函數中定義的類變量

現在我想通過使用類來縮短購買劍的代碼。我把古劍定義在一個單獨的功能和代碼隨機化,你可以在其他買劍:

namespace Text_Adventure 
{ 
     class Game 
    { 
//Weapon class 

    public class Weapon{ 
      public string name; 
      public int damage; 
      public int magic; 
      public int durability; 
      public int price; 
     } 

//the weapon showed in the shop 
    public class Weaponshop{ 
      public string name; 
      public int damage; 
      public int magic; 
      public int durability; 
      public int price; 
     } 

... 


// defenition of swords 

public void swords() 
    { 
     Weapon sword1 = new Weapon(); 

     sword1.name = "Wooden Sword"; 
     sword1.damage = 2; 
     sword1.magic = 1; 
     sword1.durability = 20; 
     sword1.price = 10; 

     Weapon sword2 = new Weapon(); 

     sword2.name = "Iron Sword"; 
     sword2.damage = 3; 
     sword2.magic = 2; 
     sword2.durability = 50; 
     sword2.price = 20; 
... 
    } 

//gamble sword that are shown in the shop 
public void swordgamble() 
    { 

     Random shopgamble = new Random(); 
     Weaponshop shopsword1 = new Weaponshop(); 
     //gamble shopsword 1 

     int shopgamblenumber = shopgamble.Next(1, 8 + 1); 

現在problemcode

---> if (shopgamblenumber == 1) 
     { 
      shopsword1 = sword1; 
     } 

     if (shopgamblenumber == 2) 
     { 
      shopsword1 = sword1; 
     } 
             <---- 

與shopsword2和shopsword3

發生同樣的情況
public void buysword 
{ 

swordgamble(); 
Console.WriteLine("Shop"); 
     Console.WriteLine(); 
     Console.WriteLine("Which sword do you would like to have?"); 
     Console.WriteLine(); 

     while (correct == 0) 
     { 
      Console.WriteLine("1. " + shopsword1.name); 
      Console.WriteLine("2. " + shopsword2.name); 
      Console.WriteLine("3. " + shopsword3.name); 
      Console.WriteLine("4. None of these"); 
      Console.WriteLine(""); 
... 

} 

我的程序無法讀取我在劍中設置的變量並將其分配到商店word1

我的舊版本只有變量,而且因爲這個相當大的時候我拿了8把劍可以買到。有誰知道如何讀取我在劍中設置的變量並讀取函數buysword()中的商店字詞? ?

+0

你已經初始化了這些變量,並把它們扔在了swords()void中。你必須將它們存儲在某個地方 – 2017-10-13 08:40:42

+0

如何將'Weapon'分配給'Weaponshop'類型的變量? –

+0

--->是爲了標記扇區 對不起,我是新的 – KillSwitch

回答

0

正如某人所指出的,您必須將生成的劍存儲在一個變量中。我會使用IDictionary<int, Weapon>,它可以簡化swordgamble()中的代碼。這本詞典的關鍵是用於訪問它的shopgamblenumber。

public IDictionary<int, Weapon> swords() 
    { 
     var dict = new Dictionary<int, Weapon> swords() { 
     1, new Weapon { 
      name = "Wooden Sword", 
      damage = 2, 
      magic = 1, 
      durability = 20, 
      price = 10 
     }, 
     2, new Weapon { 
      name = "Iron Sword", 
      damage = 3, 
      magic = 2, 
      durability = 50, 
      price = 20 
     } 
    }; 
    return dict; 
} 

然後用它來生成劍。

public void swordgamble() { 

     var swordsDict = swords(); 
     Random shopgamble = new Random(Guid.NewGuid().GetHashCode()); // seed rng 
     Weaponshop shopsword1 = new Weaponshop(); 
     //gamble shopsword 1 

     int shopgamblenumber = shopgamble.Next(1, 8 + 1); 
     var sword = swordsDict[shopgamblenumber]; 
     //... 
} 

此外,Weaponshop應該繼承Weapon所以shopsword1 = sword可以工作。

+0

謝謝你現在的工作 – KillSwitch

+1

爲什麼繼承,而不是簡單地使用一個類?據我所知,'Weaponshop'和'Weapon'之間沒有區別。如果有意義的差異,我會期望'武器'沒有價格。 – Flater

0

使用格奧爾格的答案,您可以節省創建字典的開銷,每次創建字符串時都會創建一個武器工廠方法。

 public Weapon swords(int weaponNumber) 
     { 
      switch (weaponNumber) 
      { 
       case (1); 
        return new Weapon() { 
         name = "Wooden Sword", 
         damage = 2, 
         magic = 1, 
         durability = 20, 
         price = 10 
         }; 
       case (2): 
        return new Weapon() { 
         name = "Iron Sword", 
         damage = 3, 
         magic = 2, 
         durability = 50, 
         price = 20 
        }; 
       default: 
        return null; //Or some other default for when it doesn't exist 
      } 
     } 

然後調用代碼看起來像這樣

public void swordgamble() {  
     Random shopgamble = new Random(Guid.NewGuid().GetHashCode()); // seed rng 
     Weapon shopsword1; 
     //gamble shopsword 1 

     int shopgamblenumber = shopgamble.Next(1, 8 + 1); 
     shopsword1 = swords(shopgamblenumber); 
     //... 
} 

而作爲Flater評論有沒有必要,你可以只使用武器類再次舉行獲取武器的實例WeaponShop類從商店

相關問題