Example #1
0
    private void initLevelUpText(GameObject textObject)
    {
        TempText tt = textObject.GetComponent <TempText>();
        string   s;

        switch (currentBuff)
        {
        case Buff.SpeedBuff:
            s = "Speed ↑";
            break;

        case Buff.JumpBuff:
            s = "Jump ↑";
            break;

        case Buff.ShootDelayBuff:
            s = "Shooting ↑";
            break;

        case Buff.HealthBuff:
            s = "Health ↑";
            break;

        case Buff.BulletBuff:
            s = "Bullet ↑";
            break;

        default:
            s = currentBuff.ToString();
            break;
        }
        tt.Init(s);
    }
Example #2
0
    public static void positionStructure(BinaryTree head)
    {
        //For positions, find the height and width of the tree using the final depths array: width = length, height = max in the list
        //finalDepths = new List<int>();
        width  = 0;
        height = 0;

        getBinaryTreeDepths(head, 0);
        //Make more efficient: store only width and maximum depth rather than a list
        print($"Width of tree: {width}");      //{finalDepths.Count}");
        print($"Max Depth of tree: {height}"); //{finalDepths.Max()}");

        if (width == 0 || height == 0)
        {
            print("Invalid structure!");
            TempText.setTempText($"Sorry, the structure with head {head.name} is not a valid {head.GetType().Name} structure", 3);
            return;
        }

        //move head to be above void
        head.transform.parent.position = new Vector3(head.transform.parent.position.x, height * treeHeightBetween + 1, head.transform.parent.position.z);

        //position each node
        positionBinaryTree(head, Vector3.zero, treeWidthBetween * width);

        //restitch
        foreach (Link l in head.transform.parent.GetComponentsInChildren <Link>())
        {
            Node.restitch(l);
        }
    }
Example #3
0
    public void Update()
    {
        //switch iteration mode
        if (Input.GetKeyDown(KeyCode.LeftBracket))
        {
            selectedAlgorithm = ((int)selectedAlgorithm) == aMin ? (Algorithm)aMax : (Algorithm)(selectedAlgorithm - 1);
            TempText.setTempText($"Selected algorithm: {selectedAlgorithm.ToString()}", 3);
        }
        if (Input.GetKeyDown(KeyCode.RightBracket))
        {
            selectedAlgorithm = ((int)selectedAlgorithm) == aMax ? (Algorithm)aMin : (Algorithm)(selectedAlgorithm + 1);
            TempText.setTempText($"Selected algorithm: {selectedAlgorithm.ToString()}", 3);
        }

        if (Input.GetKeyDown(KeyCode.Semicolon))
        {
            iterationMode = ((int)iterationMode) == iMin ? (IterationMode)iMax : (IterationMode)(iterationMode - 1);
            TempText.setTempText($"Selected iteration mode: {iterationMode.ToString()}", 3);
        }
        if (Input.GetKeyDown(KeyCode.Quote))
        {
            iterationMode = ((int)iterationMode) == iMax ? (IterationMode)iMin : (IterationMode)(iterationMode + 1);
            TempText.setTempText($"Selected iteration mode: {iterationMode.ToString()}", 3);
        }
    }
Example #4
0
 public void startAlgo(Node n)
 {
     if (inAlgo)
     {
         TempText.setTempText("There is already an algorithm in process. Please finish that one's iterations before starting a new one", 3);
         return;
     }
     firstStep(n);
 }
