// 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); } } } }
// Destroys all elements + reset public void DestroyAndReset() { Util.DestroyObjects(sortingElements); containsElements = false; SortingElementBase.SORTING_ELEMENT_NR = 0; usedValues = new HashSet <int>(); currentMoving = null; superElement = null; }
// Not used public SortingElementBase RemoveSoringElement() { if (currentHolding.Count > 0) { SortingElementBase element = currentHolding[0]; currentHolding.RemoveAt(0); return(element); } return(null); }
// Returns a string with the initial list values, e.g.: [13, 4, 2, ..., 43] public string InitList() { string result = "["; foreach (GameObject obj in sortingElements) { SortingElementBase element = obj.GetComponent <SortingElementBase>(); result += element.Value + ", "; } return(result.Substring(0, result.Length - 2) + "]"); }
public void NotifyMovingElement(SortingElementBase currentMoving, bool moving) { if (moving) { this.currentMoving = currentMoving; } else { this.currentMoving = null; } }
public void RemoveSortingElement(SortingElementBase sortingElement) { if (currentHolding.Contains(sortingElement)) { currentHolding.Remove(sortingElement); if (prevSortingElementID == sortingElement.SortingElementID) { prevSortingElementID = -1; } } }
// Set as current holding, and set element as current on top of this holder protected virtual void OnCollisionEnter(Collision collision) { //Debug.Log(collision.collider.tag + " collided with holder " + holderID); if (collision.collider.tag == UtilSort.SORTING_ELEMENT_TAG) { SortingElementBase element = collision.collider.GetComponent <SortingElementBase>(); element.CurrentStandingOn = this; if (parent.Settings.IsUserTest() && element.UserInteraction) { element.PerformUserMove(this); } } }
// Register elements above private void OnTriggerStay(Collider other) { //Debug.Log("Entering holder" + holderID + ": " + other.tag); if (other.tag == UtilSort.SORTING_ELEMENT_TAG) // .compareTag() { SortingElementBase element = other.GetComponent <SortingElementBase>(); if (!registeredAboveHolder.Contains(element)) { registeredAboveHolder.Add(element); } //if (parent.Settings.Difficulty == Util.BEGINNER) // GiveHint(); } }
public static List <SortingElementBase> InsertionSortStandard2(List <SortingElementBase> list) { int i = 1; while (i < list.Count) { SortingElementBase pivot = list[i]; int j = i - 1; while (j >= 0 && pivot.Value < list[j].Value) { list[j + 1] = list[j]; j -= 1; } list[j + 1] = pivot; i += 1; } return(list); }
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; } }
private bool isValidSortingElement(SortingElementBase element) { return(element != null && element.Instruction != null); }
public override IEnumerator Demo(GameObject[] sortingElements) { i = 0; j = 0; // Line 0 (set parameter) pseudoCodeViewer.SetCodeLine(CollectLine(0), Util.BLACKBOARD_TEXT_COLOR); // Create buckets Vector3[] pos = new Vector3[1] { bucketManager.FirstBucketPosition }; int numberOfBuckets = bucketSortManager.NumberOfBuckets; bucketManager.CreateObjects(numberOfBuckets, pos); // Line 1 (Create buckets) yield return(HighlightPseudoCode(CollectLine(1), Util.HIGHLIGHT_STANDARD_COLOR)); // Buckets GameObject[] buckets = null; if (bucketManager != null) { buckets = bucketManager.Buckets; } // Add elements to buckets for (i = 0; i < sortingElements.Length; i++) { // Check if user wants to stop the demo if (sortMain.UserStoppedTask) { break; } // Line 2 (Update for-loop) yield return(HighlightPseudoCode(CollectLine(2), Util.HIGHLIGHT_STANDARD_COLOR)); // Check if user wants to stop the demo if (sortMain.UserStoppedTask) { break; } // Get element GameObject element = sortingElements[i]; PreparePseudocodeValue(element.GetComponent <SortingElementBase>().Value, 1); // Bucket index bucketIndex = BucketIndex(value1, numberOfBuckets, maxValue); // Line 3 (Display bucket index) yield return(HighlightPseudoCode(CollectLine(3), Util.HIGHLIGHT_STANDARD_COLOR)); // Check if user wants to stop the demo if (sortMain.UserStoppedTask) { break; } // Get bucket Bucket bucket = buckets[bucketIndex].GetComponent <Bucket>(); // element.GetComponent<SortingElementBase>().Value - minValue); // Move element above the bucket and put it inside element.transform.position = bucket.transform.position + UtilSort.ABOVE_BUCKET_VR; // Line 4 (Put element into bucket) yield return(HighlightPseudoCode(CollectLine(4), Util.HIGHLIGHT_STANDARD_COLOR)); } // Line 5 (end for-loop) yield return(HighlightPseudoCode(CollectLine(5), Util.HIGHLIGHT_STANDARD_COLOR)); // Display elements for (int x = 0; x < numberOfBuckets; x++) { // Check if user wants to stop the demo if (sortMain.UserStoppedTask) { break; } // Line 6 (For-loop: Sort elements in buckets) //pseudoCodeViewer.SetCodeLine(6, PseudoCode(6, x, Util.NO_VALUE, Util.NO_VALUE, true), Util.HIGHLIGHT_COLOR); //yield return new WaitForSeconds(seconds); //pseudoCodeViewer.SetCodeLine(6, PseudoCode(6, x, Util.NO_VALUE, Util.NO_VALUE, true), Util.BLACKBOARD_TEXT_COLOR); Bucket bucket = buckets[x].GetComponent <Bucket>(); bucket.SetEnterTrigger(false); // Sort bucket *** TODO: go to insertion sort scene bucket.CurrenHolding = InsertionSort.InsertionSortStandard2(bucket.CurrenHolding); // Line 6 (Sort elements in bucket) i = x; yield return(HighlightPseudoCode(CollectLine(6), Util.HIGHLIGHT_STANDARD_COLOR)); // Check if user wants to stop the demo if (sortMain.UserStoppedTask) { break; } // Put elements for display on top of buckets int numberOfElementsInBucket = bucket.CurrenHolding.Count; for (int y = 0; y < numberOfElementsInBucket; y++) { // Check if user wants to stop the demo if (sortMain.UserStoppedTask) { break; } SortingElementBase element = bucket.GetElementForDisplay(y); element.gameObject.active = true; element.transform.position += UtilSort.ABOVE_BUCKET_VR; yield return(demoStepDuration); } } // Line 8 (end for loop) //pseudoCodeViewer.SetCodeLine(8, PseudoCode(8, Util.NO_VALUE, Util.NO_VALUE, Util.NO_VALUE, true), Util.HIGHLIGHT_COLOR); //yield return new WaitForSeconds(seconds); //pseudoCodeViewer.SetCodeLine(8, PseudoCode(8, Util.NO_VALUE, Util.NO_VALUE, Util.NO_VALUE, true), Util.BLACKBOARD_TEXT_COLOR); // Put elements back into list k = 0; // Line 7 (set k) yield return(HighlightPseudoCode(CollectLine(7), Util.HIGHLIGHT_STANDARD_COLOR)); // Holder positions (where the sorting elements initialized) Vector3[] holderPos = sortMain.HolderManager.GetHolderPositions(); // while (k < sortingElements.Length && i < numberOfBuckets) for (i = 0; i < numberOfBuckets; i++) { // Check if user wants to stop the demo if (sortMain.UserStoppedTask) { break; } Bucket bucket = buckets[i].GetComponent <Bucket>(); // number of elements in bucket bucketSize = bucket.CurrenHolding.Count.ToString(); // Line 8 (For-loop: Concatenate all buckets) yield return(HighlightPseudoCode(CollectLine(8), Util.HIGHLIGHT_STANDARD_COLOR)); for (j = 0; j < bucket.CurrenHolding.Count; j++) { // Check if user wants to stop the demo if (sortMain.UserStoppedTask) { break; } // Line 9 (2nd For-loop: Concatenate all buckets) yield return(HighlightPseudoCode(CollectLine(9), Util.HIGHLIGHT_STANDARD_COLOR)); // Check if user wants to stop the demo if (sortMain.UserStoppedTask) { break; } sortingElements[k] = bucket.RemoveSoringElement().gameObject; // Value of sorting element PreparePseudocodeValue(sortingElements[k].GetComponent <SortingElementBase>().Value, 2); // Move element back to holder sortingElements[k].transform.position = holderPos[k] + UtilSort.ABOVE_HOLDER_VR; sortingElements[k].transform.rotation = Quaternion.identity; sortingElements[k].GetComponent <SortingElementBase>().IsSorted = true; // Line 10 (Put element back into list) yield return(HighlightPseudoCode(CollectLine(10), Util.HIGHLIGHT_STANDARD_COLOR)); // Check if user wants to stop the demo if (sortMain.UserStoppedTask) { break; } // Line 11 (Update k) yield return(HighlightPseudoCode(CollectLine(11), Util.HIGHLIGHT_STANDARD_COLOR)); k++; } // Line 12 (2nd for-inner-loop end) yield return(HighlightPseudoCode(CollectLine(12), Util.HIGHLIGHT_STANDARD_COLOR)); // Check if user wants to stop the demo if (sortMain.UserStoppedTask) { break; } } // Line 13 (2nd for-loop end) yield return(HighlightPseudoCode(CollectLine(13), Util.HIGHLIGHT_STANDARD_COLOR)); if (sortMain.UserStoppedTask) { sortMain.UpdateCheckList(Util.DEMO, true); } else { isTaskCompleted = true; } }
private bool ValidateSortingElement(SortingElementBase element) { return((element.Value >= bucketCapacity[0] && element.Value < bucketCapacity[1]) ? true : false); }
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--; }