예제 #1
0
    public override InstructionBase[] CopyFirstState(GameObject[] sortingElements)
    {
        BubbleSortInstruction[] elementStates = new BubbleSortInstruction[sortingElements.Length];

        for (int i = 0; i < sortingElements.Length; i++)
        {
            BubbleSortElement element = sortingElements[i].GetComponent <BubbleSortElement>();
            int  sortingElementID     = element.SortingElementID;
            int  holderID             = element.CurrentStandingOn.HolderID;
            int  value     = element.Value;
            bool isCompare = element.IsCompare;
            bool isSorted  = element.IsSorted;
            elementStates[i] = new BubbleSortInstruction(UtilSort.INIT_INSTRUCTION, 0, UtilSort.NO_VALUE, UtilSort.NO_VALUE, UtilSort.NO_VALUE, sortingElementID, UtilSort.NO_VALUE, holderID, UtilSort.NO_VALUE, value, UtilSort.NO_VALUE, false, false);
// new BubbleSortInstruction(sortingElementID, Util.NO_DESTINATION, holderID, Util.NO_DESTINATION, value, Util.NO_VALUE, Util.NO_VALUE, Util.NO_VALUE, Util.INIT_INSTRUCTION, 0, isCompare, isSorted);
        }
        return(elementStates);
    }
예제 #2
0
    public override int PrepareNextInstruction(InstructionBase instruction)
    {
        Debug.Log(instruction.DebugInfo());

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

        if (gotSortingElement)
        {
            // Get the next two instructions
            BubbleSortInstruction bubbleInstruction = (BubbleSortInstruction)instruction;

            // Find the sorting elements for this instruction
            BubbleSortElement s1 = sortMain.ElementManager.GetSortingElement(bubbleInstruction.SortingElementID1).GetComponent <BubbleSortElement>();
            BubbleSortElement s2 = sortMain.ElementManager.GetSortingElement(bubbleInstruction.SortingElementID2).GetComponent <BubbleSortElement>();

            // Hand the instructions out
            s1.Instruction = bubbleInstruction;
            s2.Instruction = bubbleInstruction;

            // Give this sorting element permission to give feedback to progress to next intstruction
            if (instruction.Instruction == UtilSort.COMPARE_START_INST || instruction.Instruction == UtilSort.SWITCH_INST)
            {
                s1.NextMove = true;
                s2.NextMove = true;
            }
        }

        if (sortMain.SortSettings.Difficulty <= Util.BEGINNER)
        {
            sortMain.WaitForSupportToComplete++;
            StartCoroutine(sortMain.GetTeachingAlgorithm().UserTestHighlightPseudoCode(instruction, gotSortingElement));
        }

        if (gotSortingElement && !noDestination)
        {
            return(0);
        }
        return(2);
    }