Example #5
0
        /// <summary>
        /// Performs same operation as the Val function in VB
        /// </summary>
        /// <param name="myObject">Input Object</param>
        /// <returns>Returns input in double format. Returns 0 if null object is passed on.</returns>
        public double Val(object myObject)
        {
            string TempNum = "";
            string TempText;

            //If the string object is null return 0
            if (myObject == null)
            {
                return(0);
            }

            string Text = myObject.ToString();

            int Length = Text.Length;

            //If the length of string is 0 then return 0
            if (Length == 0)
            {
                return(0);
            }

            TempText = Text;
            for (int i = 1; i <= Length; i++)
            {
                if (TempText.Substring(0, 1).ToString() == ".")
                {
                    TempNum += TempText.Substring(0, 1).ToString();
                }
                else
                {
                    try
                    {
                        TempNum += int.Parse(TempText.Substring(0, 1)).ToString();
                    }
                    catch
                    {
                        break;
                    }
                }
                TempText = Text.Substring(i, Length - i);
            }

            if (TempNum == "")
            {
                TempNum = "0";
            }

            return(double.Parse(TempNum));
        }
Example #6
0
 void ReleaseDesignerOutlets()
 {
     if (GetWeatherButton != null)
     {
         GetWeatherButton.Dispose();
         GetWeatherButton = null;
     }
     if (HumidityText != null)
     {
         HumidityText.Dispose();
         HumidityText = null;
     }
     if (ResultsTitle != null)
     {
         ResultsTitle.Dispose();
         ResultsTitle = null;
     }
     if (SunriseText != null)
     {
         SunriseText.Dispose();
         SunriseText = null;
     }
     if (SunsetText != null)
     {
         SunsetText.Dispose();
         SunsetText = null;
     }
     if (TempText != null)
     {
         TempText.Dispose();
         TempText = null;
     }
     if (VisibilityText != null)
     {
         VisibilityText.Dispose();
         VisibilityText = null;
     }
     if (WindText != null)
     {
         WindText.Dispose();
         WindText = null;
     }
     if (ZipCodeEdit != null)
     {
         ZipCodeEdit.Dispose();
         ZipCodeEdit = null;
     }
 }
Example #7
0
    void HandleValueChanged(object sender, ValueChangedEventArgs args)
    {
        if (args.DatabaseError != null)
        {
            Debug.LogError(args.DatabaseError.Message);
            return;
        }
        //Debug.Log(args.Snapshot.Key);
        // Do something with the data in args.Snapshot
        foreach (var t in args.Snapshot.Children)
        {
            if (Regex.IsMatch(t.Key, "_" + num + "$"))
            {
                var c = t.Value;

                if (Regex.IsMatch(t.Key, "^hi"))
                {
                    HIText.SetActive(true);
                    HIText.GetComponent <Text>().text = "Heat Index:" + c.ToString();
                }
                if (Regex.IsMatch(t.Key, "^ir"))
                {
                    IRText.SetActive(true);
                    IRText.GetComponent <Text>().text = "IR:" + c.ToString();
                }
                if (Regex.IsMatch(t.Key, "^gs"))
                {
                    GasText.SetActive(true);
                    GasText.GetComponent <Text>().text = "Gas Sensor:" + c.ToString();
                }
                if (Regex.IsMatch(t.Key, "^hu"))
                {
                    HumidText.SetActive(true);
                    HumidText.GetComponent <Text>().text = "Humidity:" + c.ToString();
                }
                if (Regex.IsMatch(t.Key, "^te"))
                {
                    TempText.SetActive(true);
                    TempText.GetComponent <Text>().text = "Temperature:" + c.ToString();
                }
                if (Regex.IsMatch(t.Key, "^ld"))
                {
                    LDRText.SetActive(true);
                    LDRText.GetComponent <Text>().text = "LDR:" + c.ToString();
                }
            }
        }
    }
Example #8
0
    public static void createStructure(Node head)
    {
        GameObject structure = new GameObject($"Structure of {head.name}");

        structure.transform.position = head.transform.position;

        //head.transform.SetParent(structure.transform);

        structureSetparent(head, structure.transform);
        if (!validateStructure(structure))
        {
            print("Invalid structure!");
            TempText.setTempText($"Sorry, the structure with head {head.name} is not a valid {head.GetType().Name} structure", 3);
            return;
        }


        //figure out position based on structure type
        positionStructure(head);
    }
