Infinite loop when I try to generate randomly a 2D dungeon..

Hey everyone ! I’m trying to make a little game unsing random genaration.
I have a c++ code which works very well but when I import and “convert” this code in C# in unity it doesn’t work…

The problem appears when I’m trying to prevent rooms collisions…

So there is the code of the function :

	for(int i = 0; i < tabRoom.Length; i++)
	{
		if(i == 0)
		{
			tabRoom*.Init(UnityEngine.Random.Range(iMinWidthRoom, iMaxWidthRoom),UnityEngine.Random.Range(iMinHeightRoom, iMaxHeightRoom),iMapWidth,iMapHeight);*

_ tabRoom*.PosOk = true;_
_
}_
_
else*_
* {*
_ /While the new room collides with another room we change her position/
while(tabRoom*.PosOk == false)
{
/The function init changes the room’s position and the room’s size randomly/_

_tabRoom.Init(UnityEngine.Random.Range(iMinWidthRoom,iMaxWidthRoom),
UnityEngine.Random.Range(iMinHeightRoom,
iMaxHeightRoom),iMapWidth,iMapHeight);
//We check if there is a collision with the new room or not*

* for(int j = 0; j < tabRoom.Length; j++)
{
if((tabRoom[j].iXpos > tabRoom.iXpos + tabRoom.iWidth )
|| ((tabRoom[j].iXpos) + tabRoom[j].iWidth < tabRoom.iXpos )
|| (tabRoom[j].iZpos > tabRoom.iZpos + tabRoom.iHeight )
|| (tabRoom[j].iZpos + tabRoom[j].iHeight < tabRoom.iZpos))
{
tabRoom.PosOk = true; //No collision*

* }
else*

* {
//j = tabRoom.Length;
tabRoom.PosOk = false; //Collision*

* }
}
}
}
}*

So I hope someone will can help me because I have no idea… :confused:_

The logic in your while loop looks flawed to me. Look at these highlighted lines:

*a* while(tabRoom_.PosOk == false) **_

{
b for(int j = 0; j < tabRoom.Length; j++) **
{

if( …) { }
c else
{
d tabRoom_.PosOk = false;
e j = tabRoom.Length;
_

}
}
}
Once you fall into that else branch at c, you’re stuck in an infinite loop, because you’re setting PosOk to false d, and you’re also setting j to tabRoom.Length e, which means that the for(int j=0; at b condition will never be true, and that contains the only bit of code that could have set PosOk to true.

Well, there are several things with this piece of code…

We check if there is a collision with the new room or not

First of all, at some point, i will be equal to j meaning there will most likely be a collision with itself at some point (when i == tabRoom.Length - 1 you have an infinite loop I suppose). Secondly, you constantly overwrite tabroom*.PosOk in that for loop but you only check it after the for loop (meaning that your code might ignore some collisions) which isn’t what you would want to do if you intend to do what you wrote in your comments…*
Changing it to something like this might work…
//We check if there is a collision with the new room or not
tabRoom*.PosOk = true; // before we check collision set this to no collision*

for(int j = 0; j < tabRoom.Length; j++)
{
if (j != i && (/Insert code that returns true when there is a collision/))
{
tabRoom*.PosOk = false; //Collision*
break; // Not required but quits checking for collision whn we already found one (ore efficient
}
}
// If we didn’t find a collision, PosOk is still true, if we did find a collision PosOk is set to false (obviously infinite is still possible in case there are no OK positions left and there is always a collision :D)

I found the solution, I’m completly stupid…

            for(int i = 0; i < tabRoom.Length; i++)
	{
		if(i == 0)
		{
			tabRoom*.Init(UnityEngine.Random.Range(iMinWidthRoom, iMaxWidthRoom),UnityEngine.Random.Range(iMinHeightRoom, iMaxHeightRoom),iMapWidth,iMapHeight);*

_ tabRoom*.PosOk = true;_
_
}_
_
else*_
* {*
_ /While the new room collides with another room we change her position/
while(tabRoom*.PosOk == false)
{
tabRoom.Init(UnityEngine.Random.Range(iMinWidthRoom, iMaxWidthRoom),UnityEngine.Random.Range(iMinHeightRoom, iMaxHeightRoom),iMapWidth,iMapHeight);
//We check if there is a collision with the new room or not*

* for(int j = 0; j < tabRoom.Length; j++)
{
!!!if( i != j )!!!
{
if( (tabRoom[j].iXpos > tabRoom.iXpos + tabRoom.iWidth )
|| ((tabRoom[j].iXpos) + tabRoom[j].iWidth < tabRoom.iXpos )
|| (tabRoom[j].iZpos > tabRoom.iZpos + tabRoom.iHeight )
|| (tabRoom[j].iZpos + tabRoom[j].iHeight < tabRoom.iZpos))
{
tabRoom.PosOk = true; //No collision*

* }
else*

* {
tabRoom.PosOk = false; //Collision*

* break;
}
}
}
}
}
}*_