std::string monsterNames[2] {"Weezer", "Leppin"};
for (int vecID = 1; vecID < 5; ++vecID)
{
if(newmonster.monsterData[vecID-1].posX != newmonster.monsterData[vecID].posX && newmonster.monsterData[vecID-1].posY != newmonster.monsterData[vecID].posY)
{
int randIndex = rand()% 2;
int randPosX = rand()% 3;
int randPosY = rand()% 3;
newmonster.CreateMonster(monsterNames[randIndex], randPosX, randPosY);
newmonster.monsterData.push_back(newmonster);
}
else
{
continue;
}
}
是的,由于if语句,此代码不运行。直到声明之后才会创建怪物,因此数组索引超出范围。 基本上,该程序随机的怪物名称和坐标,并没有if语句的所有作品。但我需要检查一个怪物是否已经存在于该位置,如果不存在,请重新运行循环..如何以更明智的方式做到这一点? xD 如果我没有for循环,随机数将始终相同。产生随机位置的怪物
你应该真正地分开创建“怪物”来检查另一个是否存在于同一位置(例如:有一个函数检查vector中的每个元素,如果它包含一个具有相同'posX'和'posY'的元素值) – UnholySheep
也是多余的。 – Banex