Example #9
0
        private bool OnCollision(Fixture fixtureA, Fixture fixtureB, FarseerPhysics.Dynamics.Contacts.Contact contact)
        {
            GameObject.Position = ConvertUnits.ToDisplayUnits(Body.Position);
            if (fixtureB.Body.UserData is Damageable)
            {
                int  damage = (int)Math.Round(Body.LinearVelocity.LengthSquared() / 100);
                bool hit    = ((Damageable)fixtureB.Body.UserData).TakeDamage(damage, GameObject);
                if (hit)
                {
                    TempText.Write(GameObject.Position,
                                   damage.ToString(),
                                   Color.Pink,
                                   1000f,
                                   2f);

                    string hitSound = $"shot0{Randomizer.Random.Next(1, 4)}";
                    Sounds.PlayEffect(hitSound, 25);
                    GameObject.Destroy();
                }
                return(hit);
            }
            return(true);
        }
Example #10
0
    public void inEditMethod()
    {
        if (inEdit)
        {
            if (Input.GetKeyDown(KeyCode.Return) || Input.GetMouseButtonDown(0))
            {
                //submit text to edit
                string fromField = inputField.text;
                short  newValue;
                if (short.TryParse(fromField, out newValue))
                {
                    editingNode.Value = newValue;
                    TempText.setTempText($"Node Value set to {newValue}", 1);
                    editingNode     = null;
                    inEdit          = false;
                    inputField.text = "";
                    inputField.DeactivateInputField();
                }
                else
                {
                    TempText.setTempText($"Input failed because {fromField} is invalid!", 2);
                    inputField.text = ""; //clear field
                    inputField.ActivateInputField();
                }
            }

            if (Input.GetKeyDown(KeyCode.X) || Input.GetKeyDown(KeyCode.Escape))
            {
                inputField.text = "";
                inputField.DeactivateInputField();
                TempText.setTempText($"Editing Aborted", 1);
                inEdit = false;
            }

            return; // no other node controls while editing
        }
    }
Example #11
0
 private void nodeCreation()
 {
     if (inCreation)
     {
         if (Input.GetKeyDown(KeyCode.Alpha1))
         {
             LinkedList.createNode(MouseLook.getCamPosition() + MouseLook.getCamForward() * 3, 0);
             TempText.fadeInDec = 0.2f;
             TempText.setTempText("Node created!", 1);
             inCreation = false;
         }
         if (Input.GetKeyDown(KeyCode.Alpha2))
         {
             BinaryTree.createNode(MouseLook.getCamPosition() + MouseLook.getCamForward() * 3, 0);
             TempText.fadeInDec = 0.2f;
             TempText.setTempText("Node created!", 1);
             inCreation = false;
         }
         if (Input.GetKeyDown(KeyCode.Alpha3))
         {
             GenericNode.createNode(MouseLook.getCamPosition() + MouseLook.getCamForward() * 3, 0);
             TempText.fadeInDec = 0.2f;
             TempText.setTempText("Node created!", 1);
             inCreation = false;
         }
     }
     else
     {
         if (Input.GetKeyDown(KeyCode.Z))
         {
             inCreation         = true;
             TempText.fadeInDec = 0f;
             TempText.setTempText("Node creation started: Press 1 for a LinkedList node, Press 2 for a Binary Tree Node, and Press 3 for a Generic Node", 300); // indefinite
         }
     }
 }
