// Remove element from this holder (if touched the holder) private void OnTriggerExit(Collider other) { if (other.tag == UtilSort.SORTING_ELEMENT_TAG) { // Check if other element bumped into this trigger (to avoid removing correct element) SortingElementBase otherElement = other.GetComponent <SortingElementBase>(); if (registeredAboveHolder.Contains(otherElement)) { registeredAboveHolder.Remove(otherElement); } // Remove from this holder if (CurrentHolding != null && CurrentHolding == otherElement) { prevElementID = currentHolding.SortingElementID; CurrentHolding = null; CurrentColor = Util.STANDARD_COLOR; if (registeredAboveHolder.Count > 0) { UtilSort.IndicateElement(registeredAboveHolder[0].gameObject); } } } }
// Give feedback to the user when the sorting task (user test) is completed) protected override IEnumerator FinishUserTest() { yield return(base.FinishUserTest()); // Visual feedback (make each element "jump") for (int x = 0; x < sortSettings.NumberOfElements; x++) { UtilSort.IndicateElement(elementManager.GetSortingElement(x)); elementManager.GetSortingElement(x).transform.rotation = Quaternion.identity; yield return(finishStepDuration); } WaitForSupportToComplete--; }
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--; }
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--; } }
public override IEnumerator ExecuteDemoInstruction(InstructionBase instruction, bool increment) { // Gather information from instruction MergeSortInstruction mergeSortInstruction = null; MergeSortHolderInstruction mergeSortHolderInstruction = null; MergeSortElement sortingElement = null; MergeSortHolder holder = null; if (instruction is MergeSortInstruction) { mergeSortInstruction = (MergeSortInstruction)instruction; sortingElement = sortMain.ElementManager.GetSortingElement(mergeSortInstruction.SortingElementID).GetComponent <MergeSortElement>(); holder = sortMain.HolderManager.GetHolder(mergeSortInstruction.HolderID).GetComponent <MergeSortHolder>(); } else if (instruction is MergeSortHolderInstruction) { mergeSortHolderInstruction = (MergeSortHolderInstruction)instruction; } else 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 = 0; switch (instruction.Instruction) { case Util.FIRST_INSTRUCTION: lineOfCode = 0; if (increment) { lengthOfList = sortMain.SortSettings.NumberOfElements.ToString(); } else { lengthOfList = "N"; } break; case CREATE_HOLDER: lineOfCode = 0; if (increment) { CreateSplitHolder(mergeSortHolderInstruction.MergeHolderID); } else { Destroy(splitHolders[splitHolders.Count - 1].gameObject); } break; case MOVE_ELEMENT_TO_MERGE_HOLDER: lineOfCode = 0; if (increment) { sortingElement.transform.position = mergeSortManager.GetCorrectHolder(mergeSortInstruction.NextHolderID).transform.position + UtilSort.ABOVE_HOLDER_VR; } else { sortingElement.transform.position = sortMain.HolderManager.GetHolder(mergeSortInstruction.HolderID).transform.position + UtilSort.ABOVE_HOLDER_VR; } break; case MERGE_START: lineOfCode = 0; if (increment) { } else { } break; case UtilSort.COMPARE_START_INST: lineOfCode = 0; if (increment) { sortingElement.IsCompare = mergeSortInstruction.IsCompare; sortingElement.IsSorted = mergeSortInstruction.IsSorted; } else { sortingElement.IsCompare = !mergeSortInstruction.IsCompare; if (mergeSortInstruction.HolderID == sortingElement.SortingElementID) // works for worst case, none might be buggy { sortingElement.IsSorted = mergeSortInstruction.IsSorted; } else { sortingElement.IsSorted = !mergeSortInstruction.IsSorted; } } //PreparePseudocodeValue(sortingElement.Value, 2); UtilSort.IndicateElement(sortingElement.gameObject); break; case MERGE_ELEMENT: lineOfCode = 0; if (increment) { sortingElement.transform.position = sortMain.HolderManager.GetHolder(mergeSortInstruction.NextHolderID).transform.position + UtilSort.ABOVE_HOLDER_VR; } else { sortingElement.transform.position = sortMain.HolderManager.GetHolder(mergeSortInstruction.HolderID).transform.position + UtilSort.ABOVE_HOLDER_VR; } break; case MERGE_REMAINING_ELEMENT: lineOfCode = 0; if (increment) { sortingElement.transform.position = sortMain.HolderManager.GetHolder(mergeSortInstruction.NextHolderID).transform.position + UtilSort.ABOVE_HOLDER_VR; } else { sortingElement.transform.position = sortMain.HolderManager.GetHolder(mergeSortInstruction.HolderID).transform.position + UtilSort.ABOVE_HOLDER_VR; } break; case UtilSort.FINAL_INSTRUCTION: lineOfCode = 0; break; } prevHighlightedLineOfCode = lineOfCode; // Highlight part of code in pseudocode pseudoCodeViewer.SetCodeLine(CollectLine(lineOfCode), Util.HIGHLIGHT_STANDARD_COLOR); yield return(demoStepDuration); sortMain.WaitForSupportToComplete--; }
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--; }
public override IEnumerator UserTestHighlightPseudoCode(InstructionBase instruction, bool gotSortingElement) { // Gather information from instruction InsertionSortElement sortingElement = null; if (gotSortingElement) { sortingElement = sortMain.ElementManager.GetSortingElement(((InsertionSortInstruction)instruction).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: UtilSort.IndicateElement(sortingElement.gameObject); break; case Util.FIRST_INSTRUCTION: lineOfCode = FirstInstructionCodeLine(); break; case UtilSort.FIRST_LOOP: lineOfCode = 2; i_str = i.ToString(); SetLengthOfList(); useHighlightColor = UseConditionColor(i < lengthOfListInteger); break; case Util.SET_VAR_J: lineOfCode = 3; iMinus1 = (i - 1).ToString(); break; case UtilSort.PIVOT_START_INST: lineOfCode = 4; useHighlightColor = Util.HIGHLIGHT_USER_ACTION; PreparePseudocodeValue(sortingElement.Value, 1); UtilSort.IndicateElement(sortingElement.gameObject); break; case UtilSort.COMPARE_START_INST: lineOfCode = 5; j_str = j.ToString(); useHighlightColor = Util.HIGHLIGHT_USER_ACTION; PreparePseudocodeValue(sortingElement.Value, 2); UtilSort.IndicateElement(sortingElement.gameObject); break; case UtilSort.UPDATE_LOOP_INST: // Update 2nd while loop when j < 0 lineOfCode = 5; useHighlightColor = Util.HIGHLIGHT_CONDITION_NOT_FULFILLED; j_str = j.ToString(); element2Value = "X"; break; case UtilSort.SWITCH_INST: lineOfCode = 6; jPlus1 = (j + 1).ToString(); useHighlightColor = Util.HIGHLIGHT_USER_ACTION; break; case Util.UPDATE_VAR_J: lineOfCode = 7; jMinus1 = (j - 1).ToString(); break; case UtilSort.COMPARE_END_INST: lineOfCode = 8; break; case UtilSort.PIVOT_END_INST: lineOfCode = 9; jPlus1 = (j + 1).ToString(); useHighlightColor = Util.HIGHLIGHT_USER_ACTION; break; case Util.INCREMENT_VAR_I: lineOfCode = 10; iPlus1 = (i + 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--; }
public override IEnumerator Demo(GameObject[] list) { Vector3 temp = new Vector3(); // Set first element as sorted list[0].GetComponent <SortingElementBase>().IsSorted = true; UtilSort.IndicateElement(list[0]); yield return(demoStepDuration); int listLength = list.Length; lengthOfList = listLength.ToString(); // Pseudocode i = 1; // Display pseudocode (set i) yield return(HighlightPseudoCode(CollectLine(1), Util.HIGHLIGHT_STANDARD_COLOR)); while (i < listLength) { // Check if user wants to stop the demo if (sortMain.UserStoppedTask) { break; } // Display pseudocode (1st while) yield return(HighlightPseudoCode(CollectLine(2), Util.HIGHLIGHT_STANDARD_COLOR)); // Check if user wants to stop the demo if (sortMain.UserStoppedTask) { break; } // Get index of first element to the left of the pivot and compare j = i - 1; // Display pseudocode (set j) yield return(HighlightPseudoCode(CollectLine(3), Util.HIGHLIGHT_STANDARD_COLOR)); // Check if user wants to stop the demo if (sortMain.UserStoppedTask) { break; } // Get pivot GameObject pivotObj = list[i]; InsertionSortElement pivot = list[i].GetComponent <InsertionSortElement>(); pivot.IsPivot = true; // Get pivot's initial position temp = pivot.transform.position; // Place pivot holder above the pivot element pivotHolder.transform.position = temp + pivotHolderPos; // Place the pivot on top of the pivot holder pivot.transform.position = temp + tutorialPivotElementHeight; // Set first values here to display on blackboard PreparePseudocodeValue(pivot.Value, 1); PreparePseudocodeValue(list[j].GetComponent <InsertionSortElement>().Value, 2); // Display pseudocode (set pivot) yield return(HighlightPseudoCode(CollectLine(4), Util.HIGHLIGHT_STANDARD_COLOR)); // Check if user wants to stop the demo if (sortMain.UserStoppedTask) { break; } // Start comparing until find the correct position is found // Prepare the element to compare with GameObject compareObj = list[j]; InsertionSortElement compare = compareObj.GetComponent <InsertionSortElement>(); compare.IsCompare = true; UtilSort.IndicateElement(compare.gameObject); // Display pseudocode (2nd while) yield return(HighlightPseudoCode(CollectLine(5), Util.HIGHLIGHT_STANDARD_COLOR)); // Check if user wants to stop the demo if (sortMain.UserStoppedTask) { break; } compare.IsCompare = false; compare.IsSorted = true; while (value1 < value2) { // Check if user wants to stop the demo if (sortMain.UserStoppedTask) { break; } // Pivot is smaller, start moving compare element // Compare element's position Vector3 temp2 = compare.transform.position; // Moving other element one index to the right compare.transform.position = temp; // Updating list list[j + 1] = compareObj; // Display pseudocode (move compare element) yield return(HighlightPseudoCode(CollectLine(6), Util.HIGHLIGHT_STANDARD_COLOR)); // Check if user wants to stop the demo if (sortMain.UserStoppedTask) { break; } // Preparing for next step temp = temp2; // Display pseudocode (update j) yield return(HighlightPseudoCode(CollectLine(7), Util.HIGHLIGHT_STANDARD_COLOR)); j -= 1; // Check if user wants to stop the demo if (sortMain.UserStoppedTask) { break; } // Move pivot out and place it ontop of pivot holder (above holder it check whether it's put the element) pivotHolder.transform.position = temp + pivotHolderPos; pivot.transform.position = temp + tutorialPivotElementHeight; // Wait to show the pivot being moved yield return(demoStepDuration); // Check if user wants to stop the demo if (sortMain.UserStoppedTask) { break; } // Check if there are more elements to compare the pivot with if (j >= 0) { // Start comparing until find the correct position is found // Prepare the element to compare with compareObj = list[j]; compare = compareObj.GetComponent <InsertionSortElement>(); compare.IsCompare = true; PreparePseudocodeValue(compare.Value, 2); UtilSort.IndicateElement(compare.gameObject); // Display pseudocode (2nd while new compare value) yield return(HighlightPseudoCode(CollectLine(5), Util.HIGHLIGHT_STANDARD_COLOR)); // Check if user wants to stop the demo if (sortMain.UserStoppedTask) { break; } compare.IsCompare = false; compare.IsSorted = true; if (value1 >= value2) { UtilSort.IndicateElement(compare.gameObject); // Display pseudocode (end 2nd while) yield return(HighlightPseudoCode(CollectLine(8), Util.HIGHLIGHT_STANDARD_COLOR)); // Check if user wants to stop the demo if (sortMain.UserStoppedTask) { break; } } } else { // Display pseudocode (end 2nd while) yield return(HighlightPseudoCode(CollectLine(8), Util.HIGHLIGHT_STANDARD_COLOR)); break; } } // Check if user wants to stop the demo if (sortMain.UserStoppedTask) { break; } if (i == 1 && value1 >= value2) { compare.CurrentStandingOn.CurrentColor = UtilSort.SORTED_COLOR; } // Finish off the pivots work pivot.IsSorted = true; pivot.IsPivot = false; pivot.transform.position = temp; // Put pivot object back into the list list[j + 1] = pivotObj; yield return(HighlightPseudoCode(CollectLine(9), Util.HIGHLIGHT_STANDARD_COLOR)); // Check if user wants to stop the demo if (sortMain.UserStoppedTask) { break; } // Display pseudocode (increment i) yield return(HighlightPseudoCode(CollectLine(10), Util.HIGHLIGHT_STANDARD_COLOR)); i += 1; // Check if user wants to stop the demo if (sortMain.UserStoppedTask) { break; } } // Display pseudocode (end 1st while) yield return(HighlightPseudoCode(CollectLine(11), Util.HIGHLIGHT_STANDARD_COLOR)); // Mark the last element sorted InsertionSortElement lastElement = null; if (list[list.Length - 1] != null) { lastElement = list[list.Length - 1].GetComponent <InsertionSortElement>(); } if (lastElement != null) { lastElement.IsSorted = true; } // Finished off; remove pivot holder PivotHolderVisible(false); if (sortMain.UserStoppedTask) { sortMain.UpdateCheckList(Util.DEMO, true); } else { isTaskCompleted = true; } }
protected override void UpdateSortingElementState() { if (insertionSortInstruction != null) { // Debugging instruction = insertionSortInstruction.Instruction; hID = insertionSortInstruction.HolderID; nextHolderID = insertionSortInstruction.NextHolderID; switch (insertionSortInstruction.Instruction) { case UtilSort.INIT_INSTRUCTION: status = "Init pos"; break; case UtilSort.PIVOT_START_INST: status = "Move to pivot holder"; intermediateMove = true; break; case UtilSort.PIVOT_END_INST: status = "Move down from pivot holder"; intermediateMove = true; break; case UtilSort.COMPARE_START_INST: status = "Comparing with pivot"; break; case UtilSort.COMPARE_END_INST: status = "Comparing stop"; break; case UtilSort.SWITCH_INST: status = "Move to " + nextHolderID; intermediateMove = true; break; case UtilSort.SET_SORTED_INST: IsSorted = true; UtilSort.IndicateElement(gameObject); break; case UtilSort.EXECUTED_INST: status = UtilSort.EXECUTED_INST; break; default: Debug.Log("UpdateSortingElementState(): Add '" + instruction + "' case, or ignore"); break; } if (insertionSortInstruction.IsPivot) { IsPivot = true; } else { IsPivot = false; } if (insertionSortInstruction.IsCompare) { IsCompare = true; } else { IsCompare = false; } if (insertionSortInstruction.IsSorted) { IsSorted = true; } else { IsSorted = false; } } }
public override IEnumerator UserTestHighlightPseudoCode(InstructionBase instruction, bool gotSortingElement) { // Gather information from instruction BubbleSortElement se1 = null, se2 = null; if (gotSortingElement) { se1 = sortMain.ElementManager.GetSortingElement(((BubbleSortInstruction)instruction).SortingElementID1).GetComponent <BubbleSortElement>(); se2 = sortMain.ElementManager.GetSortingElement(((BubbleSortInstruction)instruction).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(); SetLengthOfList(); n = lengthOfList; break; case UtilSort.UPDATE_LOOP_INST: if (k == UtilSort.OUTER_LOOP) { lineOfCode = 2; useHighlightColor = UseConditionColor(i != lengthOfListInteger); } else if (k == UtilSort.INNER_LOOP) { lineOfCode = 3; numberOfElementsMinusI1 = (lengthOfListInteger - i - 1).ToString(); useHighlightColor = UseConditionColor(j != lengthOfListInteger - i - 1); } break; case UtilSort.COMPARE_START_INST: lineOfCode = 4; useHighlightColor = Util.HIGHLIGHT_USER_ACTION; PreparePseudocodeValue(se1.Value, 1); PreparePseudocodeValue(se2.Value, 2); UtilSort.IndicateElement(se1.gameObject); UtilSort.IndicateElement(se2.gameObject); break; case UtilSort.SWITCH_INST: lineOfCode = 5; useHighlightColor = Util.HIGHLIGHT_USER_ACTION; break; case UtilSort.COMPARE_END_INST: lineOfCode = 6; 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(); break; } // Highlight part of code in pseudocode yield return(HighlightPseudoCode(CollectLine(lineOfCode), useHighlightColor)); // Mark prev for next round prevHighlightedLineOfCode = lineOfCode; sortMain.WaitForSupportToComplete--; }
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--; }
public override IEnumerator Demo(GameObject[] list) { int N = list.Length; i = 0; j = 0; // Display pseudocode line 1: n = len(list) yield return(HighlightPseudoCode(CollectLine(1), Util.HIGHLIGHT_STANDARD_COLOR)); for (i = 0; i < N; i++) { // Check if user wants to stop the demo if (sortMain.UserStoppedTask) { break; } // Display outer loop yield return(HighlightPseudoCode(CollectLine(2), Util.HIGHLIGHT_STANDARD_COLOR)); for (j = 0; j < N - i - 1; j++) { // Check if user wants to stop the demo if (sortMain.UserStoppedTask) { break; } // Display pseudocode (update for-loops) yield return(HighlightPseudoCode(CollectLine(3), Util.HIGHLIGHT_STANDARD_COLOR)); // Check if user wants to stop the demo if (sortMain.UserStoppedTask) { break; } // Choose sorting elements to compare BubbleSortElement p1 = list[j].GetComponent <BubbleSortElement>(); BubbleSortElement p2 = list[j + 1].GetComponent <BubbleSortElement>(); // Change status if (p1 != null) { p1.IsCompare = true; } if (p2 != null) { p2.IsCompare = true; } // Update color on holders UtilSort.IndicateElement(p1.gameObject); UtilSort.IndicateElement(p2.gameObject); // Get their values if (p1 != null) { PreparePseudocodeValue(p1.Value, 1); } if (p2 != null) { PreparePseudocodeValue(p2.Value, 2); } // Display pseudocode (list length) yield return(HighlightPseudoCode(CollectLine(4), Util.HIGHLIGHT_STANDARD_COLOR)); // Check if user wants to stop the demo if (sortMain.UserStoppedTask) { break; } if (value1 > value2) { // Switch their positions GameObject temp = list[j]; GameObject temp2 = list[j + 1]; // Switch their positions (added some stuff such that the elements get back in position) Vector3 pos1 = list[j].GetComponent <SortingElementBase>().PlacementAboveHolder, pos2 = list[j + 1].GetComponent <SortingElementBase>().PlacementAboveHolder; p1.transform.position = pos2; list[j] = temp2; p2.transform.position = pos1; list[j + 1] = temp; UtilSort.ResetRotation(temp); UtilSort.ResetRotation(temp2); // Display pseudocode (swap) yield return(HighlightPseudoCode(CollectLine(5), Util.HIGHLIGHT_STANDARD_COLOR)); // Check if user wants to stop the demo if (sortMain.UserStoppedTask) { break; } } // Display pseudocode (comparison/if end) yield return(HighlightPseudoCode(CollectLine(6), Util.HIGHLIGHT_STANDARD_COLOR)); // Check if user wants to stop the demo if (sortMain.UserStoppedTask) { break; } p1.IsCompare = false; if (p1.CurrentStandingOn != null) { p1.CurrentStandingOn.CurrentColor = UtilSort.STANDARD_COLOR; } p2.IsCompare = false; if (p2.CurrentStandingOn != null) { p2.CurrentStandingOn.CurrentColor = UtilSort.STANDARD_COLOR; } } // Display pseudocode (end 2nd for-loop) yield return(HighlightPseudoCode(CollectLine(7), Util.HIGHLIGHT_STANDARD_COLOR)); // Check if user wants to stop the demo if (sortMain.UserStoppedTask) { break; } // Biggest element moved to the last* index, mark it as sorted if (list[N - i - 1] != null) { BubbleSortElement lastElement = list[N - i - 1].GetComponent <BubbleSortElement>(); if (lastElement != null) { lastElement.IsSorted = true; } } UtilSort.IndicateElement(list[N - i - 1]); //list[N - i - 1].transform.position += Util.ABOVE_HOLDER_VR; yield return(demoStepDuration); } // Display pseudocode (end 1st for-loop) yield return(HighlightPseudoCode(CollectLine(8), Util.HIGHLIGHT_STANDARD_COLOR)); if (sortMain.UserStoppedTask) { sortMain.UpdateCheckList(Util.DEMO, true); } else { isTaskCompleted = true; } }