Пример #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 IEnumerator PutElementsForDisplay(int bucketID)
    {
        //sortMain.UpdateCheckList(UtilSort.ALGORITHM_MANAGER, false);
        Bucket bucket = GetBucket(bucketID);

        bucket.SetEnterTrigger(false);
        yield return(emptyBucketDuration);

        int numberOfElements = bucket.CurrenHolding.Count;

        if (numberOfElements > 0)
        {
            for (int y = 0; y < numberOfElements; y++)
            {
                BucketSortElement element = (BucketSortElement)bucket.GetElementForDisplay(y);
                bucket.PutElementForDisplay(element);
                StartCoroutine(bucket.Animation(Bucket.HIGHLIGHT, 1));
                yield return(emptyBucketDuration);
            }
            bucket.Empty();
        }

        //sortMain.UpdateCheckList(UtilSort.ALGORITHM_MANAGER, true);
        superElement.WaitForSupportToComplete--;
    }
Пример #3
0
    private void OnTriggerExit(Collider other)
    {
        if (other.tag == UtilSort.SORTING_ELEMENT_TAG)
        {
            BucketSortElement sortingElement = other.GetComponent <BucketSortElement>();

            if (onTopOfBucketTrigger.enabled)
            {
                sortingElement.CurrentInside = null;
                RemoveSortingElement(sortingElement);
                sortingElement.RigidBody.constraints = RigidbodyConstraints.None;
                prevSortingElementID = -1;
            }
        }
    }
Пример #4
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);
    }
Пример #5
0
    public void PutElementForDisplay(BucketSortElement sortingElement)
    {
        Debug.Log("Putting element " + sortingElement.SortingElementID + " for display");

        // Make sure it's still considered inside the bucket
        sortingElement.CurrentInside = this;

        // Fix position and rotation
        sortingElement.transform.position = dropElementForDisplayPos.position;
        sortingElement.transform.rotation = Quaternion.identity;

        // Enable Y-axis movement
        sortingElement.RigidBody.constraints = ~RigidbodyConstraints.FreezePositionY;

        sortingElement.GetComponent <ElementInteraction>().PutBack();

        // Disable all constraints after some seconds
        //StartCoroutine(RemoveConstraintsDelay(sortingElement));
    }
Пример #6
0
    protected override void OnCollisionEnter(Collision collision)
    {
        base.OnCollisionEnter(collision);

        if (collision.collider.tag == UtilSort.SORTING_ELEMENT_TAG)
        {
            // Current holding the sorting element that collided
            BucketSortElement element = collision.collider.GetComponent <BucketSortElement>();
            CurrentHolding = element;

            // Demo
            if (parent.SortSettings.IsDemo())
            {
                if (currentHolding.IsCompare)
                {
                    CurrentColor = UtilSort.COMPARE_COLOR;
                }
                if (currentHolding.IsSorted)
                {
                    CurrentColor = UtilSort.SORTED_COLOR;
                }
            }
        }
    }
Пример #7
0
    public void AddSortingElementToBucket(SortingElementBase sortingElement)
    {
        if (currentHolding.Contains(sortingElement))
        {
            return;
        }

        Debug.Log("Adding element: " + sortingElement.SortingElementID);
        currentHolding.Add(sortingElement);

        // Change transform parent to this bucket
        sortingElement.transform.parent = transform;

        if (sortingElement is BucketSortElement)
        {
            BucketSortElement bucketSortElement = (BucketSortElement)sortingElement;
            bucketSortElement.CurrentInside = this;
            StartCoroutine(Animation(ENTER, 3));

            // Make invisible
            bucketSortElement.transform.position    = transform.position;
            bucketSortElement.RigidBody.constraints = RigidbodyConstraints.FreezeAll;
        }
    }
Пример #8
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--;
    }
Пример #9
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--;
        }
    }
Пример #10
0
    private IEnumerator RemoveConstraintsDelay(BucketSortElement element)
    {
        yield return(colorChangeDuration);

        element.RigidBody.constraints = RigidbodyConstraints.None;
    }
Пример #11
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);
    }