Example #12
0
    void HandleValueChanged(object sender, ValueChangedEventArgs args)
    {
        if (args.DatabaseError != null)
        {
            Debug.LogError(args.DatabaseError.Message);
            return;
        }
        //Debug.Log(args.Snapshot.Key);
        // Do something with the data in args.Snapshot
        foreach (var t in args.Snapshot.Children)
        {
            if (Regex.IsMatch(t.Key, "_" + num + "$"))
            {
                var c = t.Value;
                Debug.Log(c);
                if (Regex.IsMatch(t.Key, "^hi"))
                {
                    t1 = DateTime.Now;
                    HIText.SetActive(true);
                    HIText.GetComponent <Text>().text = "Heat Index:" + c.ToString() + "\nUpdated " + t1.ToLongTimeString();
                }
                if (Regex.IsMatch(t.Key, "^ir"))
                {
                    t2 = DateTime.Now;
                    IRText.SetActive(true);
                    IRText.GetComponent <Text>().text = "IR:" + c.ToString() + "\nUpdated " + t2.ToLongTimeString();
                }
                if (Regex.IsMatch(t.Key, "^gs"))
                {
                    t3 = DateTime.Now;
                    GasText.SetActive(true);
                    var comp = GasText.GetComponent <Text>();

                    comp.text = "Gas Sensor:" + c.ToString() + "\nUpdated " + t3.ToLongTimeString();
                    //comp.color = new Color(255, 0, 0);

                    if (float.Parse(c.ToString()) > 500f)
                    {
                        Debug.Log("HIGH");
                        comp.color = new Color(255, 0, 0);
                    }
                    else
                    {
                        Debug.Log("LOW");
                        comp.color = new Color(0, 0, 0);
                    }
                }
                if (Regex.IsMatch(t.Key, "^hu"))
                {
                    var comp = HumidText.GetComponent <Text>();
                    if (float.Parse(c.ToString()) > 100f)
                    {
                        comp.color = new Color(255, 0, 0);
                    }
                    else
                    {
                        comp.color = new Color(0, 0, 0);
                    }
                    t4 = DateTime.Now;
                    HumidText.SetActive(true);
                    comp.text = "Humidity:" + c.ToString() + "\nUpdated " + t4.ToLongTimeString();
                }
                if (Regex.IsMatch(t.Key, "^te"))
                {
                    var comp = TempText.GetComponent <Text>();
                    if (float.Parse(c.ToString()) > 35f)
                    {
                        comp.color = new Color(255, 0, 0);
                    }
                    else
                    {
                        comp.color = new Color(0, 0, 0);
                    }
                    t5 = DateTime.Now;
                    TempText.SetActive(true);
                    comp.text = "Temperature:" + c.ToString() + "\nUpdated " + t5.ToLongTimeString();
                }
                if (Regex.IsMatch(t.Key, "^ld"))
                {
                    var comp = LDRText.GetComponent <Text>();
                    if (float.Parse(c.ToString()) > 350f)
                    {
                        comp.color = new Color(255, 0, 0);
                    }
                    else
                    {
                        comp.color = new Color(0, 0, 0);
                    }
                    t6 = DateTime.Now;
                    LDRText.SetActive(true);
                    comp.text = "LDR:" + c.ToString() + "\nUpdated " + t6.ToLongTimeString();
                }
            }
        }
    }
