Ejemplo n.º 1
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.º 2
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.º 3
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.º 4
0
    public override Dictionary <int, InstructionBase> UserTestInstructions(InstructionBase[] sortingElements)
    {
        // Instructions for user test + pseudo code
        int instNr = 0;
        Dictionary <int, InstructionBase> instructions = new Dictionary <int, InstructionBase>();
        InsertionSortInstruction          compareElement;

        instructions.Add(instNr, new InsertionSortInstruction(UtilSort.SET_SORTED_INST, instNr++, Util.NO_VALUE, Util.NO_VALUE, Util.NO_VALUE, 0, ((InsertionSortInstruction)sortingElements[0]).Value, false, true, false, 0, UtilSort.NO_DESTINATION));

        int i = 1; // Line 1

        // Add the first instruction which will be used for Pseudo code
        instructions.Add(instNr, new InstructionBase(Util.FIRST_INSTRUCTION, instNr++));

        while (i < sortingElements.Length)
        {
            // Line 2
            instructions.Add(instNr, new InstructionLoop(UtilSort.FIRST_LOOP, instNr++, i, Util.NO_VALUE, Util.NO_VALUE));

            // Line 3
            int j = i - 1;
            instructions.Add(instNr, new InstructionLoop(Util.SET_VAR_J, instNr++, i, j, Util.NO_VALUE));

            InsertionSortInstruction element = (InsertionSortInstruction)sortingElements[i];
            int temp1 = element.HolderID;
            // compare, sorted, pivot
            InsertionSortInstruction pivot = new InsertionSortInstruction(UtilSort.PIVOT_START_INST, instNr, i, j, Util.NO_VALUE, element.SortingElementID, element.Value, false, false, true, temp1, pivotHolder.HolderID);
            sortingElements[i] = pivot;

            // Line 4: Add this move (Pivot moved in pivot position)
            instructions.Add(instNr++, pivot);

            while (true)
            {
                InsertionSortInstruction element2 = (InsertionSortInstruction)sortingElements[j];

                // Line 5: Choose a new compare element
                compareElement = new InsertionSortInstruction(UtilSort.COMPARE_START_INST, instNr, i, j, Util.NO_VALUE, element2.SortingElementID, element2.Value, true, element2.IsSorted, false, j, UtilSort.NO_DESTINATION);

                sortingElements[j] = compareElement;
                instructions.Add(instNr++, compareElement);

                // Pivot larger than compare element, place compare element
                if (pivot.Value >= compareElement.Value)
                {
                    // Line 7
                    instructions.Add(instNr, new InsertionSortInstruction(UtilSort.COMPARE_END_INST, instNr++, i, j, Util.NO_VALUE, compareElement.SortingElementID, compareElement.Value, false, true, false, compareElement.HolderID, UtilSort.NO_DESTINATION));
                    break;
                }

                // Pivot is less than compare element, switch their spots
                int temp2 = compareElement.HolderID;
                sortingElements[j + 1] = compareElement;
                sortingElements[j]     = pivot;

                // Add this move (compare element switched to pivot/next position) // Line 6
                instructions.Add(instNr, new InsertionSortInstruction(UtilSort.SWITCH_INST, instNr++, i, j, Util.NO_VALUE, compareElement.SortingElementID, compareElement.Value, false, true, false, compareElement.HolderID, temp1));

                // Line 7
                instructions.Add(instNr, new InstructionLoop(Util.UPDATE_VAR_J, instNr++, i, j, Util.NO_VALUE));
                j -= 1;

                // temp2 is open spot, temp1 will be given to next compare element or place pivot there
                temp1 = temp2;

                if (j < 0)
                {
                    // Line 8
                    instructions.Add(instNr, new InstructionLoop(UtilSort.UPDATE_LOOP_INST, instNr++, i, j));
                    instructions.Add(instNr, new InsertionSortInstruction(UtilSort.COMPARE_END_INST, instNr++, i, j, Util.NO_VALUE, compareElement.SortingElementID, compareElement.Value, false, true, false, j + 2, UtilSort.NO_DESTINATION));
                    break;
                }
            }

            sortingElements[j + 1] = pivot;

            // Add this move (pivot sorted) // Line 9
            instructions.Add(instNr, new InsertionSortInstruction(UtilSort.PIVOT_END_INST, instNr++, i, j, Util.NO_VALUE, pivot.SortingElementID, pivot.Value, false, true, false, pivotHolder.HolderID, temp1));

            // Line 10
            instructions.Add(instNr, new InstructionLoop(Util.INCREMENT_VAR_I, instNr++, i, j, Util.NO_VALUE));
            i += 1;
        }

        // Line 2 (update loop before finishing)
        instructions.Add(instNr, new InstructionLoop(UtilSort.FIRST_LOOP, instNr++, i, Util.NO_VALUE, Util.NO_VALUE));

        // Add the final instruction which will be used for Pseudo code
        instructions.Add(instNr, new InstructionBase(Util.FINAL_INSTRUCTION, instNr++));
        return(instructions);
    }
Ejemplo n.º 5
0
 protected override void Awake()
 {
     base.Awake();
     Instruction = new InsertionSortInstruction(UtilSort.INIT_INSTRUCTION, 0, UtilSort.NO_VALUE, UtilSort.NO_VALUE, UtilSort.NO_VALUE, sortingElementID, value, false, false, false, sortingElementID, UtilSort.NO_DESTINATION); //new InsertionSortInstruction(sortingElementID, sortingElementID, Util.INIT_STATE, Util.NO_VALUE, Util.NO_VALUE, Util.INIT_INSTRUCTION, 0, value, false, false, false);
 }