Ejemplo n.º 1
0
    private void OnCollisionExit(Collision collision)
    {
        InsertionSortElement wasHolding = collision.collider.GetComponent <InsertionSortElement>();

        if (wasHolding != null && isPivotHolder)
        {
            wasHolding.RigidBody.constraints = RigidbodyConstraints.None; // Unfreeze
        }
    }
Ejemplo n.º 2
0
    public override InstructionBase[] CopyFirstState(GameObject[] sortingElements)
    {
        InsertionSortInstruction[] elementStates = new InsertionSortInstruction[sortingElements.Length];

        for (int i = 0; i < sortingElements.Length; i++)
        {
            InsertionSortElement element = sortingElements[i].GetComponent <InsertionSortElement>();
            int  sortingElementID        = element.SortingElementID;
            int  holderID  = element.CurrentStandingOn.HolderID;
            int  value     = element.Value;
            bool isPivot   = element.IsPivot;
            bool isCompare = element.IsCompare;
            bool isSorted  = element.IsSorted;
            elementStates[i] = new InsertionSortInstruction(UtilSort.INIT_INSTRUCTION, 0, UtilSort.NO_VALUE, UtilSort.NO_VALUE, UtilSort.NO_VALUE, sortingElementID, value, isCompare, isSorted, isPivot, holderID, UtilSort.NO_DESTINATION); // new InsertionSortInstruction(sortingElementID, holderID, Util.NO_DESTINATION, Util.NO_VALUE, Util.NO_VALUE, Util.INIT_INSTRUCTION, 0, value, isPivot, isCompare, isSorted);
        }
        return(elementStates);
    }
Ejemplo n.º 3
0
    public override int PrepareNextInstruction(InstructionBase instruction)
    {
        Debug.Log(instruction.DebugInfo());

        bool gotSortingElement = !insertionSort.SkipDict[UtilSort.SKIP_NO_ELEMENT].Contains(instruction.Instruction);
        bool noDestination     = insertionSort.SkipDict[UtilSort.SKIP_NO_DESTINATION].Contains(instruction.Instruction);

        if (gotSortingElement)
        {
            InsertionSortInstruction insertionSortInstruction = (InsertionSortInstruction)instruction;
            // Get the Sorting element
            InsertionSortElement sortingElement = sortMain.ElementManager.GetSortingElement(insertionSortInstruction.SortingElementID).GetComponent <InsertionSortElement>();

            // Hands out the next instruction
            sortingElement.Instruction = insertionSortInstruction;

            // Give this sorting element permission to give feedback to progress to next intstruction
            switch (instruction.Instruction)
            {
            case UtilSort.PIVOT_START_INST:
            case UtilSort.COMPARE_START_INST:
            case UtilSort.PIVOT_END_INST:
            case UtilSort.SWITCH_INST:
                sortingElement.NextMove = true;
                break;
            }
            //if (instruction.Instruction == UtilSort.PIVOT_START_INST || instruction.Instruction == UtilSort.PIVOT_END_INST || instruction.Instruction == UtilSort.SWITCH_INST)
            //    sortingElement.NextMove = true;
        }

        // Display help on blackboard
        if (sortMain.SortSettings.Difficulty <= UtilSort.BEGINNER)
        {
            sortMain.WaitForSupportToComplete++;
            StartCoroutine(sortMain.GetTeachingAlgorithm().UserTestHighlightPseudoCode(instruction, gotSortingElement));
        }


        if (gotSortingElement && !noDestination)
        {
            return(0);
        }
        return(1);
    }
