2017-10-13 192 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类从商店

相关问题