Example #13
0
    //internal only, not a player control
    public static void flip(Link l)
    {
        Node to   = l.to;
        Node from = l.from;

        //fix pointing to
        to.pointingTo.Remove(l);
        from.pointingTo.Add(l);

        //fix next
        if (to.GetType() == typeof(BinaryTree))
        {
            BinaryTree toBT   = (BinaryTree)to;
            BinaryTree fromBT = (BinaryTree)from;

            //binary tree nodes can only have one inward facing node (structures also require this)
            foreach (Link linkTo in fromBT.pointingTo)
            {
                if (linkTo != null)
                {
                    TempText.setTempText("Sorry, flipping this link would create an invalid structure. Operation aborted", 3);
                    return;
                }
            }

            if (fromBT.Value < toBT.Value) // on the left
            {
                if (toBT.left == null)
                {
                    toBT.left = fromBT;
                }
                else
                {
                    Destroy(toBT.leftLink.gameObject);
                    toBT.left = fromBT;
                }

                //if the new one is on the left, that means that originally, this node had a greater value, meaning it's on the right.
                fromBT.right     = null;
                fromBT.rightLink = null;
            }
            else
            {
                if (toBT.right == null)
                {
                    toBT.right = fromBT;
                }
                else
                {
                    Destroy(toBT.right.gameObject);
                    toBT.right = fromBT;
                }
                //if the new one is on the left, that means that originally, this node had a greater value, meaning it's on the right.
                fromBT.left     = null;
                fromBT.leftLink = null;
            }
        }
        else if (to.GetType() == typeof(LinkedList))
        {
            LinkedList toLL   = (LinkedList)to;
            LinkedList fromLL = (LinkedList)from;

            toLL.next = fromLL;
        }
        else if (to.GetType() == typeof(GenericNode))
        {
            GenericNode toN   = (GenericNode)to;
            GenericNode fromN = (GenericNode)from;

            toN.nextNodes.Add(fromN);
            fromN.nextNodes.Remove(toN);
        }

        Node temp = l.from;

        l.from            = l.to;
        l.to              = temp;
        l.gameObject.name = $"{l.from.GetInstanceID()} to {l.to.GetInstanceID()}";
        //modified restitch
        Node   a        = l.from;
        Node   b        = l.to;
        string nodeName = $"{a.GetInstanceID()} to {b.GetInstanceID()}";

        print($"Restiching {a} to {b} with name: {nodeName}");
        GameObject arrow = b.transform.Find(nodeName).gameObject;

        //reposition and rotate
        arrow.transform.position = (a.transform.position + b.transform.position) / 2;
        arrow.transform.LookAt(b.transform);
        arrow.transform.SetParent(a.transform);
        float d     = Vector3.Distance(a.transform.position, b.transform.position);
        float scale = 0.2f * d;

        arrow.transform.localScale = new Vector3(scale, scale, scale - 0.2f);
    }
Example #14
0
    IEnumerator BinarySearch(BinaryTree head)
    {
        bool       found      = false;
        GameObject output     = createOutputAt(head);
        TMP_Text   outputText = output.GetComponentInChildren <TMP_Text>();

        //get value from input field
        int value = -1;

        player.inputField.ActivateInputField();
        TempText.setTempText("Please enter the number to search for... (Press space when finished)", 2);

        outputText.text = "Binary Search for...";

        yield return(StartCoroutine(WaitForIteration()));

        player.inputField.DeactivateInputField();
        string input = player.inputField.text;

        while (!Int32.TryParse(input, out value))
        {
            TempText.setTempText("Invalid input, try again", 2);

            yield return(StartCoroutine(WaitForKeyUp(KeyCode.Space)));

            player.inputField.ActivateInputField();

            yield return(StartCoroutine(WaitForIteration()));

            player.inputField.DeactivateInputField();
            input = player.inputField.text;
        }

        BinaryTree current = head;

        //first iteration
        outputText.text = $"Binary Search for {value}";
        yield return(StartCoroutine(WaitForIteration()));

        while (!found)
        {
            createSelectionAt(current);
            outputText.text = $"Does {current.Value} == {value}?";
            if (current.Value == value)
            {
                yield return(StartCoroutine(WaitForIteration()));

                removeSelectionAt(current);
                outputText.text = $"Yes, {value} found";
                found           = true;
                break;
            }

            yield return(StartCoroutine(WaitForIteration()));

            removeSelectionAt(current);

            if (current.left == null && current.right == null)
            {
                outputText.text = $"{value} not found in the tree...";
                break;
            }
            else
            {
                //conditional and be like
                if (current.left != null && value < current.Value)
                {
                    current = (BinaryTree)current.left;
                }
                else if (current.right != null && value >= current.Value)
                {
                    current = (BinaryTree)current.right;
                }
                else
                {
                    outputText.text = $"{value} not found in the list...";
                    break;
                }
            }
        }
        removeSelectionAt(current);

        yield return(StartCoroutine(WaitForIteration()));

        Destroy(output);

        inAlgo = false;
    }
