title: Detect consecutive objects with a common attribute in 2-D array
body: <p>I have a 2-D array of gems, all the gems have been given random color. Now i want to detect if three or more consecutive gems in a row or column has the same color. If so i want to do some action with those gems. </p>
  
  <pre><code>Gem gems[10][10];
  
  for(int i=0;i&lt;10;++i){
      for(int j=0;j&lt;10;++j){
          gems[i][j].setColor(GetRandInRange(0,6));   
      }
  }
  bool detectMatch(){
    for(int i=0;i&lt;10;++i){
      for(int j=0;j&lt;10;++j){
          // Code to detect three or more consecutive gems with same color
          // Give some special color to the matched gems
      }
  }
  </code></pre>
  
  <p>Here is how i tried but it doesn't work</p>
  
  <pre><code>  bool GameBoard::findMatch(){
      for(int i=0;i&lt;10;++i){
          int count=0;
          for(int j=0;j&lt;10;++j){
              if(j!=0){  
              if(gems[i][j].getColor()==gems[i][j-1].getColor()){ //Color same with previous one
                  int a=i, b=j;
                  while(gems[a][b].getColor()==gems[i][j].getColor() &amp;&amp; (a&lt;10 &amp;&amp; b&lt;10)){ // Check till the color does not match
                      count++;
                      a++;
                      b++;
                  }
                  if(count&gt;=3){  //If 3 or more have matched
                      for(int a=i, b=j, c=0;c&lt;count;++c){
                        gems[a][b].setColor(0);       
                      }
                      return true;
                  }
              }
          }
        }
      }
      return false;
  }
  </code></pre>
  
  <p>if you can help me with this code please</p>