出于某种原因,我无法让我的方形跟踪算法工作,出于某种原因,它根本不会进入while循环。这是我的代码
void findEdgeTiles()
{
List<int> edgeRow = new List<int>();
List<int> edgeCol = new List<int>();
int dir = 0;
//get starting point
for(row = 0; row < stationWidth-1; row++)
{
for(col = 0; col < stationHeight-1; col++)
{
if(roomType[row,col] != (int)room.None)
{
edgeRow.Add(row);
edgeCol.Add(col);
break;
}
}
if(roomType[row,col] != (int)room.None)
{
break;
}
}
//north = 0, east = 1, south = 2, west = 3, minus to turn left, plus to turn right
//turn left
dir = (dir-1)%4;
//move left
row--;
Debug.Log(row + " != " + edgeRow[0] + " && " + col + " !=" + edgeCol[0]);
while(row != edgeRow[0] && col != edgeCol[0])
{
Debug.Log("Entered While loop");
if(roomType[row,col] != (int)room.None)
{
//add edge tile
edgeRow.Add(row);
edgeCol.Add(col);
//turn left
dir = (dir-1)%4;
//move forward
if(dir == 0)
{
col++;
}
if(dir == 1)
{
row++;
}
if(dir == 2)
{
col--;
}
if(dir == 3)
{
row--;
}
}
if(roomType[row,col] == (int)room.None)
{
//turn right
dir = (dir+1)%4;
//move forward
if(dir == 0)
{
col++;
}
if(dir == 1)
{
row++;
}
if(dir == 2)
{
col--;
}
if(dir == 3)
{
row--;
}
}
}
for(int i = 0; i < edgeCol.Count; i++)
{
roomType[edgeRow[i], edgeCol[i]] = (int)room.Landing;
}
}
Here's the theory of the algorithm
调试记录的内容类似于0!= 1&amp;&amp; 3!= 3表示While应该评估并开始循环,直到你回到起点。我在这做错了什么? while循环之前的所有内容都按预期工作。我试过while(row != edgeRow[0] || col != edgeCol[0])
,但这只是团结一致,也许是无限循环错误?
roomType [,]是用于映射的2D int数组和(int)room.none是枚举
答案 0 :(得分:0)
这有一些问题,调试帮助我理清。 while循环肯定应该是一个OR运算符,结果模数并没有像我想象的那样工作。 (0-1)%4
没有像我预期的那样导致3
,而是导致-1
对我来说完全没用!所以转而向左转,我需要使用`dir =(dir + 3)%4;&#39;这让我脱离了我的无限循环,并在我需要它的时候把我所有的边缘瓷砖都拿出来了,我添加了一个额外的条件,因此无法访问外部的角落最终没有作为边缘,这里&#39;我的工作代码:
void findEdgeTiles()
{
List<int> edgeRow = new List<int>();
List<int> edgeCol = new List<int>();
int dir = 0;
//get starting point
for(row = 0; row < stationWidth-1; row++)
{
for(col = 0; col < stationHeight-1; col++)
{
if(roomType[row,col] != (int)room.None)
{
edgeRow.Add(row);
edgeCol.Add(col);
break;
}
}
if(roomType[row,col] != (int)room.None)
{
break;
}
}
//north = 0, east = 1, south = 2, west = 3, minus to turn left, plus to turn right
//turn left
dir = (dir+3)%4;
//move left
row--;
Debug.Log("Moved to: "+ row + "," + col);
while(row != edgeRow[0] || col != edgeCol[0] || edgeCol.Count == 1)
{
//turn left or right
if(roomType[row,col] != (int)room.None)
{
//add edge tile as long as there is space beside it
if(roomType[row+1,col] == (int)room.None || roomType[row,col+1] == (int)room.None || roomType[row-1,col] == (int)room.None || roomType[row,col-1] == (int)room.None)
{
edgeRow.Add(row);
edgeCol.Add(col);
}
//turn left
dir = (dir+3)%4;
}
else if(roomType[row,col] == (int)room.None)
{
//turn right
dir = (dir+1)%4;
}
//move forward
moveDirection(ref dir);
}
}
void moveDirection(ref int dir)
{
if(dir == 0)
{
col++;
}
if(dir == 1)
{
row++;
}
if(dir == 2)
{
col--;
}
if(dir == 3)
{
row--;
}
}
希望有人觉得这很有用!