Question by 
               MrReynevan2 · Jul 31, 2016 at 03:03 PM · 
                c#algorithmlogicpuzzle  
              
 
              Simple `Match 3+ in a row` pattern
I'm trying to create a simple minigame for my project that requires player to match 3+ objects in a row/column to get points. Currently I'm at a loss as to why it sometimes works, and sometimes not.
Also, more often than not my script matches blocks that it shouldn't.
Basically, I'm storing all blocks in a MinigameBlock[,] array, then projecting it on the scene. After that every move on the scene is refflected back on an array, then the function for matching rows kicks in:
 public void CheckForRows()
 {
     var list = new List<MinigameBlock>();
     for (int y = blocksArray.GetLength(1) - 1; y >= 0; y--)
         for (int x = blocksArray.GetLength(0) - 1; x >= 0; x--)
         {
             MinigameBlock[] r = null;
             if (blocksArray[x, y] != null)
                 r = blocksArray[x, y].Check();
             if (r != null) list.AddRange(r);
         }
     foreach (var obj in list.Distinct())
         Destroy(obj.gameObject); //For now
 }
 
               Here's the Check() function of MinigameBlock:
 public MinigameBlock[] Check()
 {
     var t = type;
     List<MinigameBlock> blocks = new List<MinigameBlock>();
     var pos = manager.blocksArray.FindIndex(this);
     if (pos[0] > 1)
     {
         var _blocks = new List<MinigameBlock>();
         _blocks.Add(this);
         var match = 1;
         for (int i = 1; i <= 2; i++)
             if (manager.blocksArray[pos[0] - i, pos[1]] != null && manager.blocksArray[pos[0] - i, pos[1]].type == t)
             {
                 match++;
                 _blocks.Add(manager.blocksArray[pos[0] - i, pos[1]]);
             }
             else break;
         if (match >= 3)
             for (int i = pos[0]; i < manager.blocksArray.GetLength(0); i++)
                 if (manager.blocksArray[i, pos[1]] != null && manager.blocksArray[i, pos[1]].type == t)
                 {
                     match++;
                     _blocks.Add(manager.blocksArray[i, pos[1]]);
                 }
                 else break;
         if (_blocks.Count >= 3)
             blocks.AddRange(_blocks);
     }
 
               (This is only for horizontal matching for now)
Also, here is my butchered attempt at populating the board with blocks:
 void InstantiateBlocks()
 {
     var parent = GameObject.Find("Table").transform;
     int lastId = 0, sameBlock = 0;
     int _x = 0, _y = 0;
     for (int y = -2; y <= 3; y++)
     {
         for (int x = -4; x < 4; x++)
         {
             if (Random.Range(0f, 1f) < 0.8)
             {
                 if (sameBlock == 0)
                     lastId = Random.Range(0, blocks.Count);
                 else if (sameBlock >= Random.Range(1, 3))
                 {
                     sameBlock = 1;
                     var _id = lastId;
                     while (lastId == _id)
                         lastId = Random.Range(0, blocks.Count);
                 }
                 else sameBlock--;
                 var b = Instantiate(blocks[lastId]);
                 b.transform.SetParent(parent, false);
                 b.transform.position = new Vector3(x, y, 0f);
                 blocksArray[_x, _y] = b.GetComponent<MinigameBlock>();
                 sameBlock++;
             }
             _x++;
         }
         _y++;
         _x = 0;
     }
 }
 
               What am I doing wrong, aside from the fact that the code needs craploads of optimization?
               Comment
              
 
               
              Your answer