Loot Generator,相同的变量

时间:2015-10-08 06:52:26

标签: c# var repeat

所以我做了一个随机的战利品发生器,HP伤害和法力值不同但是由于某种原因它只在两种物品类型之间循环 罕见的梅斯,罕见的梅斯,罕见的靴子,罕见的靴子,传奇的梅斯。 即使我告诉它生成20个项目,它只有2种类型。

这是发电机

namespace TestingRNG
{
public class Items
{

    public int ID { get; set; }
    public int Damage { get; set; }
    public int Defense { get; set; }
    public string Name { get; set; }
    public int HP { get; set; }
    public int MANA { get; set; }
    public string Rarity { get; set; }
    public int NumberOfNames { get; set; }
    public string ItemType { get; set; }
    public Random Random = new Random();
    public List<string> ItemTypeList = new List<string> {"Weapon", "Armor" };
    public List<string> WeaponList = new List<string> { "Staff", "Sword", "Dagger", "Wand", "Axe", "Mace" };
    public List<string> ArmorList = new List<string> { "Chest area armor", "Shield", "Head area armor", "Leggings", "Shoes", "Gloves", "Belt", "Ring" };
    public string WeaponType { get; set; }
    public string Value { get; set; }
    public List<string> DamageType = new List<string> { "Meele", "Magic" };
    public List<string> WeaponListMeele = new List<string> { "Staff", "Wand" };
    public List<string> WeaponListMagic = new List<string> { "Sword", "Dagger", "Axe", "Mace" };
    public List<string> ValuePoor = new List<string> { "Wooden", "Bronze" };
    public List<string> ValueCommon = new List<string> { "Bronze", "Iron" };
    public List<string> Value2 = new List<string> { "Silver", "Steel", "Iron" };
    public List<string> ValueElite = new List<string> { "Silver", "Steel", "Dragon Glass", "Adamantium", "Titanium", "Arcane" };
    public List<string> ValueLegendary = new List<string> { "Dragon Glass", "Adamantium", "Titanium", "Arcane" };
    public List<string> ArmorValuePoor = new List<string> { "Cloth", "Leather" };
    public List<string> ArmorValueCommon = new List<string> { "Cloth", "Leather", "Iron" };
    public List<string> ArmorValue2 = new List<string> { "Iron", "Silver", "Steel" };
    public List<string> ArmorValueElite = new List<string> { "Silver", "Steel", "Titanium", "Arcane", "Dragon Glass", "Titanium" };
    public List<string> ArmorValueLegendary = new List<string> { "Titanium", "Arcane", "Dragon Glass", "Adamantium" };
    public List<string> Name1 = new List<string> { "Stupid", "Great", "Powerful", "Epic", "Masterfull", "Mighty", "Bountiful", "Foreverlasting", "Pointless", "Useless" };
    public List<string> Name2 = new List<string> { "Things", "Power", "Proportions", "Torment", "Legs", "Turnament", "Shit", "Crazyness", "Awesomeness" };
    public List<string> NameLenght3List = new List<string> { "Ragnarok", "Power", "Bondage", "Noobs", "Great Weakness", "Error", "404", "Torture", "SHIT STATS(No idea since I'm not good enough to programm stats based on names)", "The Dark Lord", "Emperor", "Monkey Kong", "I ran out of words", "Blablabla" };