Ejemplo n.º 4
0
    public override IEnumerator UserTestHighlightPseudoCode(InstructionBase instruction, bool gotSortingElement)
    {
        // Gather information from instruction
        InsertionSortElement sortingElement = null;

        if (gotSortingElement)
        {
            sortingElement = sortMain.ElementManager.GetSortingElement(((InsertionSortInstruction)instruction).SortingElementID).GetComponent <InsertionSortElement>();
        }

        if (instruction is InstructionLoop)
        {
            i = ((InstructionLoop)instruction).I;
            j = ((InstructionLoop)instruction).J;
            // k = ((InstructionLoop)instruction).K;
        }


        // Remove highlight from previous instruction
        pseudoCodeViewer.ChangeColorOfText(prevHighlightedLineOfCode, Util.BLACKBOARD_TEXT_COLOR);

        // Gather part of code to highlight
        int lineOfCode = Util.NO_VALUE;

        useHighlightColor = Util.HIGHLIGHT_STANDARD_COLOR;
        switch (instruction.Instruction)
        {
        case UtilSort.SET_SORTED_INST:
            UtilSort.IndicateElement(sortingElement.gameObject);
            break;

        case Util.FIRST_INSTRUCTION:
            lineOfCode = FirstInstructionCodeLine();
            break;

        case UtilSort.FIRST_LOOP:
            lineOfCode = 2;
            i_str      = i.ToString();
            SetLengthOfList();
            useHighlightColor = UseConditionColor(i < lengthOfListInteger);
            break;

        case Util.SET_VAR_J:
            lineOfCode = 3;
            iMinus1    = (i - 1).ToString();
            break;

        case UtilSort.PIVOT_START_INST:
            lineOfCode        = 4;
            useHighlightColor = Util.HIGHLIGHT_USER_ACTION;

            PreparePseudocodeValue(sortingElement.Value, 1);
            UtilSort.IndicateElement(sortingElement.gameObject);
            break;

        case UtilSort.COMPARE_START_INST:
            lineOfCode        = 5;
            j_str             = j.ToString();
            useHighlightColor = Util.HIGHLIGHT_USER_ACTION;
            PreparePseudocodeValue(sortingElement.Value, 2);
            UtilSort.IndicateElement(sortingElement.gameObject);
            break;

        case UtilSort.UPDATE_LOOP_INST:     // Update 2nd while loop when j < 0
            lineOfCode        = 5;
            useHighlightColor = Util.HIGHLIGHT_CONDITION_NOT_FULFILLED;
            j_str             = j.ToString();
            element2Value     = "X";
            break;

        case UtilSort.SWITCH_INST:
            lineOfCode        = 6;
            jPlus1            = (j + 1).ToString();
            useHighlightColor = Util.HIGHLIGHT_USER_ACTION;
            break;

        case Util.UPDATE_VAR_J:
            lineOfCode = 7;
            jMinus1    = (j - 1).ToString();
            break;

        case UtilSort.COMPARE_END_INST:
            lineOfCode = 8;
            break;

        case UtilSort.PIVOT_END_INST:
            lineOfCode        = 9;
            jPlus1            = (j + 1).ToString();
            useHighlightColor = Util.HIGHLIGHT_USER_ACTION;
            break;

        case Util.INCREMENT_VAR_I:
            lineOfCode = 10;
            iPlus1     = (i + 1).ToString();
            break;

        case Util.FINAL_INSTRUCTION:
            lineOfCode = FinalInstructionCodeLine();
            break;
        }

        // Highlight part of code in pseudocode
        yield return(HighlightPseudoCode(CollectLine(lineOfCode), useHighlightColor));

        // Mark prev for next round
        prevHighlightedLineOfCode = lineOfCode;

        sortMain.WaitForSupportToComplete--;
    }
