Skip to content

Commit 7cdc05f

Browse files
committed
refactor: make level metadata easier to read
Instead of keeping them in constants, ComparisonSort now extracts them from its own source code.
1 parent e0bcb33 commit 7cdc05f

13 files changed

+69
-65
lines changed

levels/bogo_sort.gd

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
1-
class_name BogoSort
2-
extends ComparisonSort
1+
"""
2+
BOGOSORT
33
4-
const NAME = "BOGOSORT"
5-
const ABOUT = """
64
Generates random permutations until the array is sorted.
7-
"""
8-
const CONTROLS = """
5+
96
Keep on hitting RIGHT ARROW to CONTINUE and hope for the best!
107
"""
118

9+
class_name BogoSort
10+
extends ComparisonSort
11+
1212
func _init(array).(array):
1313
pass
1414

levels/bubble_sort.gd

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
1-
class_name BubbleSort
2-
extends ComparisonSort
1+
"""
2+
BUBBLE SORT
33
4-
const NAME = "BUBBLE SORT"
5-
const ABOUT = """
64
Bubble sort iterates through the array and looks at each pair of
75
elements, swapping them if they are out of order. When it has gone
86
through the entire array without swapping a single pair, it has
97
finished. Though simple to understand, bubble sort is hopelessly
108
inefficient on all but the smallest of arrays.
11-
"""
12-
const CONTROLS = """
9+
1310
If the two highlighted elements are out of order, hit LEFT ARROW to swap
1411
them. Otherwise, hit RIGHT ARROW to continue.
1512
"""
1613

