我正在使用物品,商店等在控制台中进行基于文本的冒险。
现在我想缩短使用课程购买剑的代码。我把剑的定义放在一个单独的函数和代码中随机化你可以购买的剑:
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);
现在问题代码
---> 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("");
...
}
我的程序无法读取我在剑中设置的变量并将其分配给shopsword1
我的旧版本只有变量,因为当我拿了8把剑你可以购买时这个非常大。有谁知道如何读取我在剑中设置的变量并阅读函数buysword()中的storesword; ?
答案 0 :(得分:0)
如某人所述,您必须将生成的剑存储在变量中。我会使用SharedPreferences sp=getSharedPreferences("Button", Context.MODE_PRIVATE);
SharedPreferences.Editor Ed=sp.edit();
// get status of button to set backround from SharedPreferences in oncrate() methosd
if(sp.getBoolean("button1",false)){
b1.setBackgroundColor(Color.WHITE);
}else {
b1.setBackgroundColor(Color.GREEN);
}
if(sp.getBoolean("button2",false)){
b2.setBackgroundColor(Color.WHITE);
}else {
b2.setBackgroundColor(Color.GREEN);
}
// set button background status in SharedPreferences
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
b1.setBackgroundColor(Color.GREEN);
b2.setBackgroundColor(Color.WHITE);
Ed.putBoolean("button1", true);
Ed.commit();
}
}
});
b2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
b1.setBackgroundColor(Color.WHITE);
b2.setBackgroundColor(Color.RED);
Ed.putBoolean("button2", true);
Ed.commit();
}
}
});
,这可以简化IDictionary<int, Weapon>
中的代码。这本词典的关键是用于访问它的shopgamblenumber。
swordgamble()
然后用它来生成剑。
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
可以正常工作。
答案 1 :(得分:0)
使用Georg的答案,每次通过创建武器工厂方法调用剑时,可以节省创建字典的开销。
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类,你可以再次使用Weapon类来保存从商店中检索到的武器实例