Skip to content

Commit f65e121

Browse files
committed
refactor: combine check and advance logic
The check(action) function in ComparisonSort has been deleted and all checking logic happens in next(action) to reduce repeated code.
1 parent 84d78ef commit f65e121

File tree

8 files changed

+29
-56
lines changed

8 files changed

+29
-56
lines changed

levels/bogo_sort.gd

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,7 @@ Keep on hitting RIGHT ARROW to CONTINUE and hope for the best!
1212
func _init(array).(array):
1313
pass
1414

15-
func check(action):
16-
return true
17-
18-
func next():
15+
func next(action):
1916
array = ArrayModel.new(array.size)
2017
if array.is_sorted():
2118
emit_signal("done")

levels/bubble_sort.gd

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,14 @@ var _swapped = false
2121
func _init(array).(array):
2222
pass
2323

24-
func check(action):
25-
if array.get(_index) > array.get(_index + 1):
26-
return action == ACTIONS.SWAP
27-
else:
28-
return action == ACTIONS.NO_SWAP
29-
30-
func next():
24+
func next(action):
3125
if array.get(_index) > array.get(_index + 1):
26+
if action != null and action != ACTIONS.SWAP:
27+
return emit_signal("mistake")
3228
array.swap(_index, _index + 1)
3329
_swapped = true
30+
elif action != null and action != ACTIONS.NO_SWAP:
31+
return emit_signal("mistake")
3432
_index += 1
3533
if _index + 1 == _end:
3634
if not _swapped:

levels/comparison_sort.gd

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -4,19 +4,18 @@ extends Node
44
signal done
55
signal mistake
66

7-
enum ACTIONS {
8-
SWAP,
9-
NO_SWAP,
7+
const ACTIONS = {
8+
"SWAP": "ui_left",
9+
"NO_SWAP": "ui_right",
1010

11-
LEFT,
12-
RIGHT,
11+
"LEFT": "ui_left",
12+
"RIGHT": "ui_right",
1313
}
1414

1515
const EFFECTS = {
1616
"NONE": GlobalTheme.GREEN,
1717
"HIGHLIGHTED": GlobalTheme.ORANGE,
1818
"DIMMED": GlobalTheme.DARK_GREEN,
19-
"WUT": 0,
2019
}
2120

2221
const DISABLE_TIME = 1.0
@@ -38,20 +37,12 @@ func _input(event):
3837
"""Pass input events for checking and take appropriate action."""
3938
if not active:
4039
return
41-
42-
for action in ACTIONS:
40+
for action in ACTIONS.values():
4341
if event.is_action_pressed(action):
44-
if check(ACTIONS[action]):
45-
return next()
46-
if event.is_pressed():
47-
emit_signal("mistake")
48-
49-
func check(action):
50-
"""Determine if the given action enum value is correct."""
51-
push_error("NotImplementedError")
42+
return next(action)
5243

53-
func next():
54-
"""Advance the state by one step and signal done if completed."""
44+
func next(action):
45+
"""Check the action and advance state or emit signal as needed."""
5546
push_error("NotImplementedError")
5647

5748
func _on_ComparisonSort_mistake():

levels/insertion_sort.gd

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,17 @@ var _index = 1 # Position of element currently being inserted
2121
func _init(array).(array):
2222
pass
2323

24-
func check(action):
25-
if array.get(_index - 1) > array.get(_index):
26-
return action == ACTIONS.SWAP
27-
else:
28-
return action == ACTIONS.NO_SWAP
29-
30-
func next():
24+
func next(action):
3125
if array.get(_index - 1) > array.get(_index):
26+
if action != null and action != ACTIONS.SWAP:
27+
return emit_signal("mistake")
3228
array.swap(_index - 1, _index)
3329
_index -= 1
3430
if _index == 0:
3531
_grow()
3632
else:
33+
if action != null and action != ACTIONS.NO_SWAP:
34+
return emit_signal("mistake")
3735
_grow()
3836

3937
func get_effect(i):

levels/selection_sort.gd

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,15 +21,13 @@ var _index = 1 # Index of tentative new smallest
2121
func _init(array).(array):
2222
pass
2323

24-
func check(action):
25-
if array.get(_base) > array.get(_index):
26-
return action == ACTIONS.SWAP
27-
else:
28-
return action == ACTIONS.NO_SWAP
29-
30-
func next():
24+
func next(action):
3125
if array.get(_base) > array.get(_index):
26+
if action != null and action != ACTIONS.SWAP:
27+
return emit_signal("mistake")
3228
array.swap(_base, _index)
29+
elif action != null and action != ACTIONS.NO_SWAP:
30+
return emit_signal("mistake")
3331
_index += 1
3432
if _index == array.size:
3533
_base += 1

scenes/levels.tscn

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,12 +48,6 @@ margin_bottom = 640.0
4848
rect_min_size = Vector2( 0, 640 )
4949
script = ExtResource( 3 )
5050

51-
[node name="Placeholder" type="Control" parent="LevelSelect/Preview/Display"]
52-
margin_left = 20.0
53-
margin_top = 20.0
54-
margin_right = 1352.0
55-
margin_bottom = 620.0
56-
5751
[node name="InfoBorder" type="MarginContainer" parent="LevelSelect/Preview"]
5852
margin_top = 648.0
5953
margin_right = 1372.0
@@ -73,7 +67,7 @@ margin_right = 810.0
7367
margin_bottom = 332.0
7468
rect_min_size = Vector2( 810, 0 )
7569
size_flags_vertical = 3
76-
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."
70+
text = "This is a short description of the algorithm. It should tell how it works in a simple yet complete way and explain its relevance in computer science. It should be accessible to the layman while not being oversimplifying."
7771
autowrap = true
7872

7973
[node name="Controls" type="Label" parent="LevelSelect/Preview/InfoBorder/Info"]
@@ -83,10 +77,7 @@ margin_bottom = 332.0
8377
rect_min_size = Vector2( 450, 0 )
8478
size_flags_horizontal = 3
8579
size_flags_vertical = 3
86-
text = "INSTRUCTIONS
87-
88-
LEFT ARROW: SWAP
89-
RIGHT ARROW: CONTINUE"
80+
text = "These are the controls for the level. They should be tailored to each level for maximum efficiency and simplicity."
9081
autowrap = true
9182

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

scripts/levels.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func get_level(name):
5151
return level
5252

5353
func _on_Timer_timeout():
54-
level.next()
54+
level.next(null)
5555

5656
func _cleanup(string):
5757
return string.strip_edges().replace("\n", " ")

scripts/menu.gd

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,4 @@ func _on_Credits_pressed():
1515
GlobalScene.change_scene("res://scenes/credits.tscn")
1616

1717
func _on_Timer_timeout():
18-
level.next()
18+
level.next(null)

0 commit comments

Comments
 (0)