Ejemplo n.º 5
0
    public override IEnumerator ExecuteDemoInstruction(InstructionBase instruction, bool increment)
    {
        // Gather information from instruction
        InsertionSortInstruction insertionInstruction = null;
        InsertionSortElement     sortingElement       = null;

        if (instruction is InsertionSortInstruction)
        {
            insertionInstruction = (InsertionSortInstruction)instruction;

            // Change internal state of sorting element
            sortingElement = sortMain.ElementManager.GetSortingElement(insertionInstruction.SortingElementID).GetComponent <InsertionSortElement>();
        }

        if (instruction is InstructionLoop)
        {
            i = ((InstructionLoop)instruction).I;
            j = ((InstructionLoop)instruction).J;
            //k = ((InstructionLoop)instruction).K;
        }

        // Remove highlight from previous instruction
        pseudoCodeViewer.ChangeColorOfText(prevHighlightedLineOfCode, Util.BLACKBOARD_TEXT_COLOR);

        // Gather part of code to highlight
        int lineOfCode = Util.NO_VALUE;

        useHighlightColor = Util.HIGHLIGHT_STANDARD_COLOR;
        switch (instruction.Instruction)
        {
        case UtilSort.SET_SORTED_INST:
            if (increment)
            {
                sortingElement.IsSorted = insertionInstruction.IsSorted;
                UtilSort.IndicateElement(sortingElement.gameObject);
            }
            else
            {
                sortingElement.IsSorted = !insertionInstruction.IsSorted;
                UtilSort.IndicateElement(sortingElement.gameObject);
            }
            break;

        case Util.FIRST_INSTRUCTION:
            lineOfCode = FirstInstructionCodeLine();
            break;

        case UtilSort.FIRST_LOOP:
            lineOfCode = 2;

            if (increment)
            {
                i_str = i.ToString();
                SetLengthOfList();
                useHighlightColor = UseConditionColor(i < lengthOfListInteger);
            }
            else
            {
                if (i > 1)
                {
                    i_str = (i - 1).ToString();
                }
                else
                {
                    i_str        = "i";
                    lengthOfList = "len(list)";
                }
            }
            break;

        case Util.SET_VAR_J:
            lineOfCode = 3;

            if (increment)
            {
                iMinus1 = (i - 1).ToString();
            }
            else
            {
                if (i > 1)
                {
                    iMinus1 = (i - 2).ToString();
                }
                else
                {
                    iMinus1 = "i - 1";
                }
            }
            break;

        case UtilSort.PIVOT_START_INST:
            lineOfCode = 4;

            if (increment)
            {
                PreparePseudocodeValue(sortingElement.Value, 1);
                sortingElement.IsPivot = insertionInstruction.IsPivot;
            }
            else
            {
                element1Value          = "list[i]";
                sortingElement.IsPivot = !insertionInstruction.IsPivot;
            }
            UtilSort.IndicateElement(sortingElement.gameObject);
            break;

        case UtilSort.COMPARE_START_INST:
            lineOfCode = 5;
            if (increment)
            {
                PreparePseudocodeValue(sortingElement.Value, 2);
                sortingElement.IsCompare = insertionInstruction.IsCompare;
                sortingElement.IsSorted  = insertionInstruction.IsSorted;
                j_str             = j.ToString();
                useHighlightColor = UseConditionColor(j >= 0 && sortingElement.Value > value1);
            }
            else
            {
                element2Value            = "list[j]";
                sortingElement.IsCompare = !insertionInstruction.IsCompare;
                sortingElement.IsSorted  = !insertionInstruction.IsSorted;

                if (j > 0)
                {
                    j_str = j.ToString();
                }
                else
                {
                    j_str = "j";
                }
            }
            UtilSort.IndicateElement(sortingElement.gameObject);
            break;

        case UtilSort.UPDATE_LOOP_INST:     // Update 2nd while loop when j < 0
            lineOfCode = 5;

            if (increment)
            {
                j_str             = j.ToString();
                element2Value     = "X";
                useHighlightColor = Util.HIGHLIGHT_CONDITION_NOT_FULFILLED;
            }
            else
            {
                j_str         = "j";
                element2Value = "list[j]";
            }
            break;

        case UtilSort.SWITCH_INST:
            lineOfCode = 6;

            if (increment)
            {
                //PreparePseudocodeValue(sortingElement.Value, 2); // testing
                sortingElement.IsCompare = insertionInstruction.IsCompare;
                sortingElement.IsSorted  = insertionInstruction.IsSorted;
                jPlus1 = (j + 1).ToString();
            }
            else
            {
                element2Value            = "list[j]";
                jPlus1                   = "j + 1";
                sortingElement.IsCompare = !insertionInstruction.IsCompare;
            }
            break;

        case Util.UPDATE_VAR_J:
            lineOfCode = 7;

            if (increment)
            {
                jMinus1 = (j - 1).ToString();
            }
            else
            {
                if (i > 1)
                {
                    jMinus1 = j.ToString();
                }
                else
                {
                    jMinus1 = "j - 1";
                }
            }
            break;

        case UtilSort.COMPARE_END_INST:
            if (increment)
            {
                sortingElement.IsCompare = insertionInstruction.IsCompare;
                sortingElement.IsSorted  = insertionInstruction.IsSorted;
            }
            else
            {
                sortingElement.IsCompare = !insertionInstruction.IsCompare;
                sortingElement.IsSorted  = !insertionInstruction.IsSorted;
            }

            UtilSort.IndicateElement(sortingElement.gameObject);
            lineOfCode = 8;
            break;

        case UtilSort.PIVOT_END_INST:
            lineOfCode = 9;

            if (increment)
            {
                sortingElement.IsPivot  = insertionInstruction.IsPivot;
                sortingElement.IsSorted = insertionInstruction.IsSorted;
                jPlus1 = (j + 1).ToString();
            }
            else
            {
                PreparePseudocodeValue(sortingElement.Value, 1);
                sortingElement.IsPivot  = !insertionInstruction.IsPivot;
                sortingElement.IsSorted = !insertionInstruction.IsSorted;
                jPlus1 = "j + 1";
            }
            UtilSort.IndicateElement(sortingElement.gameObject);
            break;

        case Util.INCREMENT_VAR_I:
            lineOfCode = 10;

            if (increment)
            {
                iPlus1 = (i + 1).ToString();
            }
            else
            {
                if (i > 1)
                {
                    iPlus1 = i.ToString();
                }
                else
                {
                    iPlus1 = "i + 1";
                }
            }
            break;

        case Util.FINAL_INSTRUCTION:
            lineOfCode      = FinalInstructionCodeLine();
            IsTaskCompleted = increment;
            break;
        }

        // Highlight part of code in pseudocode
        yield return(HighlightPseudoCode(CollectLine(lineOfCode), useHighlightColor));

        // Mark prev for next round
        prevHighlightedLineOfCode = lineOfCode;

        // Move sorting element
        if (instruction is InsertionSortInstruction)
        {
            switch (insertionInstruction.Instruction)
            {
            case UtilSort.COMPARE_START_INST:     // testing
                if (increment)
                {
                    // Positioning the pivot holder behind/slightly above comparing element
                    pivotHolder.transform.position = new Vector3(insertionSortManager.GetCorrectHolder(sortingElement.CurrentStandingOn.HolderID).transform.position.x, pivotHolder.transform.position.y, pivotHolder.transform.position.z);

                    // Postitioning the pivot element on top of the pivot holder
                    SortingElementBase pivotElement = pivotHolder.CurrentHolding;
                    pivotElement.transform.position = pivotHolder.transform.position + UtilSort.ABOVE_HOLDER_VR;
                    pivotElement.transform.rotation = Quaternion.identity;
                }
                else
                {
                    //sortingElement.transform.position = insertionSortManager.GetCorrectHolder(insertionInstruction.HolderID).transform.position + Util.ABOVE_HOLDER_VR;
                }
                break;

            case UtilSort.PIVOT_START_INST:     // tesing (was combined with switch/pivot_end
                if (increment)
                {
                    pivotHolder.transform.position    = new Vector3(insertionSortManager.GetCorrectHolder(sortingElement.CurrentStandingOn.HolderID).transform.position.x, pivotHolder.transform.position.y, pivotHolder.transform.position.z);
                    sortingElement.transform.position = pivotHolder.transform.position + UtilSort.ABOVE_HOLDER_VR;
                    sortingElement.transform.rotation = Quaternion.identity;
                }
                else
                {
                    //
                    sortingElement.transform.position = insertionSortManager.GetCorrectHolder(insertionInstruction.HolderID).transform.position + UtilSort.ABOVE_HOLDER_VR;
                    sortingElement.transform.rotation = Quaternion.identity;
                }
                break;

            //case Util.PIVOT_START_INST: // original working setup (non moving pivot holder)
            case UtilSort.SWITCH_INST:
            case UtilSort.PIVOT_END_INST:
                if (increment)
                {
                    sortingElement.transform.position = insertionSortManager.GetCorrectHolder(insertionInstruction.NextHolderID).transform.position + UtilSort.ABOVE_HOLDER_VR;
                    sortingElement.transform.rotation = Quaternion.identity;
                }
                else
                {
                    sortingElement.transform.position = insertionSortManager.GetCorrectHolder(insertionInstruction.HolderID).transform.position + UtilSort.ABOVE_HOLDER_VR;
                    sortingElement.transform.rotation = Quaternion.identity;
                }
                break;
            }
        }
        sortMain.WaitForSupportToComplete--;
    }
