Пример #1
0
    private void OnTriggerEnter(Collider other)
    {
        if (other.tag == UtilSort.SORTING_ELEMENT_TAG)
        {
            BucketSortElement sortingElement = other.GetComponent <BucketSortElement>();

            // Check for bug(same sorting element got added twice)
            //if (prevSortingElementID == sortingElement.SortingElementID)
            //    return;

            if (enterTrigger.enabled)
            {
                if (parent.SortSettings.IsDemo())
                {
                    if (ValidateSortingElement(sortingElement)) // !displayElements &&
                    {
                        // Do animation (color -> green -> color)
                        AddSortingElementToBucket(sortingElement);
                        prevSortingElementID = sortingElement.SortingElementID;
                    }
                    else
                    {
                        // Can't be put into this bucket
                        StartCoroutine(Animation(ERROR, 2));
                    }
                }
                else // User test
                {
                    if (sortingElement.Instruction is BucketSortInstruction)
                    {
                        BucketSortInstruction inst = (BucketSortInstruction)sortingElement.Instruction;

                        if (instruction == sortingElement.Instruction && instruction.Instruction == UtilSort.MOVE_TO_BUCKET_INST) // inst.Instruction == UtilSort.MOVE_TO_BUCKET_INST && inst.BucketID == bucketID
                        {
                            AddSortingElementToBucket(sortingElement);
                            prevSortingElementID = sortingElement.SortingElementID;

                            // Score
                            parent.GetComponent <UserTestManager>().IncrementTotalCorrect();

                            // Progress user test
                            parent.GetComponent <UserTestManager>().ReadyForNext += 1;
                        }
                        else if (ValidateSortingElement(sortingElement))
                        {
                        }
                        else
                        {
                            // Can't be put into this bucket
                            StartCoroutine(Animation(ERROR, 2));
                            parent.GetComponent <UserTestManager>().Mistake();
                        }
                    }
                }
            }
            else if (onTopOfBucketTrigger.enabled)
            {
            }
        }
    }
Пример #2
0
    public override InstructionBase[] CopyFirstState(GameObject[] sortingElements)
    {
        BucketSortInstruction[] elementStates = new BucketSortInstruction[sortingElements.Length];

        for (int i = 0; i < sortingElements.Length; i++)
        {
            BucketSortElement element = sortingElements[i].GetComponent <BucketSortElement>();
            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 BucketSortInstruction(Util.INIT_INSTRUCTION, 0, Util.NO_VALUE, Util.NO_VALUE, Util.NO_VALUE, sortingElementID, value, isCompare, isSorted, holderID, UtilSort.NO_DESTINATION, Util.NO_VALUE);
        }
        return(elementStates);
    }
