Example #1
0
    public override Dictionary <int, InstructionBase> UserTestInstructions(InstructionBase[] sortingElements)
    {
        Dictionary <int, InstructionBase> instructions = new Dictionary <int, InstructionBase>();
        int instNr = 0, i = 0, j = 0;

        // Add first instruction
        instructions.Add(instNr, new InstructionBase(Util.FIRST_INSTRUCTION, instNr++));

        int N = sortingElements.Length; // line 1

        for (i = 0; i < N; i++)
        {
            instructions.Add(instNr, new InstructionLoop(UtilSort.UPDATE_LOOP_INST, instNr++, i, j, UtilSort.OUTER_LOOP));
            for (j = 0; j < N - i - 1; j++)
            {
                // Update for loop instruction
                instructions.Add(instNr, new InstructionLoop(UtilSort.UPDATE_LOOP_INST, instNr++, i, j, UtilSort.INNER_LOOP));

                // Choose sorting elements to compare
                BubbleSortInstruction comparison = MakeInstruction(sortingElements[j], sortingElements[j + 1], i, j, UtilSort.COMPARE_START_INST, instNr, true, false);

                // Add this instruction
                instructions.Add(instNr++, comparison);

                if (comparison.Value1 > comparison.Value2)
                {
                    // Switch their positions
                    instructions.Add(instNr, MakeInstruction(sortingElements[j], sortingElements[j + 1], i, j, UtilSort.SWITCH_INST, instNr++, true, false));

                    int             holder1 = ((BubbleSortInstruction)sortingElements[j]).HolderID1;
                    int             holder2 = ((BubbleSortInstruction)sortingElements[j + 1]).HolderID1;
                    InstructionBase temp    = sortingElements[j];
                    sortingElements[j]     = sortingElements[j + 1];
                    sortingElements[j + 1] = temp;

                    // Update holderID
                    ((BubbleSortInstruction)sortingElements[j]).HolderID1     = holder1;
                    ((BubbleSortInstruction)sortingElements[j + 1]).HolderID1 = holder2;
                }
                // Add this instruction
                instructions.Add(instNr, MakeInstruction(sortingElements[j], sortingElements[j + 1], i, j, UtilSort.COMPARE_END_INST, instNr++, false, false));
            }
            if (instructions[instructions.Count - 1] is BubbleSortInstruction)
            {
                ((BubbleSortInstruction)instructions[instructions.Count - 1]).IsSorted = true;
            }

            // Update end 2nd for-loop instruction
            instructions.Add(instNr, new InstructionLoop(UtilSort.UPDATE_LOOP_INST, instNr++, i, j, UtilSort.INNER_LOOP));
            instructions.Add(instNr, new InstructionLoop(UtilSort.END_LOOP_INST, instNr++, i, j, UtilSort.INNER_LOOP));
        }
        instructions.Add(instNr, new InstructionLoop(UtilSort.UPDATE_LOOP_INST, instNr++, i, j, UtilSort.OUTER_LOOP));
        instructions.Add(instNr, new InstructionLoop(UtilSort.END_LOOP_INST, instNr++, i, j, UtilSort.OUTER_LOOP));

        // Final instruction
        instructions.Add(instNr, new InstructionBase(Util.FINAL_INSTRUCTION, instNr++));
        return(instructions);
    }
Example #2
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);
    }
Example #3
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);
    }
Example #4
0
 protected override void Awake()
 {
     base.Awake();
     Instruction = new BubbleSortInstruction(UtilSort.INIT_INSTRUCTION, 0, UtilSort.NO_VALUE, UtilSort.NO_VALUE, UtilSort.NO_VALUE, sortingElementID, sortingElementID, UtilSort.NO_VALUE, UtilSort.NO_VALUE, value, UtilSort.NO_VALUE, false, false); //new BubbleSortInstruction(sortingElementID, Util.NO_DESTINATION, sortingElementID, Util.NO_DESTINATION, value, Util.NO_DESTINATION, Util.NO_VALUE, Util.NO_VALUE, Util.INIT_INSTRUCTION, 0, false, false);
 }
Example #5
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--;
    }