How to destroy eleement of matrix

Hi guys,
I am trying to make a simple game with unity and I am stuck.
I have 10x10 matrix fulled with red and green squares. I am trying to make a fuction deleting squares, when three in a row are the same. I think you know rules of similar games and know how it works :slight_smile:
So, this is my code:

     for (i = 0; i < 8; i++)
            {
                for (j = 0; j < 8; j++)
                {                   
                    if ((klocki_[j].tag == klocki[i + 1][j].tag) && (klocki*[j].tag == klocki[i + 2][j].tag))*_

{
Destroy(klocki*[j].gameObject);*
Destroy(klocki[i + 1][j].gameObject);
Destroy(klocki[i + 2][j].gameObject);
}
if ((klocki[j].tag == klocki[j + 1].tag) && (klocki_[j].tag == klocki*[j + 2].tag))
{_

_Destroy(klocki[j].gameObject);_
_Destroy(klocki[j + 1].gameObject);_
_Destroy(klocki[j + 2].gameObject);
}
}
}*_

Ofc green are tagged green, and red are tagget red.
And here is result of this:
[110317-przechwytywanie.png|110317]

As You can see, 9th and 10th columns and rows are not fully cleared, and I cannot understand why.
Can somebody help me, pls?
Thanks in advance

Thank You very much!

You’re iterating your loops up to 8 to stay within the array range when you do the i+2, but this is preventing you from catching certain cases like you’ve shown above.


Try to visualize what your code is doing here. You go along the bottom-left 8x8 subsection, checking if that node makes a matching line North and East of itself. You never catch the cases where a full line rests entirely in the 9th and 10th columns and rows because you are trying to catch both Vertical and Horizontal cases in one piece of logic. You can optimize this a bit later, but here’s an adjusted version of your code:


for (int i = 0; i < 8; i++)
{
	for (int j = 0; j < 10; j++)
	{                   
		if ((klocki_[j].tag == klocki[i + 1][j].tag) && (klocki*[j].tag == klocki[i + 2][j].tag))*_

* {*
_ Destroy(klocki*[j].gameObject);
Destroy(klocki[i + 1][j].gameObject);
Destroy(klocki[i + 2][j].gameObject);
}
}
}*_

for (int i = 0; i < 10; i++)
{
* for (int j = 0; j < 8; j++)*
* {*
if ((klocki[j].tag == klocki[j + 1].tag) && (klocki_[j].tag == klocki*[j + 2].tag))
{
Destroy(klocki[j].gameObject);
Destroy(klocki[j + 1].gameObject);
Destroy(klocki[j + 2].gameObject);
}
}
}*_