Ejemplo n.º 6
0
    public override IEnumerator Demo(GameObject[] list)
    {
        Vector3 temp = new Vector3();

        // Set first element as sorted
        list[0].GetComponent <SortingElementBase>().IsSorted = true;
        UtilSort.IndicateElement(list[0]);
        yield return(demoStepDuration);

        int listLength = list.Length;

        lengthOfList = listLength.ToString(); // Pseudocode

        i = 1;
        // Display pseudocode (set i)
        yield return(HighlightPseudoCode(CollectLine(1), Util.HIGHLIGHT_STANDARD_COLOR));

        while (i < listLength)
        {
            // Check if user wants to stop the demo
            if (sortMain.UserStoppedTask)
            {
                break;
            }

            // Display pseudocode (1st while)
            yield return(HighlightPseudoCode(CollectLine(2), Util.HIGHLIGHT_STANDARD_COLOR));

            // Check if user wants to stop the demo
            if (sortMain.UserStoppedTask)
            {
                break;
            }

            // Get index of first element to the left of the pivot and compare
            j = i - 1;

            // Display pseudocode (set j)
            yield return(HighlightPseudoCode(CollectLine(3), Util.HIGHLIGHT_STANDARD_COLOR));

            // Check if user wants to stop the demo
            if (sortMain.UserStoppedTask)
            {
                break;
            }

            // Get pivot
            GameObject           pivotObj = list[i];
            InsertionSortElement pivot    = list[i].GetComponent <InsertionSortElement>();
            pivot.IsPivot = true;

            // Get pivot's initial position
            temp = pivot.transform.position;

            // Place pivot holder above the pivot element
            pivotHolder.transform.position = temp + pivotHolderPos;

            // Place the pivot on top of the pivot holder
            pivot.transform.position = temp + tutorialPivotElementHeight;

            // Set first values here to display on blackboard
            PreparePseudocodeValue(pivot.Value, 1);
            PreparePseudocodeValue(list[j].GetComponent <InsertionSortElement>().Value, 2);

            // Display pseudocode (set pivot)
            yield return(HighlightPseudoCode(CollectLine(4), Util.HIGHLIGHT_STANDARD_COLOR));

            // Check if user wants to stop the demo
            if (sortMain.UserStoppedTask)
            {
                break;
            }

            // Start comparing until find the correct position is found
            // Prepare the element to compare with
            GameObject           compareObj = list[j];
            InsertionSortElement compare    = compareObj.GetComponent <InsertionSortElement>();
            compare.IsCompare = true;
            UtilSort.IndicateElement(compare.gameObject);

            // Display pseudocode (2nd while)
            yield return(HighlightPseudoCode(CollectLine(5), Util.HIGHLIGHT_STANDARD_COLOR));

            // Check if user wants to stop the demo
            if (sortMain.UserStoppedTask)
            {
                break;
            }

            compare.IsCompare = false;
            compare.IsSorted  = true;

            while (value1 < value2)
            {
                // Check if user wants to stop the demo
                if (sortMain.UserStoppedTask)
                {
                    break;
                }

                // Pivot is smaller, start moving compare element
                // Compare element's position
                Vector3 temp2 = compare.transform.position;

                // Moving other element one index to the right
                compare.transform.position = temp;
                // Updating list
                list[j + 1] = compareObj;

                // Display pseudocode (move compare element)
                yield return(HighlightPseudoCode(CollectLine(6), Util.HIGHLIGHT_STANDARD_COLOR));

                // Check if user wants to stop the demo
                if (sortMain.UserStoppedTask)
                {
                    break;
                }

                // Preparing for next step
                temp = temp2;

                // Display pseudocode (update j)
                yield return(HighlightPseudoCode(CollectLine(7), Util.HIGHLIGHT_STANDARD_COLOR));

                j -= 1;


                // Check if user wants to stop the demo
                if (sortMain.UserStoppedTask)
                {
                    break;
                }

                // Move pivot out and place it ontop of pivot holder (above holder it check whether it's put the element)
                pivotHolder.transform.position = temp + pivotHolderPos;
                pivot.transform.position       = temp + tutorialPivotElementHeight;

                // Wait to show the pivot being moved
                yield return(demoStepDuration);

                // Check if user wants to stop the demo
                if (sortMain.UserStoppedTask)
                {
                    break;
                }

                // Check if there are more elements to compare the pivot with
                if (j >= 0)
                {
                    // Start comparing until find the correct position is found
                    // Prepare the element to compare with
                    compareObj        = list[j];
                    compare           = compareObj.GetComponent <InsertionSortElement>();
                    compare.IsCompare = true;
                    PreparePseudocodeValue(compare.Value, 2);
                    UtilSort.IndicateElement(compare.gameObject);

                    // Display pseudocode (2nd while new compare value)
                    yield return(HighlightPseudoCode(CollectLine(5), Util.HIGHLIGHT_STANDARD_COLOR));

                    // Check if user wants to stop the demo
                    if (sortMain.UserStoppedTask)
                    {
                        break;
                    }

                    compare.IsCompare = false;
                    compare.IsSorted  = true;

                    if (value1 >= value2)
                    {
                        UtilSort.IndicateElement(compare.gameObject);
                        // Display pseudocode (end 2nd while)
                        yield return(HighlightPseudoCode(CollectLine(8), Util.HIGHLIGHT_STANDARD_COLOR));

                        // Check if user wants to stop the demo
                        if (sortMain.UserStoppedTask)
                        {
                            break;
                        }
                    }
                }
                else
                {
                    // Display pseudocode (end 2nd while)
                    yield return(HighlightPseudoCode(CollectLine(8), Util.HIGHLIGHT_STANDARD_COLOR));

                    break;
                }
            }
            // Check if user wants to stop the demo
            if (sortMain.UserStoppedTask)
            {
                break;
            }

            if (i == 1 && value1 >= value2)
            {
                compare.CurrentStandingOn.CurrentColor = UtilSort.SORTED_COLOR;
            }

            // Finish off the pivots work
            pivot.IsSorted           = true;
            pivot.IsPivot            = false;
            pivot.transform.position = temp;

            // Put pivot object back into the list
            list[j + 1] = pivotObj;
            yield return(HighlightPseudoCode(CollectLine(9), Util.HIGHLIGHT_STANDARD_COLOR));

            // Check if user wants to stop the demo
            if (sortMain.UserStoppedTask)
            {
                break;
            }

            // Display pseudocode (increment i)
            yield return(HighlightPseudoCode(CollectLine(10), Util.HIGHLIGHT_STANDARD_COLOR));

            i += 1;

            // Check if user wants to stop the demo
            if (sortMain.UserStoppedTask)
            {
                break;
            }
        }
        // Display pseudocode (end 1st while)
        yield return(HighlightPseudoCode(CollectLine(11), Util.HIGHLIGHT_STANDARD_COLOR));

        // Mark the last element sorted
        InsertionSortElement lastElement = null;

        if (list[list.Length - 1] != null)
        {
            lastElement = list[list.Length - 1].GetComponent <InsertionSortElement>();
        }

        if (lastElement != null)
        {
            lastElement.IsSorted = true;
        }

        // Finished off; remove pivot holder
        PivotHolderVisible(false);

        if (sortMain.UserStoppedTask)
        {
            sortMain.UpdateCheckList(Util.DEMO, true);
        }
        else
        {
            isTaskCompleted = true;
        }
    }