Example #1
0
    protected override void UpdateSortingElementState()
    {
        if (bubbleSortInstruction != null)
        {
            // Debugging
            instruction = bubbleSortInstruction.Instruction;

            hID = bubbleSortInstruction.GetHolderFor(sortingElementID);
            if (bubbleSortInstruction.Instruction == UtilSort.SWITCH_INST)
            {
                nextHolderID = bubbleSortInstruction.SwitchToHolder(sortingElementID);
            }
            else
            {
                nextHolderID = bubbleSortInstruction.GetHolderFor(sortingElementID);
            }

            switch (instruction)
            {
            case UtilSort.INIT_INSTRUCTION: status = "Init pos"; break;

            case UtilSort.COMPARE_START_INST: status = "Comparing with " + bubbleSortInstruction.GetValueFor(sortingElementID, true); break;

            case UtilSort.COMPARE_END_INST: status = "Comparing stop"; break;

            case UtilSort.SWITCH_INST:
                status           = "Move to " + nextHolderID;
                intermediateMove = true;
                break;

            case UtilSort.EXECUTED_INST: status = UtilSort.EXECUTED_INST; break;

            default: Debug.Log("UpdateSortingElementState(): Add '" + instruction + "' case, or ignore"); break;
            }

            if (bubbleSortInstruction.IsCompare)
            {
                IsCompare = true;
            }
            else
            {
                IsCompare = false;
            }

            if (bubbleSortInstruction.IsElementSorted(sortingElementID))
            {
                IsSorted = true;
            }
            else
            {
                IsSorted = false;
            }
        }
    }
Example #2
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--;
    }