Skip to content

Commit 98b4a90

Browse files
committed
refactor: comply with GDScript style guide
I have rewritten my code to mostly comply with the Godot Doc's style guide, mainly prefixing private variables with an underscore and code order. One big exception is that I use 4 spaces instead of tabs for better viewing in Github. In addition, I have elected to not prefix public interface methods that are meant to be overriden with an underscore. Also added Python-style docstrings and reduced the number of magic constants. Per-level instructions have been added as well.
1 parent 741dfe2 commit 98b4a90

File tree

17 files changed

+221
-145
lines changed

17 files changed

+221
-145
lines changed

levels/bogo_sort.gd

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
1-
extends ComparisonSort
21
class_name BogoSort
2+
extends ComparisonSort
33

4-
const TITLE = "BOGOSORT"
5-
const ABOUT = """Generates random permutations until the array is
6-
sorted."""
4+
const NAME = "BOGOSORT"
5+
const ABOUT = """
6+
Generates random permutations until the array is sorted.
7+
"""
8+
const CONTROLS = """
9+
Keep on hitting RIGHT ARROW to CONTINUE and hope for the best!
10+
"""
711

812
func _init(array).(array):
913
pass
@@ -13,6 +17,8 @@ func check(action):
1317

1418
func next():
1519
array = ArrayModel.new(array.size)
20+
if array.is_sorted():
21+
emit_signal("done")
1622

1723
func emphasized(i):
1824
return false

levels/bubble_sort.gd

Lines changed: 27 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,41 @@
1-
extends ComparisonSort
21
class_name BubbleSort
2+
extends ComparisonSort
33

4-
const TITLE = "BUBBLE SORT"
5-
const ABOUT = """Bubble sort iterates through the array and looks at
6-
each pair of elements, swapping them if they are out of order. When it
7-
has gone through the entire array without swapping a single pair, it has
4+
const NAME = "BUBBLE SORT"
5+
const ABOUT = """
6+
Bubble sort iterates through the array and looks at each pair of
7+
elements, swapping them if they are out of order. When it has gone
8+
through the entire array without swapping a single pair, it has
89
finished. Though simple to understand, bubble sort is hopelessly
9-
inefficient on all but the smallest of arrays."""
10-
var index = 0
11-
var swapped = false
10+
inefficient on all but the smallest of arrays.
11+
"""
12+
const CONTROLS = """
13+
If the two highlighted elements are out of order, hit LEFT ARROW to SWAP
14+
them. Otherwise, hit RIGHT ARROW to continue.
15+
"""
16+
17+
var _index = 0
18+
var _swapped = false
1219

1320
func _init(array).(array):
1421
pass
1522

1623
func check(action):
17-
if array.get(index) > array.get(index + 1):
18-
return action == "swap"
24+
if array.get(_index) > array.get(_index + 1):
25+
return action == ACTIONS.SWAP
1926
else:
20-
return action == "no_swap"
27+
return action == ACTIONS.NO_SWAP
2128

2229
func next():
23-
if array.get(index) > array.get(index + 1):
24-
array.swap(index, index + 1)
25-
swapped = true
26-
index += 1
27-
if index == array.size - 1:
28-
if not swapped:
30+
if array.get(_index) > array.get(_index + 1):
31+
array.swap(_index, _index + 1)
32+
_swapped = true
33+
_index += 1
34+
if _index == array.size - 1:
35+
if not _swapped:
2936
emit_signal("done")
30-
index = 0
31-
swapped = false
37+
_index = 0
38+
_swapped = false
3239

3340
func emphasized(i):
34-
return i == index or i == index + 1
41+
return i == _index or i == _index + 1

levels/comparison_sort.gd

Lines changed: 32 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,54 @@
1-
extends Node
21
class_name ComparisonSort
2+
extends Node
33

44
signal done
55
signal mistake
66

7-
const ACTIONS = ["swap", "no_swap"]
7+
enum ACTIONS {SWAP, NO_SWAP}
8+
9+
const DISABLE_TIME = 1.0
810

911
var array: ArrayModel
10-
var timer = Timer.new()
1112
var active = true
1213

14+
var _timer = Timer.new()
15+
1316
func _init(array):
17+
"""Initialize array and timer."""
1418
self.array = array
15-
timer.one_shot = true
16-
timer.connect("timeout", self, "_on_Timer_timeout")
17-
add_child(timer)
19+
_timer.one_shot = true
20+
_timer.connect("timeout", self, "_on_Timer_timeout")
21+
add_child(_timer)
1822
self.connect("mistake", self, "_on_ComparisonSort_mistake")
1923

