首页 > 解决方案 > 方法不断将相同的东西添加到列表中

问题描述

我正在创建一个 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;
  }

标签: c#text-based

解决方案


我偶然发现了 Creator 方法的最初 3 行,现在它可以正常工作了,原因是,正如 Phillipp 在游戏开发者堆栈中所说,旧代码保留了 monsterChoosen 变量的信息,再次调用时不会生成新的,保持相同的结果,只增加它的统计数据和级别,现在有了新的变量行,monsterChoosen 可以在每次调用时更新。

Random random = new Random();
int randId = random.Next(2);

Monster monsterChoosen = new Monster(monsterListPrefab.Find(m => m.Id == randId));

推荐阅读