我有一个程序,可以在其中从3种可能的字符串数组中随机选择一个字符串,即“ START_ROOM”,“ MID_ROOM”,“ END_ROOM”。
程序的这一部分工作正常,我随机得到一个字符串,能够将其分配给变量。然后,我检查以确保尚未选择所选值(无论是起始房间还是终止房间),因为这两个值不能重复。
为此,我制作了一个数组,当确认某个值可用时,将其添加到此数组中,然后在选择下一个值时将其与该数组中的所有值进行比较(chosenTypesList)< / p>
最初,我将selectedTypesList中的所有字符串设置为“ blah” 然后,我经历了第一次手动分配类型的迭代,并将该值添加到selectedTypesList中,这部分工作正常,并且永远不会覆盖该值。下面是以下代码,在其中我循环分配其余房间类型的过程。
for(i = 1; i < 7; i++)
{
int typeSwitcher = 1;
while (typeSwitcher == 1)
{
randTypeInt = rand() % 3;
printf("The random type int is : %d\n", randTypeInt);
typeTemp = roomTypes[randTypeInt];
printf("The chosen type is: %s\n", typeTemp);
for(k = 0; k < 7; k++)
{
if(typeTemp == chosenTypesList[k])
{
//Start by checking if it is start or end room
if(typeTemp == "START_ROOM" || typeTemp == "END_ROOM")
{
//If typeTemp is one of these and is also in the list then we have to pik a new type for it
break;
}else if(typeTemp == "MID_ROOM" && midRoomCounter == 5){
//Then we cannot ave another mid room in the list and will have to pick again
break;
}else{
typeSwitcher = 0;
}
}else{
typeSwitcher = 0;
}
}
}
strcpy(typeConfirmed, typeTemp);
if(typeConfirmed == "MID_ROOM")
{
midRoomCounter++;
}
chosenTypesList[typesItr] = typeConfirmed;
printf("rinting current chosenTypesList\n");
int m;
for(m = 0; m < 7; m++)
{
printf("%s, ", chosenTypesList[m]);
}
typesItr++;