Answers for "Determining groups in a 2D array by checking neighbours (and their neighbours etc)"
http://answers.unity.com/questions/770764/determining-neighbours-in-2d-array-without-repeati.html
The latest answers for the question "Determining groups in a 2D array by checking neighbours (and their neighbours etc)"Answer by robertbu
http://answers.unity.com/answers/772672/view.html
Here is one algorithm. I sense is that it produces a good result, but not an optimal result, but in a quick 'napkin' sketch, I could not find a situation where it did not come up with an optimal result. I define optimal result as the fewest game objects.
- Start with a nested for() loop that walks through each cell starting in the upper left corner.
- If a cell has been marked as processed, go to the next cell
- If a cell has not been process, it becomes an anchor cell
- Check the cell below the anchor cell to see if it is a match, expand to 1 x 2 area if it is a match
- If previous check succeeded, check the two cells on the right to see if you can expand it to a 2 x 2.
- If the previous check failed, see if you can expaned to 2 x 1 area.
- And so on
So basically you cycle back and forth between right and down. At each cycle, you see if you can expand your rectangle by testing the number that would grow the rectangle in either the down direction or the right direction. You stop when you cannot expand either right or down. The result becomes a single game object, and the cells that make that result are marked as processed.
Since you are walking the cells in a nested for() loop, you will know that everything to the left and above you will always be processed.Sun, 17 Aug 2014 14:01:09 GMTrobertbuAnswer by smoggach
http://answers.unity.com/answers/770866/view.html
This sounds like a job for a recursive algorithm and a data structure.
Your data structure should contain whatever 'tileBlue', 'tileGreen', etc... are as well as a boolean (have i been checked?)
Your recursive algorithm should check the surrounding tiles of any given tile and run itsself on any that are equal and not already checked. Then your main nested loop can just skip any that are already checked and run your algorithm on any that arent.Thu, 14 Aug 2014 14:00:26 GMTsmoggach