예제 #3
0
    public override IEnumerator ExecuteDemoInstruction(InstructionBase instruction, bool increment)
    {
        // Gather information from instruction
        BubbleSortInstruction bubbleInstruction = null;
        BubbleSortElement     se1 = null, se2 = null;

        if (instruction is BubbleSortInstruction)
        {
            bubbleInstruction = (BubbleSortInstruction)instruction;

            // Change internal state of following sorting elements
            if (instruction.Instruction != UtilSort.UPDATE_LOOP_INST && instruction.Instruction != UtilSort.END_LOOP_INST) // no need to check for this anymore?
            {
                //Debug.Log("Inst. debug: " + bubbleInstruction.DebugInfo());
                //Debug.Log("SortmMain         : " + (sortMain != null));
                //Debug.Log("ElementManager   : " + (sortMain.ElementManager != null));
                //Debug.Log("Element 1: " + bubbleInstruction.SortingElementID1 + "; Element 2: " + bubbleInstruction.SortingElementID2);
                //Debug.Log("Element 1        : " + (sortMain.ElementManager.GetSortingElement(bubbleInstruction.SortingElementID1) != null));
                //Debug.Log("Element 2        : " + (sortMain.ElementManager.GetSortingElement(bubbleInstruction.SortingElementID2) != null));
                //Debug.Log("Element 1        : " + (sortMain.ElementManager.GetSortingElement(bubbleInstruction.SortingElementID1).GetComponent<BubbleSortElement>() != null));
                //Debug.Log("Element 2        : " + (sortMain.ElementManager.GetSortingElement(bubbleInstruction.SortingElementID2).GetComponent<BubbleSortElement>() != null));        // Nullpointer solved: static element number not resetted (Remember to reset on destroy)

                se1 = sortMain.ElementManager.GetSortingElement(bubbleInstruction.SortingElementID1).GetComponent <BubbleSortElement>();
                se2 = sortMain.ElementManager.GetSortingElement(bubbleInstruction.SortingElementID2).GetComponent <BubbleSortElement>();
            }
        }

        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 Util.FIRST_INSTRUCTION:
            lineOfCode = FirstInstructionCodeLine();
            if (increment)
            {
                SetLengthOfList();
            }
            else
            {
                lengthOfList = "len(list)";
            }
            break;

        case UtilSort.UPDATE_LOOP_INST:
            if (k == UtilSort.OUTER_LOOP)
            {
                lineOfCode = 2;
                if (increment)
                {
                    n = lengthOfList;
                    useHighlightColor = UseConditionColor(i != lengthOfListInteger);
                }
                else
                {
                    if (i == 0)
                    {
                        n = "n";
                    }
                    else
                    {
                        i -= 1;
                    }
                }
            }
            else if (k == UtilSort.INNER_LOOP)
            {
                lineOfCode = 3;
                if (increment)
                {
                    numberOfElementsMinusI1 = (lengthOfListInteger - i - 1).ToString();
                    useHighlightColor       = UseConditionColor(j != lengthOfListInteger - i - 1);
                }
                else
                {
                    if (i == 0 && j == 0)
                    {
                        numberOfElementsMinusI1 = "n-i-1";
                    }
                    else if (j == 0)
                    {
                        j = lengthOfListInteger - i;
                        numberOfElementsMinusI1 = j.ToString();
                    }
                    else
                    {
                        j -= 1;
                    }
                }
            }
            break;

        case UtilSort.COMPARE_START_INST:
            lineOfCode = 4;
            PreparePseudocodeValue(se1.Value, 1);
            PreparePseudocodeValue(se2.Value, 2);

            if (increment)
            {
                se1.IsCompare     = bubbleInstruction.IsCompare;
                se2.IsCompare     = bubbleInstruction.IsCompare;
                useHighlightColor = UseConditionColor(se1.Value > se2.Value);
            }
            else
            {
                se1.IsCompare = !bubbleInstruction.IsCompare;
                se2.IsCompare = !bubbleInstruction.IsCompare;

                if (bubbleInstruction.IsElementSorted(se1.SortingElementID) != se1.IsSorted)
                {
                    se1.IsSorted = bubbleInstruction.IsElementSorted(se1.SortingElementID);
                }

                if (bubbleInstruction.IsElementSorted(se2.SortingElementID) != se2.IsSorted)
                {
                    se2.IsSorted = bubbleInstruction.IsElementSorted(se2.SortingElementID);
                }
            }
            UtilSort.IndicateElement(se1.gameObject);
            UtilSort.IndicateElement(se2.gameObject);
            break;

        case UtilSort.SWITCH_INST:
            lineOfCode = 5;

            if (increment)
            {
                PreparePseudocodeValue(se1.Value, 1);
                PreparePseudocodeValue(se2.Value, 2);
            }
            else
            {
                element1Value = "list[j]";
                element2Value = "list[j + 1]";
            }
            break;

        case UtilSort.COMPARE_END_INST:
            lineOfCode = 6;

            if (increment)
            {
                se1.IsCompare = bubbleInstruction.IsCompare;
                se2.IsCompare = bubbleInstruction.IsCompare;
                se1.IsSorted  = bubbleInstruction.IsElementSorted(se1.SortingElementID);
                se2.IsSorted  = bubbleInstruction.IsElementSorted(se2.SortingElementID);
            }
            else
            {
                se1.IsCompare = !bubbleInstruction.IsCompare;
                se2.IsCompare = !bubbleInstruction.IsCompare;
                if (bubbleInstruction.IsElementSorted(se1.SortingElementID) != se1.IsSorted)
                {
                    se1.IsSorted = bubbleInstruction.IsElementSorted(se1.SortingElementID);
                }

                if (bubbleInstruction.IsElementSorted(se2.SortingElementID) != se2.IsSorted)
                {
                    se2.IsSorted = bubbleInstruction.IsElementSorted(se2.SortingElementID);
                }
            }
            UtilSort.IndicateElement(se1.gameObject);
            UtilSort.IndicateElement(se2.gameObject);
            break;

        case UtilSort.END_LOOP_INST:
            if (k == UtilSort.OUTER_LOOP)
            {
                lineOfCode = 8;
            }
            else if (k == UtilSort.INNER_LOOP)
            {
                lineOfCode = 7;
            }
            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 BubbleSortInstruction)
        {
            switch (instruction.Instruction)
            {
            case UtilSort.SWITCH_INST:
                if (increment)
                {
                    se1.transform.position = sortMain.AlgorithmManagerBase.GetCorrectHolder(bubbleInstruction.HolderID2).transform.position + UtilSort.ABOVE_HOLDER_VR;
                    se1.transform.rotation = Quaternion.identity;
                    se2.transform.position = sortMain.AlgorithmManagerBase.GetCorrectHolder(bubbleInstruction.HolderID1).transform.position + UtilSort.ABOVE_HOLDER_VR;
                    se2.transform.rotation = Quaternion.identity;
                }
                else
                {
                    se1.transform.position = sortMain.AlgorithmManagerBase.GetCorrectHolder(bubbleInstruction.HolderID1).transform.position + UtilSort.ABOVE_HOLDER_VR;
                    se1.transform.rotation = Quaternion.identity;
                    se2.transform.position = sortMain.AlgorithmManagerBase.GetCorrectHolder(bubbleInstruction.HolderID2).transform.position + UtilSort.ABOVE_HOLDER_VR;
                    se2.transform.rotation = Quaternion.identity;
                }
                break;
            }
        }

        sortMain.WaitForSupportToComplete--;
    }
