我试图使用flood fill算法来查找列表中所有类似的相邻对象,以标记它们以进行删除。我试图修改维基百科上的伪代码,但都陷入了僵局。实施洪水填充算法的变体。
列表中的每个对象都有一个int X值,一个int Y值,一个Name和一个bool以标记为删除。我想匹配这个名字。
该程序挂起没有try-catch,只是退出。它不会返回错误消息。这是我迄今为止,试图找到任何直接上面的对象。
//Find neighbouring bubbles
gameGrid.DetectNeighbours2(gameGrid.planets.Last(), gameGrid.planets.Last().name);
//Flood fill algorithm to detect all connected planets
internal void DetectNeighbours(Planet p, Planet.Name planetName)
{
try
{
if (p.planet != planetName)
return;
p.deletable = true;
DetectNeighbours(GetTopNode(p), planetName);
}
catch (Exception err)
{
Debug.WriteLine(err.Message);
}
}
internal Planet GetTopNode(Planet b)
{
foreach (Planet gridPlanet in planets)
{
if (gridPlanet .Y == b.Y - 50)
return gridPlanet ;
}
return b; //Don't think this is right, but couldn't think of alternative
}
我说对了,'p.planet!= planetName'这个比较纯粹是为了检测没有行星的情况吗?那么这个答案是正确的做法之一。 – unkulunkulu
我是对的,那是在第一次之后添加的那个东西。 – unkulunkulu
谢谢。这样做更有意义,而且看起来更干净。 – David