24+
func _input(event):
25+
"""Pass input events for checking and take appropriate action."""
26+
if not active:
27+
return
28+
for action in ACTIONS:
29+
if event.is_action_pressed(action):
30+
if check(ACTIONS[action]):
31+
next()
32+
else:
33+
emit_signal("mistake")
34+
2035
func check(action):
21-
pass
36+
"""Determine if the given action enum value is correct."""
37+
push_error("NotImplementedError")
2238

2339
func next():
24-
pass
40+
"""Advance the state by one step and signal done if completed."""
41+
push_error("NotImplementedError")
42+
43+
func emphasized(i):
44+
"""Return whether the given index should be highlighted."""
45+
push_error("NotImplementedError")
2546

2647
func _on_ComparisonSort_mistake():
48+
"""Disable the controls for one second."""
2749
active = false
28-
timer.start(1)
50+
_timer.start(DISABLE_TIME)
2951

3052
func _on_Timer_timeout():
53+
"""Reenable the controls."""
3154
active = true
32-
33-
func _input(event):
34-
if not active:
35-
return
36-
37-
for action in ACTIONS:
38-
if event.is_action_pressed(action):
39-
if check(action):
40-
next()
41-
else:
42-
emit_signal("mistake")

levels/insertion_sort.gd

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,46 @@
1-
extends ComparisonSort
21
class_name InsertionSort
2+
extends ComparisonSort
33

4-
const TITLE = "INSERTION SORT"
5-
const ABOUT = """Insertion sort goes through the array and inserts each
4+
const NAME = "INSERTION SORT"
5+
const ABOUT = """
6+
Insertion sort goes through the array and inserts each
67
element into its correct position. It is most similar to how most people
78
would sort a deck of cards. It is also slow on large arrays but it is
89
one of the faster quadratic algorithms. It is often used to sort smaller
9-
subarrays in hybrid sorting algorithms."""
10-
var end = 1
11-
var index = end
10+
subarrays in hybrid sorting algorithms.
11+
"""
12+
const CONTROLS = """
13+
Hit LEFT ARROW to SWAP the two highlighted elements as long as they are
14+
out of order. When this is no longer the case, hit RIGHT ARROW to
15+
advance.
16+
"""
17+
18+
var _end = 1
19+
var _index = _end
1220

1321
func _init(array).(array):
1422
pass
1523

1624
func check(action):
17-
if array.get(index - 1) > array.get(index):
18-
return action == "swap"
25+
if array.get(_index - 1) > array.get(_index):
26+
return action == ACTIONS.SWAP
1927
else:
20-
return action == "no_swap"
28+
return action == ACTIONS.NO_SWAP
2129

2230
func next():
23-
if array.get(index - 1) > array.get(index):
24-
array.swap(index - 1, index)
25-
index -= 1
26-
if index == 0:
31+
if array.get(_index - 1) > array.get(_index):
32+
array.swap(_index - 1, _index)
33+
_index -= 1
34+
if _index == 0:
2735
_grow()
2836
else:
2937
_grow()
3038

3139
func _grow():
32-
end += 1
33-
if end == array.size:
40+
_end += 1
41+
if _end == array.size:
3442
emit_signal("done")
35-
index = end
43+
_index = _end
3644

3745
func emphasized(i):
38-
return i == index or i == index - 1
46+
return i == _index or i == _index - 1

levels/selection_sort.gd

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,41 @@
1-
extends ComparisonSort
21
class_name SelectionSort
2+
extends ComparisonSort
3+
4+
const NAME = "SELECTION SORT"
5+
const ABOUT = """
6+
Selection sort incrementally builds a sorted array by repeatedly looking
7+
for the smallest element and swapping it onto the end of the sorted
8+
portion of the array, which initially starts with size zero but grows
9+
after each round. It is faster than an unoptimized bubble sort but
10+
slower than insertion sort.
11+
"""
12+
const CONTROLS = """
13+
Keep on hitting RIGHT ARROW until you encounter an element that is
14+
smaller than the left highlighted element, then hit LEFT ARROW to swap
15+
the new smallest into place and keep going.
16+
"""
317

4-
const TITLE = "SELECTION SORT"
5-
const ABOUT = """Selection sort incrementally builds a sorted array by
6-
repeatedly looking for the smallest element and swapping it onto the
7-
end of the sorted portion of the array, which initially starts with size
8-
zero but grows after each round. It is faster than an unoptimized bubble
9-
sort but slower than insertion sort."""
10-
var base = 0
11-
var index = base + 1
18+
var _base = 0
19+
var _index = _base + 1
1220