예제 #4
0
    public override IEnumerator UserTestHighlightPseudoCode(InstructionBase instruction, bool gotSortingElement)
    {
        // Gather information from instruction
        BubbleSortElement se1 = null, se2 = null;

        if (gotSortingElement)
        {
            se1 = sortMain.ElementManager.GetSortingElement(((BubbleSortInstruction)instruction).SortingElementID1).GetComponent <BubbleSortElement>();
            se2 = sortMain.ElementManager.GetSortingElement(((BubbleSortInstruction)instruction).SortingElementID2).GetComponent <BubbleSortElement>();
        }

        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 Util.FIRST_INSTRUCTION:
            lineOfCode = FirstInstructionCodeLine();
            SetLengthOfList();
            n = lengthOfList;
            break;

        case UtilSort.UPDATE_LOOP_INST:
            if (k == UtilSort.OUTER_LOOP)
            {
                lineOfCode        = 2;
                useHighlightColor = UseConditionColor(i != lengthOfListInteger);
            }
            else if (k == UtilSort.INNER_LOOP)
            {
                lineOfCode = 3;
                numberOfElementsMinusI1 = (lengthOfListInteger - i - 1).ToString();
                useHighlightColor       = UseConditionColor(j != lengthOfListInteger - i - 1);
            }
            break;

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

            PreparePseudocodeValue(se1.Value, 1);
            PreparePseudocodeValue(se2.Value, 2);

            UtilSort.IndicateElement(se1.gameObject);
            UtilSort.IndicateElement(se2.gameObject);
            break;

        case UtilSort.SWITCH_INST:
            lineOfCode        = 5;
            useHighlightColor = Util.HIGHLIGHT_USER_ACTION;
            break;

        case UtilSort.COMPARE_END_INST:
            lineOfCode = 6;
            break;

        case UtilSort.END_LOOP_INST:
            if (k == UtilSort.OUTER_LOOP)
            {
                lineOfCode = 8;
            }
            else if (k == UtilSort.INNER_LOOP)
            {
                lineOfCode = 7;
            }
            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--;
    }
