From 8ebc0f7d8c57fb8381137a20269b4e48ada14694 Mon Sep 17 00:00:00 2001 From: Daniel Ting Date: Tue, 30 Jun 2020 14:57:33 -0500 Subject: [PATCH] feat: dim already compared elements in merge sort Previously, all elements in the left and right subarrays would be highlighted. Now, those elements that have already been passed over will be dimmed. --- levels/merge_sort.gd | 24 ++++++++++-------------- project.godot | 6 ------ scripts/levels.gd | 1 - 3 files changed, 10 insertions(+), 21 deletions(-) diff --git a/levels/merge_sort.gd b/levels/merge_sort.gd index b09846f..62e6594 100644 --- a/levels/merge_sort.gd +++ b/levels/merge_sort.gd @@ -24,11 +24,11 @@ func _init(array).(array): pass func next(action): - if _left == -1: + if _left == _get_middle(): if action != null and action != ACTIONS.RIGHT: return emit_signal("mistake") _right += 1 - elif _right == -1: + elif _right == _get_end(): if action != null and action != ACTIONS.LEFT: return emit_signal("mistake") _left += 1 @@ -40,30 +40,26 @@ func next(action): if action != null and action != ACTIONS.RIGHT: return emit_signal("mistake") _right += 1 - # Test if end of subarrays have been reached - if _left == _get_middle(): - _left = -1 - if _right == _get_end(): - _right = -1 # If both ends have been reached, merge and advance to next block - if _left == -1 and _right == -1: + if _left == _get_middle() and _right == _get_end(): array.sort(_get_begin(), _get_end()) _sub_no += 1 - _left = _get_begin() - _right = _get_middle() # If last block has been completed, go up a level if _sub_no == array.size / (_sub_size): _sub_size *= 2 _sub_no = 0 - _left = _get_begin() - _right = _get_middle() if _sub_size == array.size * 2: emit_signal("done") + # Update pointers + _left = _get_begin() + _right = _get_middle() func get_effect(i): - if i == _left or i == _right: + var is_left = _left != _get_middle() and i == _left + var is_right = _right != _get_end() and i == _right + if is_left or is_right: return EFFECTS.HIGHLIGHTED - if i < _sub_no * _sub_size or i >= _sub_no * _sub_size + _sub_size: + if i < _left or i >= _get_middle() and i < _right or i >= _get_end(): return EFFECTS.DIMMED return EFFECTS.NONE diff --git a/project.godot b/project.godot index bdc995c..328acbe 100644 --- a/project.godot +++ b/project.godot @@ -45,11 +45,6 @@ _global_script_classes=[ { "path": "res://levels/merge_sort.gd" }, { "base": "ComparisonSort", -"class": "QuickSort", -"language": "GDScript", -"path": "res://levels/quick_sort.gd" -}, { -"base": "ComparisonSort", "class": "SelectionSort", "language": "GDScript", "path": "res://levels/selection_sort.gd" @@ -62,7 +57,6 @@ _global_script_class_icons={ "ComparisonSort": "", "InsertionSort": "", "MergeSort": "", -"QuickSort": "", "SelectionSort": "" } diff --git a/scripts/levels.gd b/scripts/levels.gd index 44436f4..1ce23d6 100644 --- a/scripts/levels.gd +++ b/scripts/levels.gd @@ -5,7 +5,6 @@ const LEVELS = [ InsertionSort, SelectionSort, MergeSort, - QuickSort, ] var _level: ComparisonSort