Example #15
0
    IEnumerator LinearSearch(LinkedList head)
    {
        bool       found      = false;
        GameObject output     = createOutputAt(head);
        TMP_Text   outputText = output.GetComponentInChildren <TMP_Text>();

        //get value from input field
        int value = -1;

        player.inputField.ActivateInputField();
        TempText.setTempText("Please enter the number to search for... (Press space when finished)", 2);

        outputText.text = "Linear Search for...";

        yield return(StartCoroutine(WaitForIteration()));

        player.inputField.DeactivateInputField();
        string input = player.inputField.text;

        while (!Int32.TryParse(input, out value))
        {
            TempText.setTempText("Invalid input, try again", 2);

            yield return(StartCoroutine(WaitForKeyUp(KeyCode.Space)));

            player.inputField.ActivateInputField();

            yield return(StartCoroutine(WaitForIteration()));

            player.inputField.DeactivateInputField();
            input = player.inputField.text;
        }

        LinkedList current = head;

        //first iteration
        outputText.text = $"Linear Search for {value}";
        yield return(StartCoroutine(WaitForIteration()));

        while (current != null)
        {
            createSelectionAt(current);
            outputText.text = $"Does {current.Value} == {value}?";
            if (current.Value == value)
            {
                //print("found value");
                outputText.text = $"Yes, {value} found";
                found           = true;
                yield return(StartCoroutine(WaitForIteration()));

                removeSelectionAt(current);
                break;
            }

            yield return(StartCoroutine(WaitForIteration()));

            removeSelectionAt(current);
            current = (LinkedList)current.next;
        }

        if (!found)
        {
            outputText.text = $"{value} not found in the list...";
            yield return(StartCoroutine(WaitForIteration()));
        }

        Destroy(output);

        inAlgo = false;

        yield return(null);
    }
Example #16
0
    public void stitchNodes(Node n)
    {
        //print($"institch: {inStitch}");
        if (inStitch)
        {
            //type matching
            if (n.GetType() != stitchFrom.GetType())
            {
                //print("Type mismatch");
                TempText.setTempText("Type mismatch between nodes!", 2);
                inStitch   = false;
                stitchFrom = null;
                stitchTo   = null;
                return;
            }

            //discern type
            LinkedList  ll;
            GenericNode tree;
            BinaryTree  bt;

            stitchTo = n;

            if (stitchTo == stitchFrom)
            {
                TempText.setTempText("Cannot connect to itself!", 2);
                inStitch   = false;
                stitchFrom = null;
                stitchTo   = null;
                return;
            }

            //get type of current what you're stitching from
            if (ll = stitchFrom.gameObject.GetComponent <LinkedList>())
            {
                print("LL stitch");
                if (ll.next != null)
                {
                    if (ll.GetComponentInChildren <Link>())
                    {
                        GameObject arrow = ll.GetComponentInChildren <Link>().transform.gameObject;
                        Destroy(arrow);
                    }
                    Node.stitch(stitchFrom, stitchTo);
                    ll.next = stitchTo;
                }
                else
                {
                    Node.stitch(stitchFrom, stitchTo);
                    ll.next = stitchTo;
                }
            }
            else if (tree = stitchFrom.gameObject.GetComponent <GenericNode>())
            {
                tree.nextNodes.Add(stitchTo);
                tree.connections.Add(Node.stitch(stitchFrom, stitchTo));
            }
            else if (bt = stitchFrom.gameObject.GetComponent <BinaryTree>())
            {
                //check if somethin is already pointing into this node
                BinaryTree btTo = (BinaryTree)stitchTo;
                foreach (Link l in btTo.pointingTo)
                {
                    print("l");
                    if (l != null)
                    {
                        Node.removeLink(l);
                    }
                }

                if (stitchTo.Value < stitchFrom.Value) // on the left
                {
                    if (bt.left == null)
                    {
                        bt.left     = stitchTo;
                        bt.leftLink = Node.stitch(stitchFrom, stitchTo);
                    }
                    else
                    {
                        Destroy(bt.leftLink.gameObject);
                        bt.leftLink = Node.stitch(stitchFrom, stitchTo);
                        bt.left     = stitchTo;
                    }
                }
                else
                {
                    if (bt.right == null)
                    {
                        bt.right     = stitchTo;
                        bt.rightLink = Node.stitch(stitchFrom, stitchTo);
                    }
                    else
                    {
                        Destroy(bt.rightLink.gameObject);
                        bt.rightLink = Node.stitch(stitchFrom, stitchTo);
                        bt.right     = stitchTo;
                    }
                }
            }

            TempText.setTempText($"Node {stitchFrom.Value} connected to {stitchTo.Value}", 2);

            //After stitching:
            inStitch   = false;
            stitchFrom = null;
            stitchTo   = null;
        }
        else //select node, enter stitchin mode
        {
            stitchFrom = n;
            inStitch   = true;
            TempText.setTempText($"Node of Value {n.Value} selected", 2);
        }
    }