1321
func _init(array).(array):
1422
pass
1523

1624
func check(action):
17-
if array.get(base) > array.get(index):
18-
return action == "swap"
25+
if array.get(_base) > array.get(_index):
26+
return action == ACTIONS.SWAP
1927
else:
20-
return action == "no_swap"
28+
return action == ACTIONS.NO_SWAP
2129

2230
func next():
23-
if array.get(base) > array.get(index):
24-
array.swap(base, index)
25-
index += 1
26-
if index == array.size:
27-
base += 1
28-
index = base + 1
29-
if base == array.size - 1:
31+
if array.get(_base) > array.get(_index):
32+
array.swap(_base, _index)
33+
_index += 1
34+
if _index == array.size:
35+
_base += 1
36+
_index = _base + 1
37+
if _base == array.size - 1:
3038
emit_signal("done")
3139

3240
func emphasized(i):
33-
return i == index or i == base
41+
return i == _index or i == _base

models/array_model.gd

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,34 @@
11
"""
2-
A plain old one-dimensional random access array.
2+
A plain old one-dimensional random access array wrapper around the
3+
built-in class.
34
"""
45

5-
extends Reference
66
class_name ArrayModel
7+
extends Reference
78

89
var array = []
9-
var size
10+
var size = 0
1011

11-
func _init(size):
12+
func _init(size=16):
13+
"""Randomize the array."""
1214
for i in range(1, size + 1):
1315
array.append(i)
1416
array.shuffle()
1517
self.size = size
1618

1719
func get(i):
20+
"""Retrieve the value of the element at index i."""
1821
return array[i]
1922

2023
func is_sorted():
24+
"""Check if the array is in monotonically increasing order."""
2125
for i in range(size - 1):
2226
if array[i] > array[i + 1]:
2327
return false
2428
return true
2529

2630
func swap(i, j):
31+
"""Swap the elements at indices i and j."""
2732
var temp = array[i]
2833
array[i] = array[j]
2934
array[j] = temp

project.godot

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,8 @@ config/icon="res://assets/icon.png"
6464

6565
[autoload]
6666

67-
scene="*res://scripts/scene.gd"
67+
GlobalScene="*res://scripts/scene.gd"
68+
GlobalTheme="*res://scripts/theme.gd"
6869

6970
[display]
7071

@@ -116,12 +117,12 @@ ui_down={
116117
, Object(InputEventJoypadButton,"resource_local_to_scene":false,"resource_name":"","device":0,"button_index":13,"pressure":0.0,"pressed":false,"script":null)
117118
]
118119
}
119-
swap={
120+
SWAP={
120121
"deadzone": 0.5,
121122
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777231,"unicode":0,"echo":false,"script":null)
122123
]
123124
}
124-
no_swap={
125+
NO_SWAP={
125126
"deadzone": 0.5,
126127
"events": [ Object(InputEventKey,"resource_local_to_scene":false,"resource_name":"","device":0,"alt":false,"shift":false,"control":false,"meta":false,"command":false,"pressed":false,"scancode":16777233,"unicode":0,"echo":false,"script":null)
127128
]

scenes/levels.tscn

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,15 @@ margin_right = 1352.0
6868
margin_bottom = 352.0
6969
custom_constants/separation = 50
7070

71-
[node name="Description" type="Label" parent="LevelSelect/Preview/InfoBorder/Info"]
71+
[node name="About" type="Label" parent="LevelSelect/Preview/InfoBorder/Info"]
7272
margin_right = 810.0
7373
margin_bottom = 332.0
7474
rect_min_size = Vector2( 810, 0 )
7575
size_flags_vertical = 3
7676
text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Suspendisse luctus lorem felis, in imperdiet mauris faucibus in. Vestibulum interdum mi at arcu congue congue. Cras sodales mauris odio, eget iaculis dolor tempor quis. Suspendisse nec iaculis sapien, eu sollicitudin orci. Nulla volutpat pellentesque ex nec cursus."
7777
autowrap = true
7878

79-
[node name="Instructions" type="Label" parent="LevelSelect/Preview/InfoBorder/Info"]
79+
[node name="Controls" type="Label" parent="LevelSelect/Preview/InfoBorder/Info"]
8080
margin_left = 860.0
8181
margin_right = 1332.0
8282
margin_bottom = 332.0

0 commit comments

Comments
 (0)