【问题标题】:C# method freezes entire programC#方法冻结整个程序
【发布时间】:2022-12-15 19:40:28
【问题描述】:

我已经在 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
    }

【问题讨论】:

    标签: c# methods freeze


    【解决方案1】:

    这是问题所在:for (int j = -1; i&lt;=1; j++)。应该是 j&lt;=1 而不是 i&lt;=1

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-09-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-03-04
      • 2021-12-04
      • 2018-09-01
      相关资源
      最近更新 更多