Пример #3
0
    public override Dictionary <int, InstructionBase> UserTestInstructions(InstructionBase[] sortingElements)
    {
        Dictionary <int, InstructionBase> instructions = new Dictionary <int, InstructionBase>();
        int instNr = 0;

        // Line 0 (set parameter)
        instructions.Add(instNr, new InstructionBase(Util.FIRST_INSTRUCTION, instNr++));

        // Create buckets
        Vector3[] pos = new Vector3[1] {
            bucketManager.FirstBucketPosition
        };
        int numberOfBuckets = bucketSortManager.NumberOfBuckets;

        bucketManager.CreateObjects(numberOfBuckets, pos);

        // Line 1 (Create buckets)
        instructions.Add(instNr, new InstructionBase(UtilSort.CREATE_BUCKETS_INST, instNr++));

        int x;

        // Add elements to buckets
        for (x = 0; x < sortingElements.Length; x++)
        {
            // Line 2 (Update for-loop)
            instructions.Add(instNr, new InstructionLoop(UtilSort.FIRST_LOOP, instNr++, x, Util.NO_VALUE, Util.NO_VALUE)); // TODO: create one unique instruction for each loop

            // Get element
            BucketSortInstruction element = (BucketSortInstruction)sortingElements[x];
            int bucketIndex = BucketIndex(element.Value, numberOfBuckets, maxValue);

            // Line 3 (Display bucket index)
            instructions.Add(instNr, new BucketSortInstruction(UtilSort.BUCKET_INDEX_INST, instNr++, x, Util.NO_VALUE, Util.NO_VALUE, element.SortingElementID, element.Value, true, false, element.HolderID, UtilSort.NO_DESTINATION, bucketIndex));

            // Line 4 (Put element into bucket)
            instructions.Add(instNr, new BucketSortInstruction(UtilSort.MOVE_TO_BUCKET_INST, instNr++, x, Util.NO_VALUE, Util.NO_VALUE, element.SortingElementID, element.Value, false, false, element.HolderID, UtilSort.NO_DESTINATION, bucketIndex));
        }
        // Line 2: condition
        instructions.Add(instNr, new InstructionLoop(UtilSort.FIRST_LOOP, instNr++, x, Util.NO_VALUE, Util.NO_VALUE));

        // Line 5 (end for-loop)
        instructions.Add(instNr, new InstructionLoop(UtilSort.END_LOOP_INST, instNr++, Util.NO_VALUE, Util.NO_VALUE, Util.NO_VALUE, UtilSort.OUTER_LOOP));

        // Line 6 (make the buckets sort what they hold)
        instructions.Add(instNr, new InstructionBase(UtilSort.PHASING_INST, instNr++));

        // Sorting elements
        int[] values = new int[sortingElements.Length];
        for (int y = 0; y < sortingElements.Length; y++)
        {
            values[y] = ((BucketSortInstruction)sortingElements[y]).Value;
        }
        int[] sorted = InsertionSort.InsertionSortFixCase(values, false);

        // Creating fictionary buckets
        Dictionary <int, List <BucketSortInstruction> > buckets = new Dictionary <int, List <BucketSortInstruction> >();

        for (int y = 0; y < numberOfBuckets; y++)
        {
            buckets[y] = new List <BucketSortInstruction>();
        }

        // Look for correct value and add element to bucket
        for (int r = 0; r < sorted.Length; r++)
        {
            for (int s = 0; s < sortingElements.Length; s++)
            {
                BucketSortInstruction t = (BucketSortInstruction)sortingElements[s];
                if (sorted[r] == t.Value)
                {
                    int bucketIndex = BucketIndex(t.Value, bucketSortManager.NumberOfBuckets, maxValue);
                    BucketSortInstruction displayInstruction = new BucketSortInstruction(UtilSort.DISPLAY_ELEMENT, instNr, Util.NO_VALUE, Util.NO_VALUE, Util.NO_VALUE, t.SortingElementID, t.Value, false, true, Util.NO_VALUE, UtilSort.NO_DESTINATION, bucketIndex);
                    instructions.Add(instNr++, displayInstruction);
                    buckets[bucketIndex].Add(displayInstruction);
                    break;
                }
            }
        }

        int i, j, k = 0;

        // Line 7 (For-loop: Concatenate all buckets)
        instructions.Add(instNr, new InstructionBase(Util.SET_VAR_K, instNr++));

        // Holder positions (where the sorting elements initialized)
        Vector3[] holderPos = sortMain.HolderManager.GetHolderPositions();
        for (i = 0; i < numberOfBuckets; i++)
        {
            List <BucketSortInstruction> bucket = buckets[i];
            int numberOfElementsInBucket        = bucket.Count;

            // Line 8 (For-loop: Concatenate all buckets)
            instructions.Add(instNr, new InstructionLoop(UtilSort.UPDATE_LOOP_INST, instNr++, i, numberOfBuckets, k, UtilSort.OUTER_LOOP));

            for (j = 0; j < numberOfElementsInBucket; j++)
            {
                // Line 9 (2nd For-loop: Concatenate all buckets)
                instructions.Add(instNr, new InstructionLoop(UtilSort.UPDATE_LOOP_INST, instNr++, i, j, numberOfElementsInBucket, UtilSort.INNER_LOOP));

                // Line 10 (Put element back into list)
                instructions.Add(instNr, new BucketSortInstruction(UtilSort.MOVE_BACK_INST, instNr++, i, j, k, bucket[j].SortingElementID, bucket[j].Value, false, true, Util.NO_VALUE, k, bucket[j].BucketID));

                // Line 11 (Update k)
                instructions.Add(instNr, new InstructionLoop(Util.UPDATE_VAR_K, instNr++, i, j, k));
                k++;
            }
            // Line 9: condition
            instructions.Add(instNr, new InstructionLoop(UtilSort.UPDATE_LOOP_INST, instNr++, i, j, numberOfElementsInBucket, UtilSort.INNER_LOOP));
            // Line 12 (2nd for-loop end)
            instructions.Add(instNr, new InstructionLoop(UtilSort.END_LOOP_INST, instNr++, i, Util.NO_VALUE, k, UtilSort.INNER_LOOP));
        }
        // Line 8: condition
        instructions.Add(instNr, new InstructionLoop(UtilSort.UPDATE_LOOP_INST, instNr++, i, numberOfBuckets, k, UtilSort.OUTER_LOOP));
        // Line 13 (2nd for-loop end)
        instructions.Add(instNr, new InstructionBase(Util.FINAL_INSTRUCTION, instNr++));

        return(instructions);
    }
