0

I'm trying to get my program to check if my 2d array has 3 of the same value next to each other.

I currently have this code, but it is returning true whenever I get to count == 2 (sorry that its in dutch):

    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;
    }  

How do I get it that it only returns true whenever the count hits 3 or bigger.

1 Answer 1

1

If I've understood you right, you are looking for at least 3 equal values in a row:

[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

Let's implement it

// 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;
} 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for your comment and help, but one small question the RegularCandies current = speelveld[i, 0]; gives back the parameter where it currently is or does it something different?
current is a candidate value we check (if it repeats 3 times). We start from hypothesis that the leftmost value - speelveld[i, 0] repeats 3 times in a row: 4 4 4 ... if we fail, e.g. 4 4 2 ... we start testing 2 if we have: 4 4 2 2 2 ... then, say, on next failure 4 4 2 1 ... we keep on with 1 etc.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.