C# 参数超出范围错误
我的代码间歇性工作,但有时会出现参数超出范围错误,我不知道为什么,我将其缩小为一个函数:C# 参数超出范围错误,c#,unity3d,C#,Unity3d,我的代码间歇性工作,但有时会出现参数超出范围错误,我不知道为什么,我将其缩小为一个函数: void corridorFill() { int dir = 0; //Set initial coords int rand = Random.Range(0,freeCol.Count); List<int> freeCorRow = new List<int>(); List<int> freeCorCol = new L
void corridorFill()
{
int dir = 0;
//Set initial coords
int rand = Random.Range(0,freeCol.Count);
List<int> freeCorRow = new List<int>();
List<int> freeCorCol = new List<int>();
row = freeRow[rand];
col = freeCol[rand];
int fill = 0;
while(fill < area)
{
//Pick a random direction and go that way
//0 = north, 1 = east, 2 = south, 3 = west
dir = Random.Range(0,4);
if(directionIsSafe(dir, row, col, (int)room.Unreserved, roomType)
&& directionIsSafe(dir, row, col, (int)connect.Empty, connections))
{
//move in direction
moveDirection(dir);
freeCorRow.Add(row);
freeCorCol.Add(col);
if(fill > 0)
{
//place exit to previous tile
addExit(row, col, (dir+2)%4);
//change exits of previous room to connect
addExit(freeCorRow[freeCorRow.Count-2], freeCorCol[freeCorCol.Count-2], dir);
}
fill++;
}
else
{
bool set = false;
while(!set)
{
//direction is not safe therefore start again somewhere else, attached to what we already have
int r = Random.Range(0,freeCorRow.Count);
//check if a tile beside a known tile is free
dir = Random.Range(0,4);
//if the direction is safe, go that way
if(directionIsSafe(dir, freeCorRow[r], freeCorCol[r], (int)room.Unreserved, roomType)
&& directionIsSafe(dir, freeCorRow[r], freeCorCol[r], (int)connect.Empty, connections))
{
addExit(freeCorRow[r], freeCorCol[r], dir);
row = freeCorRow[r];
col = freeCorCol[r];
moveDirection(dir); //move in direction
addExit(row, col, (dir+2)%4); //place exit to previous tile
freeCorRow.Add(row);
freeCorCol.Add(col);
set = true;
}
}
fill++;
}
}
}
bool directionIsSafe(int dir, int row, int col, int roomname, int[,] checkType)
{
if(dir == 0 && col+1 < stationHeight)
{
if(checkType[row, col+1] == roomname)
{
return true;
}
else
{
return false;
}
}
else if(dir == 1 && row+1 < stationWidth)
{
if(checkType[row+1,col] == roomname)
{
return true;
}
else
{
return false;
}
}
else if(dir == 2 && col > 0)
{
if(checkType[row,col-1] == roomname)
{
return true;
}
else
{
return false;
}
}
else if(dir == 3 && row > 0)
{
if(checkType[row-1,col] == roomname)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
和安全功能:
void corridorFill()
{
int dir = 0;
//Set initial coords
int rand = Random.Range(0,freeCol.Count);
List<int> freeCorRow = new List<int>();
List<int> freeCorCol = new List<int>();
row = freeRow[rand];
col = freeCol[rand];
int fill = 0;
while(fill < area)
{
//Pick a random direction and go that way
//0 = north, 1 = east, 2 = south, 3 = west
dir = Random.Range(0,4);
if(directionIsSafe(dir, row, col, (int)room.Unreserved, roomType)
&& directionIsSafe(dir, row, col, (int)connect.Empty, connections))
{
//move in direction
moveDirection(dir);
freeCorRow.Add(row);
freeCorCol.Add(col);
if(fill > 0)
{
//place exit to previous tile
addExit(row, col, (dir+2)%4);
//change exits of previous room to connect
addExit(freeCorRow[freeCorRow.Count-2], freeCorCol[freeCorCol.Count-2], dir);
}
fill++;
}
else
{
bool set = false;
while(!set)
{
//direction is not safe therefore start again somewhere else, attached to what we already have
int r = Random.Range(0,freeCorRow.Count);
//check if a tile beside a known tile is free
dir = Random.Range(0,4);
//if the direction is safe, go that way
if(directionIsSafe(dir, freeCorRow[r], freeCorCol[r], (int)room.Unreserved, roomType)
&& directionIsSafe(dir, freeCorRow[r], freeCorCol[r], (int)connect.Empty, connections))
{
addExit(freeCorRow[r], freeCorCol[r], dir);
row = freeCorRow[r];
col = freeCorCol[r];
moveDirection(dir); //move in direction
addExit(row, col, (dir+2)%4); //place exit to previous tile
freeCorRow.Add(row);
freeCorCol.Add(col);
set = true;
}
}
fill++;
}
}
}
bool directionIsSafe(int dir, int row, int col, int roomname, int[,] checkType)
{
if(dir == 0 && col+1 < stationHeight)
{
if(checkType[row, col+1] == roomname)
{
return true;
}
else
{
return false;
}
}
else if(dir == 1 && row+1 < stationWidth)
{
if(checkType[row+1,col] == roomname)
{
return true;
}
else
{
return false;
}
}
else if(dir == 2 && col > 0)
{
if(checkType[row,col-1] == roomname)
{
return true;
}
else
{
return false;
}
}
else if(dir == 3 && row > 0)
{
if(checkType[row-1,col] == roomname)
{
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
bool directionisafe(int dir,int row,int col,int roomname,int[,]checkType)
{
if(dir==0&&col+10)
{
如果(检查类型[行,列-1]==房间名称)
{
返回true;
}
其他的
{
返回false;
}
}
else如果(dir==3&&row>0)
{
if(检查类型[行1,列]==房间名称)
{
返回true;
}
其他的
{
返回false;
}
}
其他的
{
返回false;
}
}
通过阅读代码,它似乎应该可以工作,而且它在某些时候可以工作,但不是所有时候都可以。我不明白为什么有时候它不起作用。感谢你对这个问题的任何解释
EDT
freeCol和freeRow是使用以下方法创建的:
if(genType == (int)shapes.cross)
{
//for odd numbers
if(stationWidth%2 == 1)
{
for(row = 0; row < stationWidth; row++)
{
for(col = 0; col < stationHeight; col++)
{
if((row <= stationWidth/2 + (crossArmSize/2.0f + 0.5f) - 1 && row >= stationWidth/2 - (crossArmSize/2.0f + 0.5f) + 1)
|| (col <= stationHeight/2 + (crossArmSize/2.0f + 0.5f) - 1 && col >= stationHeight/2 - (crossArmSize/2.0f + 0.5f) + 1))
{
roomType[row,col] = (int)room.Unreserved;
freeRow.Add(row);
freeCol.Add(col);
}
}
}
}
//for even numbers
else if(stationWidth%2 == 0)
{
for(row = 0; row < stationWidth; row++)
{
for(col = 0; col < stationHeight; col++)
{
if((row < stationWidth/2 + crossArmSize/2 && row >= stationWidth/2 - crossArmSize/2)
|| (col < stationWidth/2 + crossArmSize/2 && col >= stationWidth/2 - crossArmSize/2))
{
roomType[row,col] = (int)room.Unreserved;
freeRow.Add(row);
freeCol.Add(col);
}
}
}
}
corridorFill();
}
if(genType==(int)shapes.cross)
{
//对于奇数
如果(站宽%2==1)
{
对于(行=0;行<站宽;行++)
{
用于(col=0;col=stationWidth/2-crossArmSize/2)
||(col=stationWidth/2-crossArmSize/2))
{
roomType[行,列]=(int)room.Unreserved;
freeRow.Add(行);
freeCol.Add(col);
}
}
}
}
corridorFill();
}
我认为是随机的。范围包括在内。Tryint rand=Random.Range(0,freeCol.Count-1)代码>我认为是随机的。范围包括在内。Tryint rand=Random.Range(0,freeCol.Count-1)代码>在上一个函数中宽度和高度是否交换?
通常,行沿着高度,列沿着宽度。在上一个函数中宽度和高度是否交换?
通常,行沿着高度,列沿着宽度。好的,所以我改变了:
row = freeRow[rand];
col = freeCol[rand];
致:
现在它每次都能工作。我只是不明白为什么前者会抛出奇怪的结果,只是简单的不起作用,如果你能解释清楚的话,我很乐意听到 好的,所以我改变了:
row = freeRow[rand];
col = freeCol[rand];
致:
现在它每次都能工作。我只是不明白为什么前者会抛出奇怪的结果,只是简单的不起作用,如果你能解释清楚的话,我很乐意听到 它确实包含:Random.Range
有2个重载int
和float
float
是包含的,int
是独占的。我使用的int重载是最大独占的,因此没有问题,因此它确实是包含的:Random。Range
有2个重载int
和float
float
是包含的,而int
是独占的。我使用的int重载是最大独占的,因此没有问题,因为在这种情况下宽度和高度是相同的,而在这种情况下宽度和高度是相同的。您没有显示定义了freeRow
或freeCol
的位置,所以我认为这不可能透露任何信息。尝试使用调试器,或尝试制作一个。编辑问题以包括如何填充这些列表您没有显示freeRow
或freeCol
的定义位置,因此我认为不可能透露任何信息。请尝试使用调试器,或尝试创建一个。请编辑问题以包括如何填写这些列表