在2D阵列中检查上方,下方,左侧,右侧2个空格的最佳方法?

时间:2012-03-07 16:18:43

标签: c# arrays 2d

我在C#中有一个5x5 2D数组。我需要检查数组上方,下方,右侧和左侧的2个空格。做这个的最好方式是什么? try catch语句适用于必须检查超出范围的点。

这是我到目前为止所使用的有效,但它看起来很草率。

bool[,] boardSpaces = new bool[5, 5] { 
    { true, true, true, true, true }, 
    { true, true, true, true, true },
    { true, true, true, true, true },
    { true, true, true, true, true },
    { true, true, true, true, true } 
};


for (int x = 0; x < 5; x++)
{
    for (int y = 0; y < 5; y++)
    {
        if (boardSpaces[x, y] == false)
        {
            try
            {
                if (boardSpaces[x - 1, y] == true && boardSpaces[x - 2, y] == true)
                {
                    validMoveRemaining = true;
                    break;
                }
            }
            catch { }

            try
            {
                if (boardSpaces[x + 1, y] == true && boardSpaces[x + 2, y] == true)
                {
                    validMoveRemaining = true;
                    break;
                }
            }
            catch { }
            try
            {
                if (boardSpaces[x, y - 1] == true && boardSpaces[x, y - 2] == true)
                {
                    validMoveRemaining = true;
                    break;
                }
            }
            catch { }
            try
            {
                if (boardSpaces[x, y + 1] == true && boardSpaces[x, y + 2] == true)
                {
                    validMoveRemaining = true;
                    break;
                }
            }
            catch { }

        }
    }
}

4 个答案:

答案 0 :(得分:1)

使用算术边界检查,而不是异常处理。您已经拥有行索引和列索引以及矩阵的维度。你为什么不简单地()那个?

if(x - 2 >= 0 && x + 2 < matrixWidth)
{
  //...
}
if(y - 2 >= 0 && y + 2 < matrixHeight)
{
  //...
}

你甚至可以用它来制作复合布尔表达式:

validMoveRemaining =
  !board[x,y] && (
    (x >= 2 && board[x - 1, y] && board[x - 2, y]) ||
    (x < boardWidth - 2 && board[x + 1, y] && board[x + 2, y]) ||
    (y >= 2 && board[x, y - 1] && board[x, y - 2]) ||
    (y < boardHeight - 2 && board[x, y + 1] && board[x, y + 2])
  );
你去吧! :)

答案 1 :(得分:0)

几乎就是这样。你肯定需要2个“fors”。 您可以通过添加更多检查来提高性能,例如,您不需要在第1列和第2列上选择左侧,因为没有2个左侧位置,因此您只能检查正确。相同的逻辑适用于行,因此在行[1,1]上,您只能检查右侧和下方。这样你就可以删除try catch。

此外,你必须检查,因为它是如此小的矩阵,可能额外的检查会使它慢于完全循环。

答案 2 :(得分:0)

根据更清晰的规格进行编辑。

public bool CanMove(int x, int y)
{
    if ( checkBeforeX(x, y) )
        return true;

    if ( checkAfterX(x, y) )
        return true;

    if ( checkBeforeY(x, y) )
        return true;

    if ( checkAfterY(x, y) )
        return true;

    return false;
}

private bool checkBeforeX(int x, int y)
{
    return x>=2 ? boardSpaces[x-1, y] && boardSpaces[x-2, y] : false;
}

private bool checkAfterX(int x, int y)
{
    return x<=2 ? boardSpaces[x+1, y] && boardSpaces[x+2, y] : false;
}

private bool checkBeforeY(int x, int y)
{
    return y>=2 ? boardSpaces[x, y-1] && boardSpaces[x, y-2] : false;
}

private bool checkAfterY(int x, int y)
{
    return y<=2 ? boardSpaces[x, y+1] && boardSpaces[x, y+2] : false;
}

答案 3 :(得分:0)

性能的最佳方式,而不是为数组添加边框

  bool[,] boardSpaces = new bool[9, 9] {  
    { false, false, false, false, false, false, false, false, false },  
    { false, false, false, false, false, false, false, false, false },  
    { false, false, true, true, true, true, true, false, false },  
    { false, false, true, true, true, true, true, false, false  }, 
    { false, false, true, true, true, true, true, false, false  }, 
    { false, false, true, true, true, true, true, false, false  }, 
    { false, false, true, true, true, true, true, false, false  },  
    { false, false, false, false, false, false, false, false, false },  
    { false, false, false, false, false, false, false, false, false },  
  };

  const int left = 2;
  const int right = left + 5;
  const int top = 2;
  const int bottom = top + 5;

  for (int x = left; x < right; x++)
  {
    for (int y = top; y < bottom; y++)
    {
      ..
    }
  } 

可读性的最佳方式而不是添加路线

  var directions = new[] 
  {
     new { dx = -1, dy = 0 }, 
     new { dx = +1, dy = 0 }, 
     new { dx = 0, dy = -1 }, 
     new { dx = 0, dy = +1 } 
  };

  for (int x = left; x < right; x++)
  {
    for (int y = top; y < bottom; y++)
    {
      foreach (var direction in directions)
      {
        if (boardSpaces[x + direction.dx, y + direction.dy] 
           && boardSpaces[x + 2 * direction.dx, y + 2 * direction.dy])
        {
          validMoveRemaining = true;
          break;
        } 

      }
      //..
    }
  }