###

» Thu Jun 27, 2013 3:39 pm

Well, first of all, let's try not to test all the grid all the time. If your gameplay is based on switching 2 tiles (like most match 3 are), you should "just" test those 2 for shapes. In 2 steps then, first detect all the same color tiles, then detect if the current group makes a shape.

1/ detect all the same tiles, it depends how you store your grid. I'll suppose you have an 3 dimensional array for it (X,Y for the grid, Z for the tile type/color). Best way to detect every tile would then be a recursive function, selecting currentTile, flaging it with a Boolean (so it's not re-analysed), and comparing its color to the 4 adjacent tiles. Run the function on every detected tile then, and collect an array of (tileX,tileY) during this function.

You should then have something like this (let's say we detect a L shape) :

[(5,5),(6,5),(5,4),(5,3)]

2/ detecting the shape from the tile array returned

First of all, find Xmin and Ymin in the array, and then substract it to every couple, so you have something like this for the current example :

[(0,2),(1,2),(0,1),(0,0)]

Next thing is a function that tests if a matrix contains an certain shape. A shape is described by another matrix, just like the previous one. The test is done by comparing if every couple of the shape is also set in the matrix to test. If the matrix to test is bigger than the shape matrix, you will have to test it multiple times with all the offsets possibles so all the matrix is tested.

Don't go rotate the matrix or anything to test all the possible L orientations, just call again this test function with other shapes representing the L shape in multiple orientations.

Repeat the 2/ with all the shapes you want to test, and you should be done.

This seems really... heavy to code in C2. I personally would use a plugin for this if I did it.