C# 检查邻居

C# 检查邻居,c#,arrays,neighbours,C#,Arrays,Neighbours,我有一个函数来检查数组的邻居,以及该元素是否等于1。X表示找到的每个邻居,v[l]表示每个0的位置。每次给我的代码都有一个问题,索引超出了数组的界限,我不知道还能做什么 public int modificari(int i,int j,int n,int m) { int x = 0; v = new int[n]; l=0; if (mat[i, j] == 1) { if (j

我有一个函数来检查数组的邻居,以及该元素是否等于1。X表示找到的每个邻居,v[l]表示每个0的位置。每次给我的代码都有一个问题,索引超出了数组的界限,我不知道还能做什么

 public int modificari(int i,int j,int n,int m)
    {
        int x = 0;
        v = new int[n];
        l=0;
        if (mat[i, j] == 1)
        {
            if (j++ < m)
            {
                if (mat[i, j++] == 1)
                    x++;
                else
                {
                    v[l] = i * n + j + 2;
                    l++;
                }
            }
            if (j++ < m && i++ < n)
            {
                if (mat[i++, j++] == 1)
                    x++;
                else
                {
                    v[l] = (i + 1) * n + j + 2;
                    l++;
                }
            }
            if (i++ < n)
            {
                if (mat[i++, j] == 1)
                    x++;
                else
                {
                    v[l] = (i + 1) * n + j + 1;
                    l++;
                }
            }
            if (j-- >= 0 && i++ < n)
            {
                if (mat[i++, j--] == 1)
                    x++;
                else
                {
                    v[l] = (i + 1) * n + j;
                    l++;
                }
            }
            if (j-- >= 0)
            {
                if (mat[i, j--] == 1)
                    x++;
                else
                {
                    v[l] = i * n + j;
                    l++;
                }
            }
            if (j-- >= 0 && i-- >= 0)
            {
                if (mat[i--, j--] == 1)
                    x++;
                else
                {
                    v[l] = (i - 1) * n + j;
                    l++;
                }
            }
            if (i-- >= 0)
            {
                if (mat[i--, j] == 1)
                    x++;
                else
                {
                    v[l] = (i - 1) * n + j + 1;
                    l++;
                }
            }
            if (j < n && i-- >= 0)
            {
                if (mat[i--, j++] == 1)
                    x++;
                else
                {
                    v[l] = (i - 1) * n + j + 2;
                    l++;
                }
            }
            if (x < 2 && x > 3)
                return 1;
            else
                return random();
        }
        return x;
    }

那真是一团糟。这是很难遵循,即使是一个有经验的程序员。为了可读性,通常不鼓励使用单字母变量名和内联++运算符

我很快就试图根据我对您试图实现的目标的最佳猜测来重写您的函数。我希望你能找到一种更适合你的方法来解决这个问题

注意:我根本没有测试这段代码,它可能有编译错误

public struct Point
{
    public int X;
    public int Y;
    public Point( int x, int y )
    {
        X = x;
        Y = y;
    }
}

public class Whatever
{
    // ...

    // Here is a list of the positions of all the neighbours whose values are
    // zero.
    List<Point> zeroPositions = new List<Point>();

    // ...

    public int Modificari(int pointX, int pointY)
    {
        // Determine dimensions of array.
        int height = mat.GetLength(0);
        int width = mat.GetLength(1);

        // Find the minimum and maximum positions bounded by array size. (So we
        // don't try to look at cell (-1, -1) when considering the neighbours of
        // cell (0, 0) for instance.
        int left = Math.Max( pointX - 1, 0 );
        int right = Math.Min( pointX + 1, width );

        int top = Math.Max( pointY - 1, 0 );
        int bottom = Math.Min( pointY + 1, height );

        // This is the number of neighbours whose value is 1.
        int oneCount = 0;

        zeroPositions.Clear();

        for( int y = top; y <= bottom; y++ )
        {
            for( int x = left; x <= right; x++ )
            {
                if( mat[x, y] == 1 )
                {
                    oneCount++;
                }
                else if( mat[x, y] == 0 )
                {
                    zeroPositions.Add( new Point( x, y ) );
                }
            }
        }

        return oneCount;
    }

    //...
}

另外,我真的建议你不要在函数中做太多的事情。尝试创建一个不同的函数来获取1的位置和返回0的数量。

当您收到异常时,代码中的哪一行会突出显示?在哪一行您会得到异常?出于对Cthulhu的热爱,请停止这种i、j、k、x的胡说八道,并使用有意义的标识符名称。另一方面,您经常使用++和-运算符。你知道他们实际上改变了变量的值吗?我想,相反,你可能必须使用mat[I+1,j]作为例子。这只会破坏我的大脑,而且我可能有从后面到前面的宽度和高度。。。我想不起来是哪一个了。谢谢你的帮助。这确实帮助我简化了源代码并获得了我需要的职位。再次感谢你。