I recently turned my practice project into a match3-type game.
The game uses a 6x10 grid, internally stored into a single array, with each item being a positive integer (0-based). I have a function to detect groups of the same type of a given size, but once the group has been deleted, the tiles above need to be shifted down.
The unordered index of each deleted tile is also stored into a separate array.
The language I'm working with is Linden Scripting Language (LSL), although language-specific answers aren't needed (but very helpful), so even general examples are welcome.
Edit: My recursive function so far:
shift(integer i) // i == index of Grid, COLS == columns/width (6)
{
if( (integer)grid[i] != -1 ) return; // blocked by deleted value
Grid[i] = (integer)Grid[i-COLS]; // copy value from above
if(i >= COLS) // haven't reached upper limit of grid
{
shift(i-COLS); // recursive call up
}
else
{
Grid[i] = (integer)llFrand(TYPES); // reached top, randomize tile
// TYPES == integer, maximum amount of types
}
}
Improvements needed:
- Don't copy deleted values from above (skip over)
- Stop recursing early if multiple values were deleted vertically
- Move horizontally and find the lowest point to begin recursing from
Limitations of the environment:
- Limited to 64KB of memory (bytecode + stack/heap)
- Each call may potentially use 512 bytes of stack