    public Items(int iD,int droprarity)
    {
        ID = iD;
        int dropRarity = droprarity;
        NumberOfNames = NumberOfNamesGenerator(dropRarity);
        Rarity = RarityGenerator(dropRarity);
        ItemType = ItemTypeGenerator();
        if (ItemType == "Weapon")
        {
            WeaponType = DamageType[Random.Next(0, DamageType.Count)];

            if (ItemType == "Meele")
            {
                ItemType = WeaponListMeele[Random.Next(0, WeaponListMeele.Count)];
            }
            else
            {
                ItemType = WeaponListMagic[Random.Next(0, WeaponListMagic.Count)];
            }
        }
        if (ItemType == "Armor")
        {
            ItemType = ArmorList[Random.Next(0, ArmorList.Count)];
        }
        if (WeaponList.Contains(ItemType))
        {
            Value = WeaponValueGenerator(dropRarity);
            Damage = WeaponDamage(dropRarity);
        }
        else
        {
            Value = ArmorValueGenerator(dropRarity);
            HP = ArmorHealthGen(dropRarity);
            int RandomChance = Random.Next(0, 100);
            if (RandomChance <= 75)
            {
                MANA = ArmorManaGen(dropRarity);
            }
        }
        Name = NameGenerator();


    }
    public string RarityGenerator(int dropRarity)
    {
        switch (dropRarity)
        {
            case 1:
                Rarity = "Poor";
                break;
            case 2:
                Rarity = "Common";
                break;
            case 3:
                Rarity = "Uncommon";
                break;
            case 4:
                Rarity = "Elite";
                break;
            case 5:
                Rarity = "Rare";
                break;
            case 6:
                Rarity = "Legendary";
                break;
        }

        return Rarity;
    }
    public string ItemTypeGenerator()
    {
        return string.Format("{0}", ItemTypeList[Random.Next(0, ItemTypeList.Count)]);
    }
    public int NumberOfNamesGenerator(int dropRarity)
    {
        switch (dropRarity)
        {
            case 1:
                NumberOfNames = 2;
                break;
            case 2:
                NumberOfNames = Random.Next(2, 4);
                break;
            case 3:
                NumberOfNames = Random.Next(2, 4);
                break;
            case 4:
                NumberOfNames = Random.Next(2, 5);
                break;
            case 5:
                NumberOfNames = Random.Next(2, 5);
                break;
            case 6:
                NumberOfNames = Random.Next(2, 6);
                break;
        }
        return NumberOfNames;
    }
    public string NameGenerator()
    {
        int nameNR = NumberOfNames;
        if (nameNR == 2)
        {
            return string.Format("{0} {1} {2}", Rarity, Value, ItemType);
        }
        else if (nameNR == 3)
        {
            return string.Format("{0} {1} {2} of {3}", Rarity, Value, ItemType, NameLenght3List[Random.Next(0, NameLenght3List.Count)]);
        }
        else if (nameNR == 4)
        {
            return string.Format("{0} {1} {2} of {3} {4}", Rarity, Value, ItemType, Name1[Random.Next(0, Name1.Count)], Name2[Random.Next(0, Name2.Count)]);
        }
        else if (nameNR == 5)
        {
            return string.Format("{0} {1} {2} of {3} {4} {5}", Rarity, Value, ItemType, Name1[Random.Next(0, Name1.Count)], Name1[Random.Next(0, Name1.Count)], Name2[Random.Next(0, Name2.Count)]);
        }
        else if (nameNR == 6)
        {
            return string.Format("{0} {1} {2} of {3} {4} {5} {6}", Rarity, Value, ItemType, Name1[Random.Next(0, Name1.Count)], Name1[Random.Next(0, Name1.Count)], Name1[Random.Next(0, Name1.Count)], Name2[Random.Next(0, Name2.Count)]);
        }
        else
        {
            string ErrorSword = "Error Sword/Armor/blablabla";
            return ErrorSword;
        }
    }
    public string WeaponValueGenerator(int droprarity)
    {
        switch (droprarity)
        {
            case 1:
                Value = ValuePoor[Random.Next(0, ValuePoor.Count)];
                break;
            case 2:
                Value = ValueCommon[Random.Next(0, ValueCommon.Count)];
                break;
            case 3:
                Value = Value2[Random.Next(0, Value2.Count)];
                break;
            case 4:
                Value = ValueElite[Random.Next(0, ValueElite.Count)];
                break;
            case 5:
                Value = ValueElite[Random.Next(0, ValueElite.Count)];
                break;
            case 6:
                Value = ValueLegendary[Random.Next(0, ValueLegendary.Count)];
                break;

        }
        return Value;
    }

