public void CenterParentBetweenChildren(GameObject parentNode)
 {
     if (parentNode != null)
     {
         PuzzleNodeScript script = parentNode.GetComponent <PuzzleNodeScript>();
         int leftMostChildX      = (int)Mathf.Round(script.next[0].transform.position.x);
         int rightMostChildX     = (int)Mathf.Round(script.next[1].transform.position.x);
         // FIRST GOES FROM LEFT TO RIGHT LOCATING LEFT-EST CHILD
         for (int i = 0; i < script.next.Count; ++i)
         {
             if (script.next[i].transform.position.x < leftMostChildX)
             {
                 leftMostChildX = (int)Mathf.Round(script.next[i].transform.position.x);
             }
         }
         // THEN GOES FROM RIGHT TO LEFT LOCATING RIGHT-EST CHILD
         for (int i = script.next.Count - 1; i >= 0; --i)
         {
             if (script.next[i].transform.position.x > rightMostChildX)
             {
                 rightMostChildX = (int)Mathf.Round(script.next[i].transform.position.x);
             }
         }
         int   puzX = (int)Mathf.Round((rightMostChildX - leftMostChildX) / 2);
         float puzY = parentNode.transform.position.y;
         float puzZ = parentNode.transform.position.z;
         parentNode.transform.position = new Vector3(puzX, puzY, puzZ);
     }
 }
 public bool IsThisSolved(PuzzleNodeScript theScript)
 {
     //Returns if this PuzzeNode matches the goal
     if (theScript.config.SequenceEqual(goalList))
     {
         visitedNodes.Enqueue(goalList); //For debug printing
         return(true);
     }
     return(false);
 }
    public void UpdateNewNodeStuff(GameObject childNode, PuzzleNodeScript parentNode, int[] newConfigList, int i)
    {
        //DrawNodeLine(parentNode.gameObject, childNode.gameObject);
        parentNode.next.Add(childNode);
        PuzzleNodeScript childNodeScript = parentNode.next[i].GetComponent <PuzzleNodeScript>();

        childNodeScript.SetParent(parentNode.gameObject);
        childNodeScript.SetGVal(parentNode.Gn + 1);
        childNodeScript.SetUpConfig(newConfigList);
        Update_H_Vals(childNodeScript);
        childNodeScript.SetupTilePosDict();
        childNodeScript.SetUpTiles();
    }
    public void DrawAllLines(GameObject parentNode)
    {
        if (!parentNode)
        {
            return;
        }
        PuzzleNodeScript script = parentNode.GetComponent <PuzzleNodeScript>();

        for (int i = 0; i < script.next.Count; ++i)
        {
            DrawAllLines(script.next[i]);
        }
        DrawNodeLine(script.myParent, script.gameObject);
        return;
    }
 public void RemoveVisitedNodes()
 {
     // WE'RE HAVING ISSUES NOT REMOVING ALL DUPLICATES. COPYING LIST.
     for (int i = 0; i < leafNodes.Count; ++i)
     {
         if (leafNodes[i] != null)
         {
             PuzzleNodeScript script = leafNodes[i].GetComponent <PuzzleNodeScript>();
             if (HaveWeVisited(script.config))       //Checks that with the memorization list
             //Debug.Log("\t\tREMOVING = "+string.Join(", ",script.config));
             {
                 leafNodes.RemoveAt(i); //Removes if we've been there
                 --i;
             }
         }
     }
 }
    // Goes through the tree and finds all nodes with the Gn layer
    //  that we want to focus on first
    public void MakeGnValNodeList(GameObject parentNode, int GnVal)
    {
        if (!parentNode)
        {
            return;
        }
        PuzzleNodeScript script = parentNode.GetComponent <PuzzleNodeScript>();

        for (int i = 0; i < script.next.Count; ++i)
        {
            MakeGnValNodeList(script.next[i], GnVal);
        }
        if (script.Gn == GnVal)
        {
            GnValNodeList.Add(parentNode);
        }
        return;
    }
    public void Update_H_Vals(PuzzleNodeScript puzzleNode)
    {
        int HValText = 0;

        if (misplaced)
        {
            HValText = CountMisplacedTiles(puzzleNode.config);
        }
        else if (manhattan)
        {
            HValText = CountManhattanDistance(puzzleNode.config);
        }
        else
        {
            HValText = CountGaschnigsSwaps(puzzleNode.config);
        }
        puzzleNode.UpdateHVal(HValText);
    }
    public GameObject GetLeafWithLowestHn()
    {
        if (leafNodes.Count <= 0)
        {
            return(null);
        }
        PuzzleNodeScript lowestHnNode = leafNodes[0].GetComponent <PuzzleNodeScript>();

        for (int i = 1; i < leafNodes.Count; ++i)
        {
            PuzzleNodeScript nextNode = leafNodes[i].GetComponent <PuzzleNodeScript>();
            if (nextNode.Hn < lowestHnNode.Hn)
            {
                lowestHnNode = nextNode;
            }
        }
        //Debug.Log("\tLOWESTHN = "+string.Join(", ",lowestHnNode.config));
        return(lowestHnNode.gameObject);
    }
    public void GetAllLeafNodes(GameObject parentNode)
    {
        if (!parentNode)
        {
            return;
        }
        PuzzleNodeScript script = parentNode.GetComponent <PuzzleNodeScript>();

        for (int i = 0; i < script.next.Count; ++i)
        {
            GetAllLeafNodes(script.next[i]);
        }
        // THIS MEANS YOU'RE A LEAF, ADD IT!
        if (script.next.Count <= 0)
        {
            leafNodes.Add(parentNode);
        }
        return;
    }
    // This finds the highest Gn in the entire tree
    public void UpdatehighestGnVal(GameObject parentNode)
    {
        if (!parentNode)
        {
            return;
        }
        PuzzleNodeScript script = parentNode.GetComponent <PuzzleNodeScript>();

        for (int i = 0; i < script.next.Count; ++i)
        {
            UpdatehighestGnVal(script.next[i]);
        }
        // THIS MEANS YOU'RE A LEAF! Check your Gn!
        if (script.next.Count <= 0)
        {
            if (script.Gn > highestGnVal)
            {
                highestGnVal = script.Gn;
            }
        }
        return;
    }
    public bool BestFirstExpandNodes(GameObject parentNode)
    {
        if (!parentNode || callCount >= maxCalls)
        {
            return(false);
        }
        callCount++; //THIS IS TO PREVENT STACK SMASHING
        // GRABS SCRIPT FOR PARENT NODE & CHECKS IF WE'VE VISITED
        PuzzleNodeScript parentScript = parentNode.GetComponent <PuzzleNodeScript>();

        // DOES THIS PARENT NODE SOLVE THE PUZZLE??
        if (IsThisSolved(parentScript))
        {
            return(true);
        }
        // THIS IT JUST DOUBLE CHECKING NO LOOPS HAPPEN
        if (HaveWeVisited(parentScript.config))
        {
            return(false);
        }
        // ADDING PARENT TO VISITEDNODES LIST
        visitedNodes.Enqueue(parentScript.config);
        // MAKES CHILDREN
        MakeChildrenNodes(parentScript);
        // GETS A LIST OF ALL LEAFS IN THE TREE
        leafNodes = new List <GameObject>();
        GetAllLeafNodes(theRootNode);
        // REMOVES THE LEAF NODES THAT WE'VE VISITED
        RemoveVisitedNodes();
        GameObject lowestHnNode = GetLeafWithLowestHn();

        if (!lowestHnNode)  //I really don't see this happening.
        {
            return(false);
        }
        BestFirstExpandNodes(lowestHnNode);
        return(false);
    }
 void Start()
 {
     rootsScript = theRootNode.GetComponent <PuzzleNodeScript>();
     solvableText.SetActive(false);
     InitOccupiedLocDict();
 }
    public void MakeChildrenNodes(PuzzleNodeScript myPuzzleNode)
    {
        AvailableMovesDict = new Dictionary <int, List <int> >()
        {
            { 0, new List <int>()
              {
                  1, 3
              } },
            { 1, new List <int>()
              {
                  0, 2, 4
              } },
            { 2, new List <int>()
              {
                  1, 5
              } },
            { 3, new List <int>()
              {
                  0, 4, 6
              } },
            { 4, new List <int>()
              {
                  1, 3, 5, 7
              } },
            { 5, new List <int>()
              {
                  2, 4, 8
              } },
            { 6, new List <int>()
              {
                  3, 7
              } },
            { 7, new List <int>()
              {
                  4, 6, 8
              } },
            { 8, new List <int>()
              {
                  7, 5
              } }
        };
        //Debug.Log("MAKE CHILDREN()"+string.Join(", ", myPuzzleNode.config));
        //Finds the zero location, passes that in as the argument along with the list
        int           zerosIndex      = FindIndexOf(myPuzzleNode.config, 0);
        List <int>    nextMoves       = new List <int>(AvailableMovesDict[zerosIndex]);
        Queue <int[]> listOfNextMoves = new Queue <int[]>(CreateQueueOfNextMoves(myPuzzleNode.config, nextMoves));
        //This pads out the nodes so they don't clutter
        int        maxSpacing  = maxWidth / (myPuzzleNode.Gn + 1);
        int        divisions   = listOfNextMoves.Count;
        int        myHeight    = myPuzzleNode.Gn + 1;
        List <int> spacingList = new List <int>(GetSpacing(maxSpacing, divisions, myHeight));
        //This goes through each new puzzleNode and connects them to their parent
        int i = 0;

        //This makes nodes, might be good to make a new function?
        while (listOfNextMoves.Count != 0)
        {
            int[]      listToAddForNewPuzzleNode = listOfNextMoves.Dequeue();
            GameObject theNewPuzzleNode          = Instantiate(puzzleNodePrefab, myPuzzleNode.transform.position, Quaternion.identity) as GameObject;
            theNewPuzzleNode.transform.parent = myPuzzleNode.transform;
            Vector3 placement = new Vector3(myPuzzleNode.transform.position.x + spacingList[i], myPuzzleNode.transform.position.y - 5, 1);
            theNewPuzzleNode.transform.position = placement;
            UpdateNewNodeStuff(theNewPuzzleNode, myPuzzleNode, listToAddForNewPuzzleNode, i);
            i++;
        }
        myPuzzleNode.IMadeChildren = true;
    }