/// <summary> /// CalculateUnion determines the roots involved and performs a union between /// two squares values (representing two points in the maze). /// On a success it returns a Pair object with the information necessary to later /// pass on to observers (a GridPoint, and a direction). On a failure (which shouldn't /// happen) it returns null. /// </summary> /// <param name="randomNumber">the GridPoint to exit from</param> /// <param name="gridPoint">the new GridPoint the exit leads to</param> /// <returns>an object of the Pair class, with Integer and GridPoint.Direction values, /// or null on failure</returns> private Pair <int, GridPoint.Direction> CalculateUnion(GridPoint gridPoint, GridPoint newPoint) { //Variables int here = GetSquareId(gridPoint); //cell value of GridPoint int next = GetSquareId(newPoint); //cell value of newPoint int hereRoot = 0; int nextRoot = 0; //find the root of here try { hereRoot = disjointSet.Find(here); } catch (Exception e) { MessageBox.Show(e.Message); } //find the root of next try { nextRoot = disjointSet.Find(next); } catch (Exception e) { MessageBox.Show(e.Message); } //if a value of 0 is returned, it is a top root. Use its square //value instead. if (hereRoot == 0) { hereRoot = here; } else if (nextRoot == 0) { nextRoot = next; } //attempt union, and return a Pair object if successful try { disjointSet.Union(nextRoot, hereRoot); //Adds edges to the extended graph extendedGraph.AddEdge(here, next, 1); extendedGraph.AddEdge(next, here, 1); return(new Pair <int, GridPoint.Direction>(here, gridPoint.GetDirection(newPoint))); } catch (Exception e) { MessageBox.Show("Union error: " + here + " " + next + " ; " + e.Message); } return(null); }