Midnight Deck Dash · Lesson 40 of 50 · Intermediate to advanced · About 40 min
The crew can test one tiny deck every time they change the game. A deliberate fault proves that the tests can notice a problem.

Download the projects from the course page. In Godot’s Project Manager, choose Import, select project.godot, and use Run Project (F5). Start from checkpoints/39 when the lesson builds on prior work, or copy the checkpoints/40 folder to try this lesson on its own. Read the file names in the steps before editing.


Build it step by step
Language and tools in this lesson Dictionary.duplicate, conditions, PackedStringArray, comparison, regression testing.
-
Open the editable checkpoint
In Godot Project Manager, choose Import, select this lesson's project.godot and click Import & Edit. Open main.gd from FileSystem and press F5 to see the finished checkpoint. Press F8 before trying a change.
Why it works The lesson scene already has a Control root with this script attached.
-
Make a miniature test deck
Read MAP and point to the start, parcel, Station A and exit characters. The intended route is East, East, South.
const MAP := ["#####", "#@aA#", "#..x#", "#####"]Why it works A fixture is a small example with a known outcome. Reusing the same board makes tests comparable.
-
Copy incoming state before changing it
Find previous.duplicate at the start of turn. Notice that each test can reuse the start Dictionary.
var next := previous.duplicate()Why it works Copying gives the rule a new object to update. The original keeps its old cell, parcel status and turns.
-
Set up exact checks
Find checks. Predict the value for the station test when the courier arrives without a parcel.
"Do not deliver an empty hand": empty_station["parcel"] == "waiting",Why it works A named check compares the actual state against an expected result. An empty-handed courier still has the waiting state.
-
Watch a check catch a fault
Press Toggle wall bug. Read the failed row. Then return to the code and trace the and not simulate_bug clause.
if symbol == "#" and not simulate_bug:Why it works The practice toggle stops one rule from refusing a wall. The failed test identifies the violated promise.
-
Prove a fix returns the expected result
Toggle the wall bug off, click Run rule checks and confirm every check passes. Use this sequence after any deliberate rule change.
report.text = "%d / %d checks pass" % [passed, checks.size()]Why it works Running the same fixture after a change helps catch regressions in rules that previously worked.
Watch the values change
-
Run checks Six expected outcomes match.
What you see. The report shows 6 / 6. -
Toggle wall bug The wall guard is bypassed.
What you see. Only the wall outcome fails, so 5 / 6 pass. -
Toggle back The guard protects the wall again.
What you see. The report returns to 6 / 6.
Predict before you run Which check fails if the program changes the original start Dictionary during the pickup?
Reveal the answer
Keep input state unchanged would fail, and other cases that reuse start might fail too.
Complete checkpoint
Six deterministic tests pass. A deliberate wall bug fails the exact wall check and passes again when restored.
main.gd
extends Control
const MAP := ["#####", "#@aA#", "#..x#", "#####"]
const EAST := Vector2i(1, 0)
const SOUTH := Vector2i(0, 1)
const WEST := Vector2i(-1, 0)
var report: Label
var details: Label
var simulate_bug := false
func _ready() -> void:
label("Midnight Deck Dash / Turn rule tests", 29)
report = label("", 84, 24)
details = label("", 147, 19)
button("Run rule checks", 52, 435, run_checks, 200)
button("Toggle wall bug", 268, 435, toggle_bug, 200)
run_checks()
func tile(cell: Vector2i) -> String:
if cell.x < 0 or cell.y < 0 or cell.y >= MAP.size() or cell.x >= MAP[cell.y].length():
return "#"
return MAP[cell.y][cell.x]
func turn(previous: Dictionary, direction: Vector2i) -> Dictionary:
var next := previous.duplicate()
var target: Vector2i = previous["cell"] + direction
var symbol := tile(target)
if symbol == "#" and not simulate_bug:
return next
next["cell"] = target
next["turns"] += 1
if symbol == "a" and next["parcel"] == "waiting":
next["parcel"] = "carried"
elif symbol == "A" and next["parcel"] == "carried":
next["parcel"] = "delivered"
elif symbol == "x" and next["parcel"] == "delivered":
next["finished"] = true
return next
func toggle_bug() -> void:
simulate_bug = not simulate_bug
run_checks()
func run_checks() -> void:
var start := {"cell": Vector2i(1, 1), "turns": 0, "parcel": "waiting", "finished": false}
var wall := turn(start, WEST)
var pickup := turn(start, EAST)
var deliver := turn(pickup, EAST)
var finish := turn(deliver, SOUTH)
var empty_station := turn({"cell": Vector2i(2, 1), "turns": 0, "parcel": "waiting", "finished": false}, EAST)
var checks := {
"Block a wall without spending a turn": wall["cell"] == start["cell"] and wall["turns"] == 0,
"Pick up the parcel": pickup["parcel"] == "carried",
"Keep input state unchanged": start["parcel"] == "waiting" and start["turns"] == 0,
"Do not deliver an empty hand": empty_station["parcel"] == "waiting",
"Deliver at Station A": deliver["parcel"] == "delivered",
"Reach the finish": finish["finished"] and finish["turns"] == 3
}
var passed := 0
var rows := PackedStringArray()
for test in checks:
if checks[test]:
passed += 1
rows.append(("%s %s" % ["PASS" if checks[test] else "FAIL", test]))
report.text = "%d / %d checks pass" % [passed, checks.size()]
details.text = "
".join(rows)
func label(text: String, y: float, font_size: int = 25) -> Label:
var new_label := Label.new()
new_label.text = text
new_label.position = Vector2(52, y)
new_label.add_theme_font_size_override("font_size", font_size)
add_child(new_label)
return new_label
func button(text: String, x: float, y: float, action: Callable, width: float = 214.0) -> Button:
var control := Button.new()
control.text = text
control.position = Vector2(x, y)
control.custom_minimum_size = Vector2(width, 48)
add_child(control)
control.pressed.connect(action)
return control
func _draw() -> void:
draw_rect(Rect2(Vector2.ZERO, size), Color("#112632"))
GDScript line by line
Follow the line numbers, including the indent at the start of each line. Find the code the game will run first, then look for the function that reacts when you tap or change something.
Read main.gd line by line
-
extends ControlAttach this script to a Control so the checkpoint can draw, size itself, place buttons and respond to input. -
const MAP := ["#####", "#@aA#", "#..x#", "#####"]Creates a constant named MAP. The expression makes a list whose items keep their order. Godot infers the type from the first value. -
const EAST := Vector2i(1, 0)Creates a constant named EAST. The expression packs whole-number x and y into one grid position. Godot infers the type from the first value. -
const SOUTH := Vector2i(0, 1)Creates a constant named SOUTH. The expression packs whole-number x and y into one grid position. Godot infers the type from the first value. -
const WEST := Vector2i(-1, 0)Creates a constant named WEST. The expression packs whole-number x and y into one grid position. Godot infers the type from the first value. -
var report: LabelDeclares report for later use. A colon followed by a type says which kind of value belongs here. -
var details: LabelDeclares details for later use. A colon followed by a type says which kind of value belongs here. -
var simulate_bug := falseCreates a variable named simulate_bug. The expression uses the yes-or-no value false. Godot infers the type from the first value. -
func _ready() -> void:Godot calls this setup function when the scene enters the running tree. void means the function gives the caller no value back. -
label("Midnight Deck Dash / Turn rule tests", 29)Calls label with the values in its parentheses. -
report = label("", 84, 24)Stores the new value in report. The expression calls label with the values in its parentheses. -
details = label("", 147, 19)Stores the new value in details. The expression calls label with the values in its parentheses. -
button("Run rule checks", 52, 435, run_checks, 200)Calls button with the values in its parentheses. -
button("Toggle wall bug", 268, 435, toggle_bug, 200)Calls button with the values in its parentheses. -
run_checks()Calls run_checks with the values in its parentheses. -
func tile(cell: Vector2i) -> String:Defines tile, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect. -
if cell.x < 0 or cell.y < 0 or cell.y >= MAP.size() or cell.x >= MAP[cell.y].length():Checks cell.x < 0 or cell.y < 0 or cell.y >= MAP.size() or cell.x >= MAP[cell.y].length(). The indented instructions below run only if this test passes. -
return "#"Finishes the function and hands its result back to the caller. The expression uses the text inside the quotes. -
return MAP[cell.y][cell.x]Finishes the function and hands its result back to the caller. The expression looks up the item or key written between the square brackets. -
func turn(previous: Dictionary, direction: Vector2i) -> Dictionary:Defines turn, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect. -
var next := previous.duplicate()Copy the dictionary before moving or changing cargo so the existing state stays usable for another test. -
var target: Vector2i = previous["cell"] + directionConstruct an intended destination from the old position and the input vector. -
var symbol := tile(target)Creates a variable named symbol. The expression calls tile with the values in its parentheses. Godot infers the type from the first value. -
if symbol == "#" and not simulate_bug:Give a wall an early exit before movement and the turn counter, except during a deliberate fault demonstration. -
return nextFinishes the function and hands its result back to the caller. The expression reads next. -
next["cell"] = targetStores the new value in next["cell"]. The expression reads target. -
next["turns"] += 1Add the turn only after the wall guard accepted this movement. -
if symbol == "a" and next["parcel"] == "waiting":Checks symbol == "a" and next["parcel"] == "waiting". The indented instructions below run only if this test passes. -
next["parcel"] = "carried"Stores the new value in next["parcel"]. The expression uses the text inside the quotes. -
elif symbol == "A" and next["parcel"] == "carried":If the earlier branches did not run, checks symbol == "A" and next["parcel"] == "carried" next. -
next["parcel"] = "delivered"Stores the new value in next["parcel"]. The expression uses the text inside the quotes. -
elif symbol == "x" and next["parcel"] == "delivered":If the earlier branches did not run, checks symbol == "x" and next["parcel"] == "delivered" next. -
next["finished"] = trueStores the new value in next["finished"]. The expression uses the yes-or-no value true. -
return nextFinishes the function and hands its result back to the caller. The expression reads next. -
func toggle_bug() -> void:Defines toggle bug, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect. -
simulate_bug = not simulate_bugStores the new value in simulate_bug. The expression reads not simulate_bug. -
run_checks()Calls run_checks with the values in its parentheses. -
func run_checks() -> void:Defines run checks, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect. -
var start := {"cell": Vector2i(1, 1), "turns": 0, "parcel": "waiting", "finished": false}Creates a variable named start. The expression makes a dictionary that looks up values by key. Godot infers the type from the first value. -
var wall := turn(start, WEST)Creates a variable named wall. The expression calls turn with the values in its parentheses. Godot infers the type from the first value. -
var pickup := turn(start, EAST)Creates a variable named pickup. The expression calls turn with the values in its parentheses. Godot infers the type from the first value. -
var deliver := turn(pickup, EAST)Creates a variable named deliver. The expression calls turn with the values in its parentheses. Godot infers the type from the first value. -
var finish := turn(deliver, SOUTH)Creates a variable named finish. The expression calls turn with the values in its parentheses. Godot infers the type from the first value. -
var empty_station := turn({"cell": Vector2i(2, 1), "turns": 0, "parcel": "waiting", "finished": false}, EAST)Start next to Station A without carrying a parcel and test what arriving at the station changes. -
var checks := {Creates a variable named checks. The expression makes a dictionary that looks up values by key. Godot infers the type from the first value. -
"Block a wall without spending a turn": wall["cell"] == start["cell"] and wall["turns"] == 0,Continues the same instruction on a new line. This is one of the arguments, data entries or expressions started above. -
"Pick up the parcel": pickup["parcel"] == "carried",Continues the same instruction on a new line. This is one of the arguments, data entries or expressions started above. -
"Keep input state unchanged": start["parcel"] == "waiting" and start["turns"] == 0,Continues the same instruction on a new line. This is one of the arguments, data entries or expressions started above. -
"Do not deliver an empty hand": empty_station["parcel"] == "waiting",Continues the same instruction on a new line. This is one of the arguments, data entries or expressions started above. -
"Deliver at Station A": deliver["parcel"] == "delivered",Continues the same instruction on a new line. This is one of the arguments, data entries or expressions started above. -
"Reach the finish": finish["finished"] and finish["turns"] == 3Continues the same instruction on a new line. This is one of the arguments, data entries or expressions started above. -
}Closes the parentheses, list or dictionary opened above. Any trailing comma separates this item from the next. -
var passed := 0Creates a variable named passed. The expression uses the whole number 0. Godot infers the type from the first value. -
var rows := PackedStringArray()Creates a variable named rows. The expression calls PackedStringArray with the values in its parentheses. Godot infers the type from the first value. -
for test in checks:Repeats the indented instructions for each value in checks. -
if checks[test]:Checks checks[test]. The indented instructions below run only if this test passes. -
passed += 1Reads passed, adds to its old value, then stores the result back in the same place. -
rows.append(("%s %s" % ["PASS" if checks[test] else "FAIL", test]))Format PASS or FAIL followed by the check name and keep it as one row in the results list. -
report.text = "%d / %d checks pass" % [passed, checks.size()]Updates report.text, which controls the words a player sees. The expression uses the text inside the quotes. -
details.text = "Updates details.text, which controls the words a player sees. The expression uses the text inside the quotes. -
".join(rows)Continues this part of the script. Read the expression on this line in the context of the nearby declaration or function. -
func label(text: String, y: float, font_size: int = 25) -> Label:Defines label, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect. -
var new_label := Label.new()Create a Godot node for visible text. The colon and equals together ask GDScript to infer the type from Label.new(). -
new_label.text = textCopy the supplied string into the actual label so the learner can read it. -
new_label.position = Vector2(52, y)Use a fixed 52-pixel left margin and the caller's chosen height in local design pixels. -
new_label.add_theme_font_size_override("font_size", font_size)Give this label the caller's text size. Other labels can keep their own sizes. -
add_child(new_label)Attach the Label to the scene root so Godot can draw and manage it. -
return new_labelGive the caller this particular label so a later delivery or move can change its words. -
func button(text: String, x: float, y: float, action: Callable, width: float = 214.0) -> Button:Defines button, a reusable set of instructions. Parameters between the parentheses receive inputs. A return type after the arrow tells you which kind of result to expect. -
var control := Button.new()Create a GUI button. It can respond to pointer taps and to the keyboard when focused. -
control.text = textShow the caller's chosen action name on the new button. -
control.position = Vector2(x, y)Place the action at the coordinates passed by the caller. -
control.custom_minimum_size = Vector2(width, 48)Ask the Control layout for at least the supplied width and 48 design pixels of height. -
add_child(control)Connect the new button to the visible scene root. -
control.pressed.connect(action)Connect the Godot pressed signal to the Callable received as action. The action runs on a press later. -
return controlReturn this button so the caller can customise it or keep its reference. -
func _draw() -> void:Godot calls this when the node needs drawing. The instructions inside paint the current state. -
draw_rect(Rect2(Vector2.ZERO, size), Color("#112632"))Draw the full dark blue practice backdrop from local (0, 0) to the current size. Shapes drawn later can appear above it.
Find and fix a bug
If the wall check stays green with the wall bug turned on, check it compares both cell and turns. Stepping into a wall should do neither.
Try a change
Ages 10–12 can add a check that trying the exit too early leaves finished false. Ages 13–16 can add a cart patrol fixture with a Wait move and compare two expected patrol positions.