Skip to content

Commit 3c393fd

Browse files
committed
feat: add insertion sort
Added a complete insertion sort implementation including description. Made left arrow swap again because it makes more sense with insertion sort. Also included a nice quality-of-life feature where the game remembers the last played level and automatically highlights it for the next game.
1 parent a37cbb1 commit 3c393fd

File tree

8 files changed

+59
-10
lines changed

8 files changed

+59
-10
lines changed

levels/bubble_sort.gd

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ each pair of elements, swapping them if they are out of order. When it
77
has gone through the entire array without swapping a single pair, it has
88
finished. Though simple to understand, bubble sort is hopelessly
99
inefficient on all but the smallest of arrays."""
10+
var index = 0
1011
var swapped = false
1112

1213
func _init(array).(array):

levels/comparison_sort.gd

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ signal mistake
77
const ACTIONS = ["swap", "no_swap"]
88

99
var array: ArrayModel
10-
var index = 0
1110
var timer = Timer.new()
1211
var active = true
1312

levels/insertion_sort.gd

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
extends ComparisonSort
2+
class_name InsertionSort
3+
4+
const TITLE = "INSERTION SORT"
5+
const ABOUT = """Insertion sort goes through the array and inserts each
6+
element into its correct position. It is most similar to how most people
7+
would sort a deck of cards. It is also slow on large arrays but it is
8+
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
12+
13+
func _init(array).(array):
14+
pass
15+
16+
func check(action):
17+
if array.get(index - 1) > array.get(index):
18+
return action == "swap"
19+
else:
20+
return action == "no_swap"
21+
22+
func next():
23+
if array.get(index - 1) > array.get(index):
24+
array.swap(index - 1, index)
25+
index -= 1
26+
if index == 0:
27+
_grow()
28+
else:
29+
_grow()
30+
31+
func _grow():
32+
end += 1
33+
if end == array.size:
34+
emit_signal("done")
35+
index = end
36+
37+
func emphasized(i):
38+
return i == index or i == index - 1

project.godot

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,19 @@ _global_script_classes=[ {
3333
"class": "ComparisonSort",
3434
"language": "GDScript",
3535
"path": "res://levels/comparison_sort.gd"
36+
}, {
37+
"base": "ComparisonSort",
38+
"class": "InsertionSort",
39+
"language": "GDScript",
40+
"path": "res://levels/insertion_sort.gd"
3641
} ]
3742
_global_script_class_icons={
3843
"ArrayModel": "",
3944
"ArrayView": "",
4045
"BogoSort": "",
4146
"BubbleSort": "",
42-
"ComparisonSort": ""
47+
"ComparisonSort": "",
48+
"InsertionSort": ""
4349
}
4450

4551
[application]
@@ -106,12 +112,12 @@ ui_down={
106112
}
107113
swap={
108114
"deadzone": 0.5,
109-
"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)
115+
"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)
110116
]
111117
}
112118
no_swap={
113119
"deadzone": 0.5,
114-
"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)
120+
"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)
115121
]
116122
}
117123

scenes/levels.tscn

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,8 @@ size_flags_horizontal = 3
8585
size_flags_vertical = 3
8686
text = "INSTRUCTIONS
8787
88-
LEFT ARROW: CONTINUE
89-
RIGHT ARROW: SWAP"
88+
LEFT ARROW: SWAP
89+
RIGHT ARROW: CONTINUE"
9090
autowrap = true
9191

9292
[node name="Timer" type="Timer" parent="."]

scripts/end.gd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,5 @@ func _ready():
55
$Button.grab_focus()
66

77
func _on_Button_pressed():
8-
scene.change_scene("res://scenes/levels.tscn")
8+
scene.change_scene("res://scenes/levels.tscn",
9+
{"level": scene.get_param("level")})

scripts/levels.gd

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ extends HBoxContainer
22

33
var levels = [
44
BubbleSort,
5+
InsertionSort,
56
]
67
var level: ComparisonSort
78

@@ -14,8 +15,10 @@ func _ready():
1415
button.connect("focus_entered", self, "_on_Button_focus_changed")
1516
button.connect("pressed", self, "_on_Button_pressed", [level.TITLE])
1617
$LevelsBorder/Levels.add_child(button)
17-
# Automatically focus on first button
18-
$LevelsBorder/Levels.get_child(0).grab_focus()
18+
if scene.get_param("level") == level:
19+
button.grab_focus()
20+
if scene.get_param("level") == null:
21+
$LevelsBorder/Levels.get_child(0).grab_focus()
1922

2023
func _on_Button_focus_changed():
2124
level = get_level(get_focus_owner().text).new(ArrayModel.new(10))

scripts/play.gd

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,5 @@ func get_score():
2222
return stepify((OS.get_ticks_msec() - start_time) / 1000.0, 0.001)
2323

2424
func _on_Level_done():
25-
scene.change_scene("res://scenes/end.tscn", {"score": get_score()})
25+
scene.change_scene("res://scenes/end.tscn",
26+
{"level": scene.get_param("level"), "score": get_score()})

0 commit comments

Comments
 (0)