【发布时间】:2021-02-26 02:02:07
【问题描述】:
我正在创建一个 Text Base 竞技场 rpg,其中每天都有新怪物添加到列表中,但只有一个怪物重复添加到列表中,并且由于某种原因每天都在增加相同的统计数据。 我需要的是创建具有不同值的不同对象而不是添加到列表中,第二部分效果很好。
此方法调用创建者。
public static List<Monster> MonsterOfTheDay()
{
int count = 0;
List<Monster> MonstersListOfTheDay = new List<Monster>();
while(count <= 5)
{
MonstersListOfTheDay.Add(Creator());
count++;
}
return MonstersListOfTheDay;
}
这是创造者
public static Monster Creator()
{
Random random = new Random();
Monster monsterChoosen = monsterListPrefab.Find(m => m.Id == random.Next(0, monsterListPrefab.Count -1));
monsterChoosen.Level = random.Next(monsterChoosen.Level, monsterChoosen.Level + 3);
//1Offensive, 2Defensive, 3Balance
monsterChoosen.Type = (Types)typeList.GetValue(random.Next(1, typeList.Length));
Console.WriteLine("Estou Aqui");
int atributes = monsterChoosen.Level * 3;
int spend = 0;
Console.WriteLine("Estou Aqui");
while(spend != atributes)
{
int chance = random.Next(0, 100);
if(monsterChoosen.Type == Types.Offensive)
{
if(chance >= 0 && chance <= 60)
{
monsterChoosen.Str++;
spend++;
}
if(chance >= 61 && chance <= 70)
{
monsterChoosen.Int++;
spend++;
}
if(chance >= 71 && chance <= 85)
{
monsterChoosen.Agi++;
spend++;
}
if(chance >= 86 && chance <= 100)
{
monsterChoosen.Vig++;
spend++;
}
}
else if(monsterChoosen.Type == Types.Defensive)
{
if(chance >= 0 && chance <= 60)
{
monsterChoosen.Vig++;
spend++;
}
if(chance >= 61 && chance <= 70)
{
monsterChoosen.Str++;
spend++;
}
if(chance >= 71 && chance <= 85)
{
monsterChoosen.Int++;
spend++;
}
if(chance >= 86 && chance <= 100)
{
monsterChoosen.Agi++;
spend++;
}
}
else if(monsterChoosen.Type == Types.Balance)
{
if(chance >= 0 && chance <= 25)
{
monsterChoosen.Str++;
spend++;
}
if(chance >= 26 && chance <= 50)
{
monsterChoosen.Int++;
spend++;
}
if(chance >= 51 && chance <= 75)
{
monsterChoosen.Agi++;
spend++;
}
if(chance >= 76 && chance <= 100)
{
monsterChoosen.Vig++;
spend++;
}
}
else if(monsterChoosen.Type == Types.Prefab)
{
spend++;
}
else
{
Console.WriteLine("Error");
}
}
return monsterChoosen;
}
【问题讨论】:
-
Random.Next 有第二个参数作为独占绑定,因此如果您的示例中的
typeList包含 3 个元素,则monsterChoosen.Type将在[1..3)中,因此为 1 或 2。如果typeList包含只有 2 个元素,类型总是 1 -
打错字结束
-
除了提到的其他问题外,每次您想要一个新的随机数时,您都会创建一个新的
Random对象。这是错误的。查看副本。
标签: c# text-based