    public string ArmorValueGenerator(int droprarity)
    {
        switch (droprarity)
        {
            case 1:
                Value = ArmorValuePoor[Random.Next(0, ArmorValuePoor.Count)];
                break;
            case 2:
                Value = ArmorValueCommon[Random.Next(0, ArmorValueCommon.Count)];
                break;
            case 3:
                Value = ArmorValue2[Random.Next(0, ArmorValue2.Count)];
                break;
            case 4:
                Value = ArmorValueElite[Random.Next(0, ArmorValueElite.Count)];
                break;
            case 5:
                Value = ArmorValueElite[Random.Next(0, ArmorValueElite.Count)];
                break;
            case 6:
                Value = ArmorValueLegendary[Random.Next(0, ArmorValueLegendary.Count)];
                break;

        }
        return Value;
    }

    public int WeaponDamage(int droprarity)
    {
        switch (droprarity)
        {
            case 1:
                Damage = Random.Next(5, 20);
                break;
            case 2:
                Damage = Random.Next(25, 45);
                break;
            case 3:
                Damage = Random.Next(50, 105);
                break;
            case 4:
                Damage = Random.Next(110, 200);
                break;
            case 5:
                Damage = Random.Next(210, 500);
                break;
            case 6:
                Damage = Random.Next(550, 1200);
                break;
        }
        return Damage;
    }

    public int ArmorHealthGen(int droprarity)
    {
        switch (droprarity)
        {
            case 1:
                HP = Random.Next(5, 20);
                break;
            case 2:
                HP = Random.Next(25, 45);
                break;
            case 3:
                HP = Random.Next(50, 105);
                break;
            case 4:
                HP = Random.Next(110, 200);
                break;
            case 5:
                HP = Random.Next(210, 500);
                break;
            case 6:
                HP = Random.Next(550, 1200);
                break;
        }
        return HP;
    }

    public int ArmorManaGen(int droprarity)
    {
        switch (droprarity)
        {
            case 1:
                MANA = Random.Next(5, 20);
                break;
            case 2:
                MANA = Random.Next(25, 45);
                break;
            case 3:
                MANA = Random.Next(50, 105);
                break;
            case 4:
                MANA = Random.Next(110, 200);
                break;
            case 5:
                MANA = Random.Next(210, 500);
                break;
            case 6:
                MANA = Random.Next(550, 1200);
                break;
        }
        return MANA;
    }
}
}

这是调用生成器并将它们存储在列表中的代码的一部分,然后说明它的值。

namespace TestingRNG
{
class Program
{
    public Random Random = new Random();
    public int dropRate, itemAmmount;
    public List<Items> Loot = new List<Items>();
    public List<string> WeaponList = new List<string> { "Staff", "Sword", "Dagger", "Wand", "Axe", "Mace" };
    static void Main(string[] args)
    {
        Program Program = new Program();
    }
    public Program()
    {
        Console.WriteLine("How many items do you want to generate");
        itemAmmount = Int32.Parse(Console.ReadLine());

        for (int i = 0; i < itemAmmount; i++)
        {
            dropRate = Random.Next(1, 6);
            Loot.Add(new Items(i,dropRate));
        }
        foreach (var Item in Loot)
        {
            Console.WriteLine(Item.Name);
            if (WeaponList.Contains(Item.ItemType))
            {
                Console.WriteLine("Damage :{0}", Item.Damage);
            }
            else
            {
                Console.WriteLine("HP :{0}",Item.HP);
                Console.WriteLine("MANA :{0}",Item.MANA);
            }
            Console.ReadLine();

        }
    }
}
}

1 个答案:

答案 0 :(得分:2)

我相信这来自使用两个不同的Random实例,一个在Items类中,一个在主程序中。为了解决这个问题,我改变了你的item类,接受主程序的随机作为参数,一切都运行得很好。

<强> Items.cs

<强> ...

 $id=$_SESSION['id'];
 header ('Location:http://siaea.com/?page_id=833&location_id=$id');

<强> ...

$id

<强> Program.cs的

...

        public string ItemType { get; set; }
        public Random Random;
        public List<string> ItemTypeList = new List<string> { "Weapon", "Armor" };

修改 为了扩展Nikerym的评论,当创建一个新的Random时,它基于当前时间;因为所有物品都是同时生成的,每个物品都使用一个新的随机物,这导致每次生成相同的数字。

Random.Next()的使用是正确的,它只需要与上面类中使用的Random相同!