C# 二维数组c中一行中的三个值

C# 二维数组c中一行中的三个值,c#,arrays,.net,C#,Arrays,.net,我试图让我的程序检查我的2d数组中是否有3个相邻的值相同 我目前有这段代码,但每当我得到count==2时,它就会返回true。很抱歉,这是荷兰语: bool ScoreRijAanwezig(RegularCandies[,] speelveld) { bool rij = false; int count = 0; ` for (int i = 0; i < speelveld.GetLength(0); i++)

我试图让我的程序检查我的2d数组中是否有3个相邻的值相同

我目前有这段代码,但每当我得到count==2时,它就会返回true。很抱歉,这是荷兰语:

    bool ScoreRijAanwezig(RegularCandies[,] speelveld)
    {
        bool rij = false;
        int count = 0;
   `    for (int i = 0; i < speelveld.GetLength(0); i++)
        {
            {
                for (int j = 0; j < speelveld.GetLength(1) - 2; j++)
                {
                    if (speelveld[i, j] == speelveld[i, j + 1])
                    {
                        count++;
                        if (speelveld[i, j + 1] == speelveld[i, j + 2])
                            count++;
                        if (count >= 3)
                        {
                            rij = true;
                            count = 0; 
                        }
                    }
                }
            }
        }
        return rij;
    }  

如何使计数达到3或更大时才返回真值。

如果我没有弄错,您在一行中查找至少3个相等的值:

[1 2 3 4 5
 5 7 8 9 1
 3 6 6 6 7   <- three 6 in a row (what we are looking for)
 3 7 8 9 0
 3 5 3 5 3]  <- just three 3, don't count
 ^
 three 3 but in a column, don't count
让我们实施它

// static: we don't use "this" in the method 
static bool ScoreRijAanwezig(RegularCandies[,] speelveld) {
  // row is too short 
  if (speelveld.GetLength(1) <= 2)
    return false;

  // scan each column
  for (int i = 0; i < speelveld.GetLength(0); ++i) {
    // we have at least 1 value in a row - it's a leftmost value
    RegularCandies current = speelveld[i, 0];
    int count = 1;

    // we are scanning row:
    //  if we have the same value, let's increment check count
    //  if not, let's start from the value again
    for (int j = 1; j < speelveld.GetLength(1); ++j) {
      if (current == speelveld[i, j]) {
        // increment and check: do we have 3 in the row?
        if (++count >= 3) 
          return true;
      }
      else {
        // sequence is broken, let's start again with count = 1 
        current = speelveld[i, j];
        count = 1; 
      } 
    }
  }

  // entire array has been scanned, no three in the row found
  return false;
} 

感谢您的评论和帮助,但有一个小问题:RegularCandies current=speelveld[i,0];返回参数当前所在的位置,或者它是否有所不同?current是一个候选值,我们将检查它是否重复3次。我们从假设最左边的值speelveld[i,0]连续重复3次开始:4。。。如果我们失败了,例如4 2。。。我们开始测试2如果我们有:4 4 2 2。。。然后,比方说,在下一次失败4 2 1。。。我们继续进行1等。