c#方法冻结整个程序



我在c#中为我正在开发的一个小型Forms程序编写了以下方法,但是每当我尝试运行此方法时,整个程序都会冻结。

没有错误,我试图做for (int n = 0; n<8; n++)而不是while(true),但这似乎没有改变任何东西…

任何想法?提前感谢!

public bool legalMove(int y, int x)
{
// Check if the cell is occupied
if (grid[x, y] != 0)
return false;
// Check if there's an opponents circle somewhere around it
for (int i = -1; i<=1; i++)
for (int j = -1; i<=1; j++)
{
if (i == 0 && j == 0)
continue;
int row = x + i;
int col = y + j;
if (row >= 0 && row < 8 && col >= 0 && col < 8 && grid[col,row] == -turn)
{
// Now we know that there's an opponents circle somewhere around this space, we now check if it can be captured
while(true)
{
row += i;
col += j;
if (row < 0 || row > 7 || col < 0 || col > 7 || grid[row, col] == 0)
return false; // Outside of the board or an empty space
else if (grid[row, col] == turn)
return true; // No empty spaces between our cell and another cell of ours 
}
}
}
return false; // No cell found around ours
}

问题来了:for (int j = -1; i<=1; j++)。应该是j<=1而不是i<=1

最新更新