Example #17
0
    IEnumerator ShiftLinkedList(LinkedList head)
    {
        GameObject output     = createOutputAt(head);
        TMP_Text   outputText = output.GetComponentInChildren <TMP_Text>();

        //To shift a linked list, gotta keep track of the head, tail, and nodes number of shift away from both ends
        //get num to shift first:
        int shift = -1;

        player.inputField.ActivateInputField();
        TempText.setTempText("Please enter the number to shift by... (Press space when finished)", 2);

        outputText.text = "Shift by...";

        yield return(StartCoroutine(WaitForIteration()));

        player.inputField.DeactivateInputField();
        string input = player.inputField.text;

        while (!Int32.TryParse(input, out shift))
        {
            TempText.setTempText("Invalid input, try again", 2);

            yield return(StartCoroutine(WaitForKeyUp(KeyCode.Space)));

            player.inputField.ActivateInputField();

            yield return(StartCoroutine(WaitForKeyDown(KeyCode.Space)));

            player.inputField.DeactivateInputField();
            input = player.inputField.text;
        }

        //Count length of the list
        int        length  = 1;
        LinkedList current = head;
        List <int> values  = new List <int>(); //take in all values for graphic implementation

        while (current != null)
        {
            createSelectionAt(current);
            outputText.text = $"Current length = {length}";
            values.Add(current.Value);
            yield return(StartCoroutine(WaitForIteration()));

            length++;
            removeSelectionAt(current);
            current = (LinkedList)current.next;
        }

        //Now iterate across the list again, with the true number of shifts and the length known
        shift %= length;
        if (shift == 0)
        {
            outputText.text = $"Shifting by some multiple of the length of the list, therefore the original list is preserved.";
            yield return(StartCoroutine(WaitForIteration()));
        }
        else
        {
            outputText.text = $"Iterating, keeping track of the nodes {Mathf.Abs(shift)} from head/tail.";
            yield return(StartCoroutine(WaitForIteration()));

            LinkedList kForward = null, kBack = null, end = null;
            current = head;
            int index = 1;
            while (current.next != null)
            {
                createSelectionAt(current);
                if (index == Mathf.Abs(shift))
                {
                    kBack    = head;
                    kForward = current;
                }
                if (index > Mathf.Abs(shift))
                {
                    kBack = (LinkedList)kBack.next;
                }
                index++;
                yield return(StartCoroutine(WaitForIteration()));

                removeSelectionAt(current);
                current = (LinkedList)current.next;
            }
            end = current;

            if (shift > 0)
            {
                outputText.text = $"The new head will be {kBack.next.Value}, and the new tail will be {kBack.Value}";
                createSelectionAt(kBack);
                createSelectionAt(kBack.next);
                yield return(StartCoroutine(WaitForIteration()));

                //though it is theoretically efficient to shift in place, since unity is being used, it's more efficient to change the node values in place, rather than to actually move GameObjects
                for (int i = 0; i < shift; i++)
                {
                    //get last element, bring it to the front
                    print($"Moving with c: {values.Count}, first = {values[0]}, and last  = {values[values.Count-1]}, full = {values.ToString()}");
                    int item = values[values.Count - 1];
                    values.RemoveAt(values.Count - 1);
                    values.Insert(0, item);
                }
                current = head;
                index   = 0;
                while (current != null)
                {
                    current.Value = values[index];
                    current       = (LinkedList)current.next;
                    index++;
                }
                yield return(StartCoroutine(WaitForIteration()));

                removeSelectionAt(kBack);
                removeSelectionAt(kBack.next);
                outputText.text = $"Linked list shifted by {shift}";
                yield return(StartCoroutine(WaitForIteration()));
            }
            else
            {
                outputText.text = $"The new head will be {kForward.next.Value}, and the new tail will be {kForward.Value}";
                createSelectionAt(kForward);
                createSelectionAt(kForward.next);
                yield return(StartCoroutine(WaitForIteration()));

                //though it is theoretically efficient to shift in place, since unity is being used, it's more efficient to change the node values in place, rather than to actually move GameObjects
                for (int i = 0; i < Mathf.Abs(shift); i++)
                {
                    //get first element, bring it to the end
                    int item = values[0];
                    values.RemoveAt(0);
                    values.Add(item);
                }
                current = head;
                index   = 0;
                while (current != null)
                {
                    current.Value = values[index];
                    current       = (LinkedList)current.next;
                    index++;
                }
                yield return(StartCoroutine(WaitForIteration()));

                removeSelectionAt(kForward);
                removeSelectionAt(kForward.next);
                outputText.text = $"Linked list shifted by {shift}";
                yield return(StartCoroutine(WaitForIteration()));
            }
        }

        Destroy(output);

        inAlgo = false;

        yield return(null);
    }
