Skip to content

Commit 68dd250

Browse files
committed
refactor: move level information into constants
This effectively undoes 7cdc05f, which was honestly a dumb idea to begin with, and turned out to break exports for some reason after doing a git bisect.
1 parent b1c18e0 commit 68dd250

14 files changed

+75
-111
lines changed

levels/bogo_sort.gd

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,13 @@
1-
"""
2-
BOGOSORT
3-
1+
class_name BogoSort
2+
extends ComparisonSort
43

4+
const NAME = "BOGOSORT"
5+
const DESCRIPTION = """
56
Generates random permutations until the array is sorted.
6-
7-
7+
"""
8+
const CONTROLS = """
89
Keep on hitting RIGHT ARROW to CONTINUE and hope for the best!
910
"""
10-
11-
class_name BogoSort
12-
extends ComparisonSort
13-
1411
const CODE = """
1512
def bogosort(a):
1613
while not a.sorted():

levels/bubble_sort.gd

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
"""
2-
BUBBLE SORT
3-
1+
class_name BubbleSort
2+
extends ComparisonSort
43

4+
const NAME = "BUBBLE SORT"
5+
const DESCRIPTION = """
56
Bubble sort looks at consecutive pairs of elements and swaps them if
67
they are out of order, finishing when it has gone through the whole
78
array from beginning to end without a single swap. The actual level
@@ -12,15 +13,11 @@ Due to its simplicity, it is commonly taught as the first sorting
1213
algorithm students learn in computer science classes, but is rarely used
1314
in real life because it is slow on large data and other simple quadratic
1415
algorithms like insertion sort perform better.
15-
16-
16+
"""
17+
const CONTROLS = """
1718
If the two highlighted elements are out of order, hit LEFT ARROW to swap
1819
them. Otherwise, hit RIGHT ARROW to continue.
1920
"""
20-
21-
class_name BubbleSort
22-
extends ComparisonSort
23-
2421
const CODE = """
2522
def bubble_sort(a):
2623
swapped = true

levels/cocktail_sort.gd renamed to levels/cocktail_shaker_sort.gd

+8-11
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,21 @@
1-
"""
2-
COCKTAIL SORT
3-
1+
class_name CocktailSort
2+
extends ComparisonSort
43

5-
Cocktail sort is a variation of bubble sort that alternates going
4+
const NAME = "COCKTAIL SHAKER SORT"
5+
const DESCRIPTION = """
6+
Cocktail shaker sort is a variation of bubble sort that alternates going
67
backwards and forwards. The actual level contains an optimization that
78
skips over elements guaranteed to be already in place.
89
910
Because it is bidirectional, it is slightly faster than bubble sort, but
1011
is still quadratic and therefore not used on large data.
11-
12-
12+
"""
13+
const CONTROLS = """
1314
If the two highlighted elements are out of order, hit LEFT ARROW to swap
1415
them. Otherwise, hit RIGHT ARROW to continue.
1516
"""
16-
17-
class_name CocktailSort
18-
extends ComparisonSort
19-
2017
const CODE = """
21-
def cocktail_sort(a):
18+
def cocktail_shaker_sort(a):
2219
swapped = true
2320
while swapped:
2421
swapped = false

levels/comb_sort.gd

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
"""
2-
COMB SORT
3-
1+
class_name CombSort
2+
extends ComparisonSort
43

4+
const NAME = "COMB SORT"
5+
const DESCRIPTION = """
56
Comb sort is a variant of bubble sort that compares elements a certain
67
gap apart instead of consecutive elements. This gap is divided after
78
every pass by an experimentally determined optimal factor of about 1.3.
@@ -10,15 +11,11 @@ Once the gap becomes 1, comb sort becomes a regular bubble sort.
1011
This allows comb sort to get rid of small values near the end more
1112
quickly, which turns out to be the bottleneck in bubble sort, but still
1213
has a quadratic worst case.
13-
14-
14+
"""
15+
const CONTROLS = """
1516
If the two highlighted elements are out of order, hit LEFT ARROW to swap
1617
them. Otherwise, hit RIGHT ARROW to continue.
1718
"""
18-
19-
class_name CombSort
20-
extends ComparisonSort
21-
2219
const CODE = """
2320
def comb_sort(a):
2421
gap = len(a)

levels/comparison_sort.gd

-6
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,6 @@ const EFFECTS = {
1111
}
1212

