Hi there.
As the title says, i have a 2d array containing gameobjects.
All gameobjects (which are some sort of cubes) have a type(a number/color) and they are generated randomly and added to the 2d array.
Every gameobject knows its 4 neighbours(if there are some, otherwise the entry is null) and they know their own type.
I now want to find the groups/islands within the 2d array and want every gameobject to have some kind of collection, lets say a list which has all gameobjects in it which belong to this group.
So if this is my table/2d-array, the orange 1’s in the top-left corner would be one island/group.
Each of them shall have a list with all 7 gameobjects in it.
I tried many things up to now but could not find a solution or a proper algorithm to do this.
I’ve read something about flood fill algorithms but trying to implement this led to a crash so i had some infinite loops.
My last approach was to ‘exchange’ both gameobjects when they meet. so i iterate through all gameobjects and when a go has a neighbour with the same color, the go gets written into this neighbours list and vice versa.
After that, the go writes itself into all gameobjects that are already in the neighbours list and it writes the neighbour is written into all gameobjects which already are in the current gameobjects list.
This works quite okay but will give me wrong results for the last gameobjects in long lines(longer than 3). In the lists of these objects, entries are missing.