# The algorithm for removing a group of adjacent balls of arbitrary shape

I'm writing a game that something like Diamond Dash and/or lines, you can delete on the horizontal / vertical and diagonal directions. Which algorithm is best suited for removing a group of balls (The algorithm to remove the neighboring balls which form a group freeform).

Not finding anything suitable himself wrote a recursive algorithm, works great :) the algorithm finds the neighboring balls (vertical / horizontal and diagonal) removes.

int** game_process::find_path(const int** map, size_t x0, size_t y0)
{
size_t j = x0;
size_t i = y0;
static int** close_list = allocate_memory(close_list, 8, 0);
bool conditions = (i >= 0 && i < 8 && j >= 0 && j < 8 && j + 1 < 8 && i + 1 < 8 && j - 1 >= 0 && i - 1 >= 0);

if(map[i][j] == -1)
{
return 0;
}
if(map[i][j] == 1)
{
if(conditions && map[i + 1][j - 1] == 1 && close_list[i + 1][j - 1] != -1)
{
close_list[i + 1][j - 1] = -1;
find_path(map, j - 1, i + 1);
}
if(conditions && map[i][j - 1] == 1  && close_list[i][j - 1] != -1)
{
close_list[i][j - 1] = -1;
find_path(map, j - 1, i);
}
if(conditions && map[i][j + 1] == 1 && close_list[i][j + 1] != -1)
{
close_list[i][j + 1] = -1;
find_path(map, j + 1, i);
}
if(conditions && map[i + 1][j] == 1 && close_list[i + 1][j] != -1)
{
close_list[i + 1][j] = -1;
find_path(map, j, i + 1);
}
if(/*conditions*/ i >=1 && map[i - 1][j] == 1 && close_list[i - 1][j] != -1)
{
close_list[i - 1][j] = -1;
find_path(map, j, i - 1);
}
if(conditions && map[i + 1][j + 1] == 1 && close_list[i + 1][j + 1] != -1)
{
close_list[i + 1][j + 1] = -1;
find_path(map, j + 1, i + 1);
}
if(/*conditions*/ i >= 1 && map[i - 1][j - 1] == 1 && close_list[i - 1][j - 1] != -1)
{
close_list[i - 1][j - 1] = -1;
find_path(map, j - 1, i - 1);
}
if(/*conditions*/ i >= 1 && map[i - 1][j + 1] == 1 && close_list[i - 1][j + 1] != -1)
{
close_list[i - 1][j + 1] = -1;
find_path(map, j + 1, i - 1);

}
}
return close_list;
}
-
If I understand the question correctly, you might start by looking at match-3 algorithms; these will identify the start of groups for you, and could be extended. There are a number of answers here: gamedev.stackexchange.com/questions/2607/… (full disclosure, includes one of mine) –  leander Apr 27 '13 at 13:51