1313
const DISABLE_TIME = 1.0
14-
var NAME = _get_header().split(" ")[0]
15-
var DESCRIPTION = _get_header().split(" ")[1].replace(" ", "\n\n")
16-
var CONTROLS = _get_header().split(" ")[-1]
1714

1815
var array: ArrayModel
1916

@@ -28,9 +25,6 @@ func _init(array):
2825
self.connect("mistake", self, "_on_ComparisonSort_mistake")
2926
self.connect("done", self, "_on_ComparisonSort_done")
3027

31-
func _get_header():
32-
return get_script().source_code.replace("\n", " ").split('"""')[1].strip_edges()
33-
3428
func _ready():
3529
set_process_input(false)
3630

levels/cycle_sort.gd

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
"""
2-
CYCLE SORT
3-
1+
class_name CycleSort
2+
extends ComparisonSort
43

4+
const NAME = "CYCLE SORT"
5+
const DESCRIPTION = """
56
Cycle sort looks at the first element and finds its correct final
67
position by counting the number of elements smaller than it. Then it
78
saves the element at that index, writes the first element there, and
@@ -11,15 +12,11 @@ demonstration, in the actual level, swaps are used instead.
1112
This results in a quadratic runtime but gives it the special property
1213
of being optimal in the number of writes to the array. This makes cycle
1314
sort useful in situations where writes are very expensive.
14-
15-
15+
"""
16+
const CONTROLS = """
1617
If the highlighted element is less than the element below the blue
1718
pointer, hit LEFT ARROW. Otherwise, hit RIGHT ARROW.
1819
"""
19-
20-
class_name CycleSort
21-
extends ComparisonSort
22-
2320
const CODE = """
2421
def cycle_sort(a):
2522
for i in range(len(a)):

levels/insertion_sort.gd

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,20 @@
1-
"""
2-
INSERTION SORT
3-
1+
class_name InsertionSort
2+
extends ComparisonSort
43

4+
const NAME = "INSERTION SORT"
5+
const DESCRIPTION = """
56
Insertion sort goes through the array and inserts each element into its
67
correct place, like how most people would sort a hand of playing cards.
78
89
It is one of the fastest quadratic algorithms in practice and is
910
efficient on small or almost sorted data. It is also simple, stable, and
1011
in-place. For these reasons it is sometimes used within faster divide
1112
and conquer algorithms when the array has been divided to a small size.
12-
13-
13+
"""
14+
const CONTROLS = """
1415
If the two highlighted elements are out of order, hit LEFT ARROW to swap
1516
them. Otherwise, hit RIGHT ARROW to continue.
1617
"""
17-
18-
class_name InsertionSort
19-
extends ComparisonSort
20-
2118
const CODE = """
2219
def insertion_sort(a):
2320
for i in range(len(a)):

levels/merge_sort.gd

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,18 @@
1-
"""
2-
MERGE SORT
3-
1+
class_name MergeSort
2+
extends ComparisonSort
43

4+
const NAME = "MERGE SORT"
5+
const DESCRIPTION = """
56
Merge sort merges subarrays of increasing size by setting a pointer to
67
the head of each half. Then it repeatedly copies the smaller pointed
78
element and increments that side's pointer. When one side is exhausted,
89
it copies the rest of the other side and overwrites the two halves with
910
the merged copy.
10-
11-
11+
"""
12+
const CONTROLS = """
1213
Press the ARROW KEY corresponding to the side that the smaller
1314
highlighted element is on or the non-exhausted side.
1415
"""
15-
16-
class_name MergeSort
17-
extends ComparisonSort
18-
1916
const CODE = """
2017
def merge_sort(a):
2118
size = 1

levels/odd_even_sort.gd

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
1-
"""
2-
ODD-EVEN SORT
3-
1+
class_name OddEvenSort
2+
extends ComparisonSort
43

4+
const NAME = "ODD-EVEN SORT"
5+
const DESCRIPTION = """
56
Odd-even sort is a variant of bubble sort that alternates between
67
comparing consecutive odd-even and even-odd indexed pairs.
78
89
It is not of much use on a single processor as it is designed for
910
parallel processors, which can perform every comparison in a single pass
1011
at the same time, thus making the algorithm much more efficient.
11-
12-
12+
"""
13+
const CONTROLS = """
1314
If the two highlighted elements are out of order, hit LEFT ARROW to swap
1415
them. Otherwise, hit RIGHT ARROW to continue.
1516
"""
16-
17-
class_name OddEvenSort
18-
extends ComparisonSort
19-
2017
const CODE = """
2118
def odd_even_sort(a):
2219
swapped = true

