How to determine the number of bubbles that share that same color in a cluster and return as a list?

Hello Unity Community,

I am currently working on my first game in unity C# and I have been having problems figuring out a solution to count the total number of bubbles in a cluster and return it as a list.

So far, I have wrote and used a flood fill algorithm found on wikipedia on c# and tested it on visual studio. I had success implementing the algorithm on visual studio, however, I am having problems applying the flood fill algorithm to count the number of bubbles that share the same color and return them as a list.

Whenever I run the particular function using recursion onto visual studio, I get a stackoverflow exception, which I don’t know exactly why it is happening:

   //stack overflow exception...
        public List<char> CharClusterRecursive(char key,char target,int x,int y)
        {
            List<char> cluster = new List<char>();

            //base case
            if (key != target && textWorld[y, x] != key)
                return null;

            //add to cluster if matches criteria
            cluster.Add(key);

            //left
            if(x - 1 >= 0)
                cluster.AddRange(CharClusterRecursive(textWorld[y,x - 1],target,x - 1,y));
            //right
            if(x + 1 < width)
                cluster.AddRange(CharClusterRecursive(textWorld[y,x + 1],target,x + 1,y));
            //up
            if (y + 1 < height)
                cluster.AddRange(CharClusterRecursive(textWorld[y + 1, x], target, x, y + 1));
            if (y - 1 >= 0)
                cluster.AddRange(CharClusterRecursive(textWorld[y - 1, x], target, x, y - 1));

            return cluster;
        }

I have also tested the same function using recursion above in unity with the following function (game crashed when I run it with the script attached, I assume its also a stackoverflow exception):

Unity Function

	public List<Orb> ColorClusterRecursive()
	{
		//declarations
		//step 1 obtain orb's neighbors.
		List<Orb> theNeighbors = this.Neighbors ();
		//step 2 obtain orb's list by color.
		List<Orb> filterByColor = this.FilterByColor (theNeighbors,render.sprite.name);

		List<Orb> cluster = new List<Orb> ();
		cluster.Add (this);


		//base case:
		if (filterByColor.Count == 0 || filterByColor == null)
			return cluster;

		//for every neighbor of the same color as the original orb
		//iterate through them and Add potential neighbors of the same color through recursion.
		foreach(Orb neighbor in filterByColor)
		{
			List<Orb> MoreNeighbors = neighbor.Neighbors ();
			List<Orb> filterNeighbors = neighbor.FilterByColor (MoreNeighbors,render.sprite.name);
			cluster.Add (neighbor);
//breaks game			cluster.AddRange (neighbor.ColorClusterRecursive ());
		}

		return cluster;

	}

Neighbors Function

	//returns the neighbors the orb may have in the grid.
	public List<Orb> Neighbors()
	{
		//get locallPosition of the balls if in grid.
		int row = (int)transform.localPosition.y;
		int col = (int)transform.localPosition.x;
		List<Orb> list = new List<Orb> ();
//		float gridHeight = (float)matrix.dim.height;
//		float gridWidth = (float)matrix.dim.width;

		Debug.Log (transform.localPosition.x + ":" + transform.localPosition.y);
		Debug.Log (row + ":" + col);


		//left
		if (col > 0f)
		{
			Debug.Log ("L");
			list.Add (gridRef[row,col - 1]);
		}
		//right
		if (col + 1 < gridWidth)
		{
			Debug.Log ("R");
			list.Add (gridRef[row,col+1]);
		}
		//top
		if (row + 1 < gridHeight)
		{
			Debug.Log ("T");
			list.Add(gridRef[row + 1,col]);
		}
		//bot
		if (row - 1 >= 0f)
		{
			Debug.Log ("B");
			list.Add (gridRef[row - 1,col]);
		}
		return  list;
	}

FilterByColor Function

	//obtains a list of orbs and filters them by color
	public List<Orb> FilterByColor(List<Orb> listToSort, string color)
	{
		List<Orb> listToReturn = new List<Orb>();

		for(int i = 0;i < listToSort.Count;i++)
		{
			if(listToSort*.render.sprite.name == color)*
  •  	{*
    

_ listToReturn.Add (listToSort*);_
_
}_
_
}*_

* return listToReturn;*
* }*
Goal: To count the number of bubbles that share the same color and are connected with each other as neighbors.
Are there any better ways of finding a cluster of bubbles that share the same color on a grid in unity?
If so, please enlighten me with the knowledge : )

@NoseKills After surfing through the web for alternate solutions to the flood fill recursive algorithm in determining a number of orbs to be placed in a cluster, I found the solution for those that are trying to also create a bubbleshooter clone.

I use the following UPDATED code to determine how many orbs belong to a cluster and return it as a list (It now uses a while loop with a stack):

	public List<Orb> ClusterTrial3(string color)
	{
		//create a stack that will process orbs that get pushed onto the data structure.
		Stack<Orb> toprocess = new Stack<Orb> ();
		//push this instance of orb onto the stack.
		toprocess.Push (this);
		//set the process flag = true.
		this.inProcess = true;

		//the cluster list that we return to find out how many orbs are contained in a cluster.
		List<Orb> cluster = new List<Orb> ();

		//deterministic style loop that exits out once there are no more orbs to pop out from the stack.
		while (toprocess.Count > 0)
		{
			//remove the top item from the stack.
			Orb currentOrb = toprocess.Pop ();

			//find neighbors of the current_orb
			List<Orb> neighbors2 = currentOrb.Neighbors ();
			//filter the currentOrb's neighbors by color.
			List<Orb> filteredColor = currentOrb.FilterByColor (neighbors2, color);
			//add the currentOrb to the list
			cluster.Add (currentOrb);

			//loop through the list of filteredColors
			for (int i = 0; i < filteredColor.Count; i++)
			{
				//if the filteredColor orb is NOT in PROCESS of being ADDED to the cluster,
				if (!filteredColor*.inProcess)*
  •  		{*
    
  •  			//push it to the toprocess stack*
    

_ toprocess.Push (filteredColor );_
* //set it to be in process true.*
_ filteredColor*.inProcess = true;
}
}
}*_

* return cluster;*

*SPECIAL THANKS to this website for the solution: http://rembound.com/articles/bubble-shooter-game-tutorial-with-html5-and-javascript*_
Code is in javascript and html5, but should be easily converted to C# code using the libraries we have for it._