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