예제 #5
0
    public override IEnumerator Demo(GameObject[] list)
    {
        int N = list.Length;

        i = 0;
        j = 0;

        // Display pseudocode line 1: n = len(list)
        yield return(HighlightPseudoCode(CollectLine(1), Util.HIGHLIGHT_STANDARD_COLOR));

        for (i = 0; i < N; i++)
        {
            // Check if user wants to stop the demo
            if (sortMain.UserStoppedTask)
            {
                break;
            }

            // Display outer loop
            yield return(HighlightPseudoCode(CollectLine(2), Util.HIGHLIGHT_STANDARD_COLOR));

            for (j = 0; j < N - i - 1; j++)
            {
                // Check if user wants to stop the demo
                if (sortMain.UserStoppedTask)
                {
                    break;
                }

                // Display pseudocode (update for-loops)
                yield return(HighlightPseudoCode(CollectLine(3), Util.HIGHLIGHT_STANDARD_COLOR));

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

                // Choose sorting elements to compare
                BubbleSortElement p1 = list[j].GetComponent <BubbleSortElement>();
                BubbleSortElement p2 = list[j + 1].GetComponent <BubbleSortElement>();

                // Change status
                if (p1 != null)
                {
                    p1.IsCompare = true;
                }
                if (p2 != null)
                {
                    p2.IsCompare = true;
                }

                // Update color on holders
                UtilSort.IndicateElement(p1.gameObject);
                UtilSort.IndicateElement(p2.gameObject);

                // Get their values
                if (p1 != null)
                {
                    PreparePseudocodeValue(p1.Value, 1);
                }
                if (p2 != null)
                {
                    PreparePseudocodeValue(p2.Value, 2);
                }

                // Display pseudocode (list length)
                yield return(HighlightPseudoCode(CollectLine(4), Util.HIGHLIGHT_STANDARD_COLOR));

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

                if (value1 > value2)
                {
                    // Switch their positions
                    GameObject temp  = list[j];
                    GameObject temp2 = list[j + 1];

                    // Switch their positions (added some stuff such that the elements get back in position)
                    Vector3 pos1 = list[j].GetComponent <SortingElementBase>().PlacementAboveHolder, pos2 = list[j + 1].GetComponent <SortingElementBase>().PlacementAboveHolder;
                    p1.transform.position = pos2;
                    list[j] = temp2;
                    p2.transform.position = pos1;
                    list[j + 1]           = temp;
                    UtilSort.ResetRotation(temp);
                    UtilSort.ResetRotation(temp2);

                    // Display pseudocode (swap)
                    yield return(HighlightPseudoCode(CollectLine(5), Util.HIGHLIGHT_STANDARD_COLOR));

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

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

                p1.IsCompare = false;
                if (p1.CurrentStandingOn != null)
                {
                    p1.CurrentStandingOn.CurrentColor = UtilSort.STANDARD_COLOR;
                }
                p2.IsCompare = false;
                if (p2.CurrentStandingOn != null)
                {
                    p2.CurrentStandingOn.CurrentColor = UtilSort.STANDARD_COLOR;
                }
            }
            // Display pseudocode (end 2nd for-loop)
            yield return(HighlightPseudoCode(CollectLine(7), Util.HIGHLIGHT_STANDARD_COLOR));

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

            // Biggest element moved to the last* index, mark it as sorted
            if (list[N - i - 1] != null)
            {
                BubbleSortElement lastElement = list[N - i - 1].GetComponent <BubbleSortElement>();
                if (lastElement != null)
                {
                    lastElement.IsSorted = true;
                }
            }

            UtilSort.IndicateElement(list[N - i - 1]); //list[N - i - 1].transform.position += Util.ABOVE_HOLDER_VR;
            yield return(demoStepDuration);
        }

        // Display pseudocode (end 1st for-loop)
        yield return(HighlightPseudoCode(CollectLine(8), Util.HIGHLIGHT_STANDARD_COLOR));

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