Пример #4
0
    public override IEnumerator UserTestHighlightPseudoCode(InstructionBase instruction, bool gotSortingElement)
    {
        // Gather information from instruction
        BucketSortInstruction bucketInstruction = null;
        BucketSortElement     sortingElement    = null;

        if (gotSortingElement)
        {
            bucketInstruction = (BucketSortInstruction)instruction;

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

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

        // 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();
            numberOfBuckets = bucketSortManager.NumberOfBuckets.ToString();
            break;

        case UtilSort.CREATE_BUCKETS_INST:
            lineOfCode = 1;
            break;

        case UtilSort.FIRST_LOOP:
            lineOfCode        = 2;
            useHighlightColor = UseConditionColor(i != lengthOfListInteger);
            break;

        case UtilSort.BUCKET_INDEX_INST:
            lineOfCode = 3;
            PreparePseudocodeValue(sortingElement.Value, 1);
            bucketIndex    = bucketInstruction.BucketID;
            bucketIndexStr = bucketIndex.ToString();

            //sortingElement.IsCompare = bucketInstruction.IsCompare;
            UtilSort.IndicateElement(sortingElement.gameObject);
            break;

        case UtilSort.MOVE_TO_BUCKET_INST:
            lineOfCode        = 4;
            useHighlightColor = Util.HIGHLIGHT_USER_ACTION;
            PreparePseudocodeValue(sortingElement.Value, 1);
            UtilSort.IndicateElement(sortingElement.gameObject);

            //sortingElement.IsCompare = bucketInstruction.IsCompare;
            break;

        case UtilSort.END_LOOP_INST:
            switch (loopType)
            {
            case UtilSort.OUTER_LOOP: lineOfCode = 5; break;

            case UtilSort.INNER_LOOP: lineOfCode = 12; break;

            default: Debug.LogError(UtilSort.END_LOOP_INST + ": '" + loopType + "' loop not found"); break;
            }
            break;

        case UtilSort.PHASING_INST:
            lineOfCode = 6;
            break;

        case Util.SET_VAR_K:
            lineOfCode = 7;
            break;

        case UtilSort.UPDATE_LOOP_INST:
            if (loopType == UtilSort.OUTER_LOOP)     // 2nd loop (outher)
            {
                //j = 0;
                lineOfCode        = 8;
                useHighlightColor = UseConditionColor(i != j);
            }
            else     // 2nd loop (inner)
            {
                lineOfCode        = 9;
                bucketSize        = k.ToString();
                useHighlightColor = UseConditionColor(j != k);
            }
            break;

        case UtilSort.MOVE_BACK_INST:
            lineOfCode        = 10;
            useHighlightColor = Util.HIGHLIGHT_USER_ACTION;
            PreparePseudocodeValue(sortingElement.Value, 2);
            break;

        case Util.UPDATE_VAR_K:
            lineOfCode = 11;
            kPlus1     = (k + 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--;
    }
Пример #5
0
    public override IEnumerator ExecuteDemoInstruction(InstructionBase instruction, bool increment)
    {
        // Gather information from instruction
        BucketSortInstruction bucketInstruction = null;
        BucketSortElement     sortingElement    = null;

        if (instruction is BucketSortInstruction)
        {
            bucketInstruction = (BucketSortInstruction)instruction;

            // Change internal state of sorting element
            sortingElement = sortMain.ElementManager.GetSortingElement(bucketInstruction.SortingElementID).GetComponent <BucketSortElement>();
            bucketIndex    = bucketInstruction.BucketID;
        }

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

        // 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();
            SetBuckets(increment);
            break;

        case UtilSort.CREATE_BUCKETS_INST:
            lineOfCode = 1;
            SetBuckets(increment);
            break;

        case UtilSort.FIRST_LOOP:
            lineOfCode = 2;

            if (increment)
            {
                SetLengthOfList();
                useHighlightColor = UseConditionColor(i != lengthOfListInteger);
            }
            else
            {
                if (i == 0)
                {
                    lengthOfList = "len(list)";
                }
                else
                {
                    i -= 1;
                }
            }

            break;

        case UtilSort.BUCKET_INDEX_INST:
            lineOfCode = 3;
            PreparePseudocodeValue(sortingElement.Value, 1);
            bucketIndex = bucketInstruction.BucketID;

            if (increment)
            {
                sortingElement.IsCompare = bucketInstruction.IsCompare;
                bucketIndexStr           = bucketIndex.ToString();
            }
            else
            {
                sortingElement.IsCompare = !bucketInstruction.IsCompare;
                bucketIndexStr           = "list[i] * N / MAX_VALUE";
            }

            UtilSort.IndicateElement(sortingElement.gameObject);
            break;

        case UtilSort.MOVE_TO_BUCKET_INST:
            lineOfCode = 4;

            if (increment)
            {
                PreparePseudocodeValue(sortingElement.Value, 1);
                bucketIndex = bucketInstruction.BucketID;
                sortingElement.IsCompare = bucketInstruction.IsCompare;
            }
            else
            {
                element1Value = "list[i]";
                //bucketIndex = ""; // TODO ?
                sortingElement.IsCompare = !bucketInstruction.IsCompare;
            }
            UtilSort.IndicateElement(sortingElement.gameObject);
            break;

        case UtilSort.END_LOOP_INST:
            switch (loopType)
            {
            case UtilSort.OUTER_LOOP: lineOfCode = 5; break;

            case UtilSort.INNER_LOOP: lineOfCode = 12; break;

            default: Debug.LogError(UtilSort.END_LOOP_INST + ": '" + loopType + "' loop not found"); break;
            }
            break;

        case UtilSort.PHASING_INST:
            lineOfCode  = 6;
            i           = 0; // ????
            j           = (bucketSortManager.NumberOfBuckets - 1);
            bucketIndex = j;

            // Sort buckets
            bucketManager.AutoSortBuckets();
            break;

        case UtilSort.DISPLAY_ELEMENT:     // TODO: Fix
            //sortMain.WaitForSupportToComplete++; // add to list?
            StartCoroutine(bucketManager.PutElementsForDisplay(bucketInstruction.BucketID));

            if (!increment)
            {
                bucketManager.GetBucket(bucketIndex).SetEnterTrigger(true);
            }

            break;

        case Util.SET_VAR_K:
            lineOfCode = 7;
            break;

        case UtilSort.UPDATE_LOOP_INST:
            if (loopType == UtilSort.OUTER_LOOP)     // 2nd loop (outher)
            {
                //j = 0;
                lineOfCode = 8;
                if (increment)
                {
                    numberOfBuckets   = bucketSortManager.NumberOfBuckets.ToString();
                    useHighlightColor = UseConditionColor(i != j);
                }
                else
                {
                    if (i > 0)
                    {
                        i -= 1;
                    }
                    else
                    {
                        numberOfBuckets = "N";
                    }
                }
            }
            else     // 2nd loop (inner)
            {
                lineOfCode = 9;
                if (increment)
                {
                    bucketSize        = k.ToString();
                    useHighlightColor = UseConditionColor(j != k);
                }
                else
                {
                    if (j > 0)
                    {
                        j--;
                    }
                    else
                    {
                        bucketSize = k.ToString();
                    }
                }
            }
            break;

        case UtilSort.MOVE_BACK_INST:
            lineOfCode = 10;
            k          = bucketInstruction.NextHolderID; // ???

            if (increment)
            {
                PreparePseudocodeValue(sortingElement.Value, 2);
                sortingElement.IsSorted = bucketInstruction.IsSorted;
            }
            else
            {
                element2Value           = "buckets[" + i + "][" + j + "]";
                sortingElement.IsSorted = !bucketInstruction.IsSorted;
            }

            break;

        case Util.UPDATE_VAR_K:
            lineOfCode = 11;
            if (increment)
            {
                kPlus1 = (k + 1).ToString();
            }
            else
            {
                //if (k > 0)
                //    kPlus1 = (k - 1).ToString();
                //else
                kPlus1 = "k + 1";
            }
            break;

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

        // Highlight part of code in pseudocode
        if (instruction.Instruction == UtilSort.DISPLAY_ELEMENT)
        {
            yield return(null);
        }
        else
        {
            yield return(HighlightPseudoCode(CollectLine(lineOfCode), useHighlightColor));

            prevHighlightedLineOfCode = lineOfCode;
        }

        // Move sorting element
        if (instruction is BucketSortInstruction)
        {
            switch (bucketInstruction.Instruction)
            {
            case UtilSort.MOVE_TO_BUCKET_INST:
                if (increment)
                {
                    sortingElement.transform.position = bucketManager.GetBucket(bucketInstruction.BucketID).transform.position + UtilSort.ABOVE_BUCKET_VR;
                }
                else
                {
                    sortingElement.transform.position = bucketSortManager.GetCorrectHolder(bucketInstruction.HolderID).transform.position + UtilSort.ABOVE_HOLDER_VR;
                    sortingElement.gameObject.SetActive(true);
                    bucketManager.GetBucket(bucketIndex).RemoveSortingElement(sortingElement);
                    sortingElement.RigidBody.constraints = RigidbodyConstraints.None;
                }
                break;

            case UtilSort.DISPLAY_ELEMENT:
                if (increment)
                {
                    sortingElement.transform.position = bucketManager.GetBucket(bucketInstruction.BucketID).transform.position + UtilSort.ABOVE_BUCKET_VR;
                    sortingElement.gameObject.SetActive(true);
                }
                else
                {
                    sortingElement.gameObject.SetActive(false);
                }

                break;

            case UtilSort.MOVE_BACK_INST:
                if (increment)
                {
                    sortingElement.transform.position = bucketSortManager.GetCorrectHolder(bucketInstruction.NextHolderID).transform.position + UtilSort.ABOVE_HOLDER_VR;
                }
                else
                {
                    sortingElement.transform.position = bucketManager.GetBucket(bucketInstruction.BucketID).transform.position + UtilSort.ABOVE_BUCKET_VR;
                }
                break;
            }
        }

        // Display element reports itself when it's done
        if (instruction.Instruction != UtilSort.DISPLAY_ELEMENT)
        {
            sortMain.WaitForSupportToComplete--;
        }
    }
Пример #6
0
 protected override void Awake()
 {
     base.Awake();
     Instruction = new BucketSortInstruction(Util.INIT_INSTRUCTION, 0, Util.NO_VALUE, Util.NO_VALUE, Util.NO_VALUE, sortingElementID, value, false, false, sortingElementID, UtilSort.NO_DESTINATION, Util.NO_VALUE);
 }
Пример #7
0
    public override int PrepareNextInstruction(InstructionBase instruction)
    {
        Debug.Log(instruction.DebugInfo());

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

        switch (instruction.Instruction)
        {
        case UtilSort.PHASING_INST:
            // Phase into Insertion Sort?
            bucketManager.AutoSortBuckets();
            break;

        case UtilSort.DISPLAY_ELEMENT:
            Debug.Log("Display elements");

            // Display elements on top of bucket
            sortMain.WaitForSupportToComplete++;
            StartCoroutine(bucketManager.PutElementsForDisplay(((BucketSortInstruction)instruction).BucketID));
            return(1);    // Nothing to do for the player, nor any pseudocode
        }

        if (instruction is BucketSortInstruction)
        {
            // Get the next instruction
            BucketSortInstruction bucketSortInstruction = (BucketSortInstruction)instruction;

            // Get the Sorting element
            BucketSortElement sortingElement = sortMain.ElementManager.GetSortingElement(bucketSortInstruction.SortingElementID).GetComponent <BucketSortElement>();

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

            Bucket bucket = bucketManager.GetBucket(bucketSortInstruction.BucketID);
            bucket.BucketSortInstruction = bucketSortInstruction;

            // Give this sorting element permission to give feedback to progress to next intstruction
            if (instruction.Instruction == UtilSort.MOVE_TO_BUCKET_INST || instruction.Instruction == UtilSort.MOVE_BACK_INST)
            {
                sortingElement.NextMove = true;
            }
        }

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


        Debug.Log("Element: " + gotSortingElement + ", no destination: " + noDestination);
        if (gotSortingElement && !noDestination)
        {
            return(0);
        }

        Debug.Log(">>>>>>>>>>>>>>>>>>>>>>>>>>>> Nothing for player to do, continuing to next instruction");
        return(1);
    }