Hello everyone,
I am having some stackoverflow issues and hopefully someone can give me some insight into a non/less-recursive solution.
Ident[][] map = ...
private int explore(Ident item, int xcoord, int ycoord) {
if ((map[xcoord][ycoord] == null) || !map[xcoord][ycoord].equals(item))
return 0;
map[xcoord][ycoord] = null;
int sumX, sumY, counter = 1;
item.translate(xcoord, ycoord);
for (int y = -1; y <= 1; y++)
for (int x = -1; x <= 1; x++) {
sumX = x + xcoord;
sumY = y + ycoord;
if (((y != 0) || (x != 0)) && (sumX >= 0) && (sumX < map.length) &&
(sumY >= 0) && (sumY < map.[0].length))
counter += explore(item, sumX, sumY);
}
}
}
return counter;
}
This method is given a 2-dimensional array of Ident Objects, a target Ident and a starting position within the array. It recursively traverses the array counting how large of a continuous area the Ident occupies. It also centers the inputted Ident item in the middle of the area.
By looping through the map array and calling the explore method on any non null elements I can construct an array of Ident items centered in their areas, and with sizes relative to their areas.
One can see that with anything but small maps, the stack will overflow.
Anyone have an alternate method of accomplishing the same task? Or some insight to help me find one?