levels/quick_sort.gd

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
"""
2-
QUICKSORT
3-
1+
class_name QuickSort
2+
extends ComparisonSort
43

4+
const NAME = "QUICKSORT"
5+
const DESCRIPTION = """
56
Quicksort designates the last element as the pivot and sets a pointer to
67
the first element. Then it iterates through the array. Every time an
78
element smaller than the pivot is encountered, that element is swapped
@@ -11,15 +12,11 @@ recursively repeated on the left and right halves.
1112
1213
Quicksort competes with other linearithmic algorithms like merge sort,
1314
which it is faster than at the tradeoff of stability.
14-
15-
15+
"""
16+
const CONTROLS = """
1617
If the highlighted element is less than the pivot or the pivot has been
1718
reached, press LEFT ARROW. Otherwise, press RIGHT ARROW.
1819
"""
19-
20-
class_name QuickSort
21-
extends ComparisonSort
22-
2320
const CODE = """
2421
def quicksort(a, low=0, high=len(a) - 1):
2522
if low < high:

levels/selection_sort.gd

+6-10
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,19 @@
1-
"""
2-
SELECTION SORT
3-
1+
class_name SelectionSort
2+
extends ComparisonSort
43

4+
const NAME = "SELECTION SORT"
5+
const DESCRIPTION = """
56
Selection sort incrementally builds a sorted subarray by finding the
67
smallest unprocessed element and putting it in place.
78
89
It is not very useful in real life as it is beat by insertion sort.
910
However, it has the distinguishing feature of making the least number
1011
of swaps in the worst case.
11-
12-
12+
"""
13+
const CONTROLS = """
1314
If the two highlighted elements are out of order, hit LEFT ARROW to swap
1415
them. Otherwise, hit RIGHT ARROW to continue.
1516
"""
16-
17-
18-
class_name SelectionSort
19-
extends ComparisonSort
20-
2117
const CODE = """
2218
def selection_sort(a):
2319
for i in range(len(a)):

levels/shell_sort.gd

+6-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
"""
2-
SHELL SORT
3-
1+
class_name ShellSort
2+
extends ComparisonSort
43

4+
const NAME = "SHELL SORT"
5+
const DESCRIPTION = """
56
Shell sort is a variant of insertion sort that compares elements a
67
certain gap apart instead of consecutive elements. This gap is divided
78
by 2 after every pass. Once the gap becomes 1, shell sort becomes a
@@ -10,15 +11,11 @@ regular insertion sort.
1011
This allows the final pass of insertion sort to avoid having to move
1112
elements long distances. However, it still has a quadratic worst case,
1213
which can be reduced with more complex gap sequences.
13-
14-
14+
"""
15+
const CONTROLS = """
1516
If the two highlighted elements are out of order, hit LEFT ARROW to swap
1617
them. Otherwise, hit RIGHT ARROW to continue.
1718
"""
18-
19-
class_name ShellSort
20-
extends ComparisonSort
21-
2219
const CODE = """
2320
def shell_sort(a):
2421
gap = len(a)

project.godot

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ _global_script_classes=[ {
4242
"base": "ComparisonSort",
4343
"class": "CocktailSort",
4444
"language": "GDScript",
45-
"path": "res://levels/cocktail_sort.gd"
45+
"path": "res://levels/cocktail_shaker_sort.gd"
4646
}, {
4747
"base": "ComparisonSort",
4848
"class": "CombSort",

scripts/levels.gd

+6-2
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,12 @@ func _reload():
4848
_restart()
4949
_load_scores(_level)
5050
$NamesContainer/Names/Current.text = _level.NAME
51-
$Level/Left/Code.text = _level.DESCRIPTION + "\n\n" + _level.CODE.strip_edges()
52-
$Level/Right/Info/ControlsContainer/Controls.text = _level.CONTROLS
51+
$Level/Left/Code.text = _format(_level.DESCRIPTION) + "\n\n" + _level.CODE.strip_edges()
52+
$Level/Right/Info/ControlsContainer/Controls.text = _format(_level.CONTROLS)
53+
54+
func _format(text):
55+
# Helper method to format text
56+
return text.strip_edges().replace("\n", " ").replace(" ", "\n\n")
5357

5458
func _restart():
5559
set_process_input(true)

0 commit comments

Comments
 (0)