Example #18
0
    public void firstStep(Node n)
    {
        inAlgo = true;
        IEnumerator routine;

        switch (selectedAlgorithm)
        {
        case Algorithm.BranchSums:
            if (n.GetType() != typeof(BinaryTree))
            {
                TempText.setTempText("Sorry, this type of node isn't valid for Branch Summation", 3);
                inAlgo = false;
                return;
            }
            routine = BranchSums((BinaryTree)n);
            StartCoroutine(routine);
            break;

        case Algorithm.LinkedListReversal:
            if (n.GetType() != typeof(LinkedList))
            {
                TempText.setTempText("Sorry, this type of node isn't valid for Linked List Reversal", 3);
                inAlgo = false;
                return;
            }
            routine = LinkedListReversal((LinkedList)n);
            StartCoroutine(routine);
            break;

        case Algorithm.LinearSearch:
            if (n.GetType() != typeof(LinkedList))
            {
                TempText.setTempText("Sorry, this type of node isn't valid for Linear Search", 3);
                inAlgo = false;
                return;
            }
            routine = LinearSearch((LinkedList)n);
            StartCoroutine(routine);
            break;

        case Algorithm.BinarySearch:
            if (n.GetType() != typeof(BinaryTree))
            {
                TempText.setTempText("Sorry, this type of node isn't valid for Binary Search", 3);
                inAlgo = false;
                return;
            }
            routine = BinarySearch((BinaryTree)n);
            StartCoroutine(routine);
            break;

        case Algorithm.ShiftLinkedList:
            if (n.GetType() != typeof(LinkedList))
            {
                TempText.setTempText("Sorry, this type of node isn't valid for Shifting", 3);
                inAlgo = false;
                return;
            }
            routine = ShiftLinkedList((LinkedList)n);
            StartCoroutine(routine);
            break;

        default:
            Debug.LogError($"Something broke with selected algortithm {selectedAlgorithm}");
            break;
        }
    }