14+
class_name BubbleSort
15+
extends ComparisonSort
16+
1717
const ACTIONS = {
1818
"SWAP": "Left",
1919
"CONTINUE": "Right",

levels/cocktail_sort.gd

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
class_name CocktailSort
2-
extends ComparisonSort
1+
"""
2+
COCKTAIL SORT
33
4-
const NAME = "COCKTAIL SORT"
5-
const ABOUT = """
64
Cocktail shaker sort is a variation of bubble sort that
75
alternates going backwards and forwards.
8-
"""
9-
const CONTROLS = """
6+
107
If the two highlighted elements are out of order, hit LEFT ARROW to swap
118
them. Otherwise, hit RIGHT ARROW to continue.
129
"""
1310

11+
class_name CocktailSort
12+
extends ComparisonSort
13+
1414
const ACTIONS = {
1515
"SWAP": "Left",
1616
"CONTINUE": "Right",

levels/comb_sort.gd

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
class_name CombSort
2-
extends ComparisonSort
1+
"""
2+
COMB SORT
33
4-
const NAME = "COMB SORT"
5-
const ABOUT = """
64
Comb sort is a variant of bubble sort that operates on gapped arrays.
7-
"""
8-
const CONTROLS = """
5+
96
If the two highlighted elements are out of order, hit LEFT ARROW to swap
107
them. Otherwise, hit RIGHT ARROW to continue.
118
"""
129

10+
class_name CombSort
11+
extends ComparisonSort
12+
1313
const SHRINK_FACTOR = 1.3
1414
const ACTIONS = {
1515
"SWAP": "Left",

levels/comparison_sort.gd

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

1313
const DISABLE_TIME = 1.0
14+
var NAME = _get_header().split(" ")[0]
15+
var DESCRIPTION = _get_header().split(" ")[1]
16+
var CONTROLS = _get_header().split(" ")[2]
1417

1518
var array: ArrayModel
1619
var moves = 0
20+
var test = _get_header().split(" ")[0]
1721

1822
var _timer = Timer.new()
1923

@@ -26,6 +30,9 @@ func _init(array):
2630
self.connect("mistake", self, "_on_ComparisonSort_mistake")
2731
self.connect("done", self, "_on_ComparisonSort_done")
2832

33+
func _get_header():
34+
return get_script().source_code.replace("\n", " ").split('"""')[1].strip_edges()
35+
2936
func _ready():
3037
set_process_input(false)
3138

levels/insertion_sort.gd

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
class_name InsertionSort
2-
extends ComparisonSort
1+
"""
2+
INSERTION SORT
33
4-
const NAME = "INSERTION SORT"
5-
const ABOUT = """
64
Insertion sort goes through the array and inserts each
75
element into its correct position. It is most similar to how most people
86
would sort a deck of cards. It is also slow on large arrays but it is
97
one of the faster quadratic algorithms. It is often used to sort smaller
108
subarrays in hybrid sorting algorithms.
11-
"""
12-
const CONTROLS = """
9+
1310
Hit LEFT ARROW to swap the two highlighted elements as long as they are
1411
out of order. When this is no longer the case, hit RIGHT ARROW to
1512
advance.
1613
"""
1714

15+
class_name InsertionSort
16+
extends ComparisonSort
17+
1818
const ACTIONS = {
1919
"SWAP": "Left",
2020
"CONTINUE": "Right",

levels/merge_sort.gd

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,20 @@
1-
class_name MergeSort
2-
extends ComparisonSort
1+
"""
2+
MERGE SORT
33
4-
const NAME = "MERGE SORT"
5-
const ABOUT = """
64
Merge sort is an efficient sorting algorithm that splits the array into
75
single-element chunks. Then it merges each pair of chunks until only one
86
sorted chunk is left by repeatedly choosing the smaller element at the
97
head of each chunk and moving the head back. However, it needs an entire
108
array's worth of auxiliary memory.
11-
"""
12-
const CONTROLS = """
9+
1310
Press the ARROW KEY corresponding to the side that the smaller
1411
highlighted element is on. If you've reached the end of one side, press
1512
the other side's ARROW KEY.
1613
"""
1714

15+
class_name MergeSort
16+
extends ComparisonSort
17+
1818
const ACTIONS = {
1919
"LEFT": "Left",
2020
"RIGHT": "Right",

levels/quick_sort.gd

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,22 @@
1-
class_name QuickSort
2-
extends ComparisonSort
1+
"""
2+
QUICKSORT
33
4-
const NAME = "QUICKSORT"
5-
const ABOUT = """
64
Quicksort designates the last element as the pivot and puts everything
75
less than the pivot before it and everything greater after it. This
86
partitioning is done by iterating through the array while keeping track
97
of a pointer initially set to the first element. Every time an element
108
less than the pivot is encountered, it is swapped with the pointed
119
element and the pointer moves forward. At the end, the pointer and pivot
1210
are swapped, and the process is repeated on the left and right halves.
13-
"""
14-
const CONTROLS = """
11+
1512
If the highlighted element is less than the pivot or the pivot has been
1613
reached, press LEFT ARROW to swap it with the pointer. Otherwise, press
1714
RIGHT ARROW to move on.
1815
"""
1916

17+
class_name QuickSort
18+
extends ComparisonSort
19+
2020
const ACTIONS = {
2121
"SWAP": "Left",
2222
"CONTINUE": "Right",

levels/selection_sort.gd

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
1-
class_name SelectionSort
2-
extends ComparisonSort
1+
"""
2+
SELECTION SORT
33
4-
const NAME = "SELECTION SORT"
5-
const ABOUT = """
64
Selection sort incrementally builds a sorted array by repeatedly looking
75
for the smallest element and swapping it onto the end of the sorted
86
portion of the array, which initially starts with size zero but grows
97
after each round. It is faster than an unoptimized bubble sort but
108
slower than insertion sort.
11-
"""
12-
const CONTROLS = """
9+
1310
Keep on hitting RIGHT ARROW until you encounter an element that is
1411
smaller than the left highlighted element, then hit LEFT ARROW and
1512
repeat.
1613
"""
1714

15+
16+
class_name SelectionSort
17+
extends ComparisonSort
18+
1819
const ACTIONS = {
1920
"SWAP": "Left",
2021
"CONTINUE": "Right",

levels/shell_sort.gd

+6-7
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,16 @@
1-
class_name ShellSort
2-
extends ComparisonSort
1+
"""
2+
SHELL SORT
33
4-
const NAME = "SHELL SORT"
5-
const ABOUT = """
64
Shell sort is a variation of insertion sort that sorts arrays separated
75
by gaps.
8-
"""
9-
const CONTROLS = """
6+
107
Hit LEFT ARROW to swap the two highlighted elements as long as they are
118
out of order. When this is no longer the case, hit RIGHT ARROW to
12-
advance.
139
"""
1410

11+
class_name ShellSort
12+
extends ComparisonSort
13+
1514
const ACTIONS = {
1615
"SWAP": "Left",
1716
"CONTINUE": "Right",

scenes/levels.tscn

+1-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ margin_right = 889.0
8383
margin_bottom = 198.0
8484
custom_constants/separation = 50
8585

86-
[node name="About" type="Label" parent="LevelSelect/Preview/InfoBorder/Info"]
86+
[node name="Description" type="Label" parent="LevelSelect/Preview/InfoBorder/Info"]
8787
margin_right = 546.0
8888
margin_bottom = 178.0
8989
size_flags_horizontal = 3

scripts/levels.gd

+4-7
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func _ready():
2323
var scores = $LevelsBorder/Levels/LevelsContainer/Scores
2424
for level in LEVELS:
2525
var button = Button.new()
26-
button.text = level.NAME
26+
button.text = level.new(ArrayModel.new()).NAME
2727
button.align = Button.ALIGN_LEFT
2828
button.connect("focus_entered", self, "_on_Button_focus_entered")
2929
button.connect("pressed", self, "_on_Button_pressed", [level])
@@ -74,8 +74,8 @@ func _on_Button_focus_entered(size=_level.array.size):
7474
$Timer.start()
7575
set_process_input(true)
7676
_level = _get_level(get_focus_owner().text).new(ArrayModel.new(size))
77-
$Preview/InfoBorder/Info/About.text = _cleanup(_level.ABOUT)
78-
$Preview/InfoBorder/Info/Controls.text = _cleanup(_level.CONTROLS)
77+
$Preview/InfoBorder/Info/Description.text = _level.DESCRIPTION
78+
$Preview/InfoBorder/Info/Controls.text = _level.CONTROLS
7979
# Start over when simulation is finished
8080
_level.connect("done", self, "_on_Button_focus_entered")
8181
# Replace old display with new
@@ -101,11 +101,8 @@ func _on_Button_pressed(level):
101101

102102
func _get_level(name):
103103
for level in LEVELS:
104-
if level.NAME == name:
104+
if level.new(ArrayModel.new()).NAME == name:
105105
return level
106106

107107
func _on_Timer_timeout():
108108
_level.next(null)
109-
110-
func _cleanup(string):
111-
return string.strip_edges().replace("\n", " ")

scripts/play.gd

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ var _level = GlobalScene.get_param(
55
"level", preload("res://scripts/levels.gd").LEVELS[0])
66

77
func _ready():
8-
$HUDBorder/HUD/Level.text = _level.NAME
8+
$HUDBorder/HUD/Level.text = _level.new(ArrayModel.new()).NAME
99

1010
func _process(delta):
1111
if _start_time >= 0:
@@ -56,7 +56,7 @@ func _on_Level_done(level):
5656
$HUDBorder/HUD.add_child(tier)
5757
restart.grab_focus()
5858
var save = GlobalScene.read_save()
59-
var name = _level.NAME
59+
var name = level.NAME
6060
var size = str(GlobalScene.get_param("size", ArrayModel.DEFAULT_SIZE))
6161
if not name in save:
6262
save[name] = {}

0 commit comments

Comments
 (0)