London Market 41 · Design a shift and prove it can be won

Market Day London · Lesson 41 of 50 · Publishing · About 45 min

Nia has one order for an apple and a flower. There is only one opening between the counter and the stalls. Check what happens if rain closes that opening.

Watercolour of Nia carrying a basket past the produce, bread and flower stalls at a London street market.
What you’ll make Draw a market map, check that its stalls can be reached and choose a fair number of turns for a collection order.

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/40 when the lesson builds on prior work, or copy the checkpoints/41 folder to try this lesson on its own. Read the file names in the steps before editing. Keep the checkpoint ZIP intact and make edits in an extracted working copy.

Sketch. Mark tiles and target stalls. → Encode. Remember position and basket. → Search. Try turns using a queue. → Playtest. Check the real shift and par.
Sketch → Encode → Search → Playtest
Godot checkpoint window for Design a shift and prove it can be won
From the running Godot checkpoint.

Build it step by step

Language and tools in this lesson Vector3i, Array, Dictionary, bit flags, breadth-first search.

  1. Draw the plan

    Use seven columns and six rows. Mark C for the collection counter, A for apples, F for flowers, B for bakery, # for a blocked tile and . for open ground. Label row and column zero before the first square.

    Why it works A repeatable plan lets you check the same coordinates on paper, in code and in the running game.

  2. Open the map lab

    Import checkpoint 41 in Godot, open main.gd from FileSystem and press F5. Try Open the passage, then Block the passage. Read the result before checking the code.

    Why it works One changed square makes a useful diagnostic. You can compare the two shifts without changing the order.

  3. Describe the search state

    In minimum_turns(), read the Vector3i in first. Its x and y are the path coordinates. Its z records the basket. The flag value 1 means apple, 2 means flower and 3 means both. The | operator sets a flag, and & checks a flag.

    var first := Vector3i(COUNTER.x, COUNTER.y, 0)

    Why it works Being at the same tile with a different basket can lead to a different answer.

  4. Read one legal turn

    Follow successors(). The loop offers four one-square moves on open tiles. The other instructions offer collecting one requested item when Nia reaches a stall. The bakery is part of the map, with no bread requested in this order.

    Why it works Moves and collections each cost one turn. The classroom model focuses on this two-item order.

  5. Check the finish

    Read the win check in minimum_turns(). With both bits set and Nia at C, return the recorded distance plus one for handing the bundle over. Change the blocked square and predict whether a route remains.

    return distance[state] + 1

    Why it works The queue finds the shortest sequence of legal actions, including the final handover.

  6. Build a shift of your own

    Copy the reference game. In levels/shifts.json, duplicate a known working record in your copy or replace one shift while keeping its group. Edit its name, rows, orders, par and brief. An order such as ["apple", "flower"] belongs inside the list of orders. Open the game and try its route hint. Check the measured turn count before adjusting par.

    Why it works The small lab models one order. The complete game also checks its real data, basket capacity and successive orders.

Watch the values change

  1. Open the passage The counter and stalls are connected.
    What you see. A legal service route takes 13 turns.
  2. Close tile (3,2) The only path between the two sections disappears.
    What you see. The solver reports no complete route.
  3. Come back with two items Nia is at C with basket flags 3.
    What you see. The solver counts a handover turn.

Predict before you run If both items are in the basket but Nia is beside the bakery, is the order complete?

Reveal the answer

The order still needs handing over at the collection counter. The search must include returning there.

Complete checkpoint

Open passage: 13 turns. Blocked passage: no complete route.

main.gd

extends Control

const Lab = preload("res://release_lab.gd")
const TITLE = "Design a shift and prove it can be won"
var ui: VBoxContainer

func _ready() -> void:
	ui = VBoxContainer.new()
	ui.set_script(Lab)
	add_child(ui)
	ui.start(TITLE)
	build()

const GRID := ["#######", "#A.F.B#", "###.###", "#.....#", "#..C..#", "#######"]
const COUNTER := Vector2i(3, 4)
const DOOR := Vector2i(3, 2)
const STEPS := [Vector2i.UP, Vector2i.RIGHT, Vector2i.DOWN, Vector2i.LEFT]

func build() -> void:
	ui.say("Nia needs an apple and a flower. Find a complete trip back to C.")
	ui.option("Open the passage", report.bind(false))
	ui.option("Block the passage", report.bind(true))

func report(closed: bool) -> void:
	var turns := minimum_turns(closed)
	if turns < 0:
		ui.say("No complete route. Reopen the passage or redesign the map.")
	else:
		ui.say("Shortest complete route: %d turns, including handover at C." % turns)

func minimum_turns(closed: bool) -> int:
	var first := Vector3i(COUNTER.x, COUNTER.y, 0)
	var pending: Array[Vector3i] = [first]
	var distance := {}
	distance[first] = 0
	while not pending.is_empty():
		var state: Vector3i = pending.pop_front()
		if Vector2i(state.x, state.y) == COUNTER and state.z == 3:
			return distance[state] + 1
		for candidate in successors(state, closed):
			if not distance.has(candidate):
				distance[candidate] = distance[state] + 1
				pending.append(candidate)
	return -1

func successors(state: Vector3i, closed: bool) -> Array[Vector3i]:
	var options: Array[Vector3i] = []
	var position_on_grid := Vector2i(state.x, state.y)
	for step: Vector2i in STEPS:
		var next := position_on_grid + step
		if next.x >= 0 and next.y >= 0 and next.x < 7 and next.y < 6:
			if GRID[next.y][next.x] != "#" and not (closed and next == DOOR):
				options.append(Vector3i(next.x, next.y, state.z))
	var stall: String = GRID[state.y][state.x]
	var bit := 1 if stall == "A" else (2 if stall == "F" else 0)
	if bit != 0 and (state.z & bit) == 0:
		options.append(Vector3i(state.x, state.y, state.z | bit))
	return options

release_lab.gd

extends VBoxContainer

var output: Label

func start(title_text: String) -> void:
	set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
	offset_left = 32.0
	offset_top = 28.0
	offset_right = -32.0
	offset_bottom = -28.0
	add_theme_constant_override("separation", 12)
	var background := ColorRect.new()
	background.color = Color("#20342e")
	background.mouse_filter = Control.MOUSE_FILTER_IGNORE
	get_parent().add_child(background)
	background.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)
	get_parent().move_child(background, 0)
	var title := Label.new()
	title.text = title_text
	title.add_theme_font_size_override("font_size", 32)
	title.add_theme_color_override("font_color", Color("#f3c991"))
	add_child(title)
	output = Label.new()
	output.custom_minimum_size.y = 151.0
	output.autowrap_mode = TextServer.AUTOWRAP_WORD_SMART
	output.add_theme_font_size_override("font_size", 19)
	add_child(output)

func say(message: String) -> void:
	output.text = message

func option(label: String, command: Callable) -> void:
	var button := Button.new()
	button.text = label
	button.custom_minimum_size.y = 46.0
	button.add_theme_font_size_override("font_size", 19)
	button.pressed.connect(command)
	add_child(button)

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
  1. extends ControlStarts with Godot's Control type. This script gets the abilities of that type and adds the game's own rules.
  2. const Lab = preload("res://release_lab.gd")Creates a constant named Lab. The expression prepares a resource at a fixed project path when the script is loaded.
  3. const TITLE = "Design a shift and prove it can be won"Creates a constant named TITLE. The expression uses the text inside the quotes.
  4. var ui: VBoxContainerDeclares ui for later use. A colon followed by a type says which kind of value belongs here.
  5. func _ready() -> void:Godot calls this once after the scene and its children are ready. Use it to set up the room and connect controls.
  6. ui = VBoxContainer.new()Stores the new value in ui. The expression creates a new VBoxContainer object.
  7. ui.set_script(Lab)Calls ui.set_script with the values in its parentheses.
  8. add_child(ui)Adds the new node to the scene tree so that Godot can run and draw it.
  9. ui.start(TITLE)Calls ui.start with the values in its parentheses.
  10. build()Calls build with the values in its parentheses.
  11. const GRID := ["#######", "#A.F.B#", "###.###", "#.....#", "#..C..#", "#######"]Creates a constant named GRID. The expression makes a list whose items keep their order. Godot infers the type from the first value.
  12. const COUNTER := Vector2i(3, 4)Creates a constant named COUNTER. The expression packs whole-number x and y into one grid position. Godot infers the type from the first value.
  13. const DOOR := Vector2i(3, 2)Creates a constant named DOOR. The expression packs whole-number x and y into one grid position. Godot infers the type from the first value.
  14. const STEPS := [Vector2i.UP, Vector2i.RIGHT, Vector2i.DOWN, Vector2i.LEFT]Creates a constant named STEPS. The expression makes a list whose items keep their order. Godot infers the type from the first value.
  15. func build() -> void:Defines build, 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.
  16. ui.say("Nia needs an apple and a flower. Find a complete trip back to C.")Calls ui.say with the values in its parentheses.
  17. ui.option("Open the passage", report.bind(false))Calls ui.option with the values in its parentheses.
  18. ui.option("Block the passage", report.bind(true))Calls ui.option with the values in its parentheses.
  19. func report(closed: bool) -> void:Defines report, 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.
  20. var turns := minimum_turns(closed)Creates a variable named turns. The expression calls minimum_turns with the values in its parentheses. Godot infers the type from the first value.
  21. if turns < 0:Checks turns < 0. The indented instructions below run only if this test passes.
  22. ui.say("No complete route. Reopen the passage or redesign the map.")Calls ui.say with the values in its parentheses.
  23. else:Runs the indented fallback instructions if none of the earlier tests passed.
  24. ui.say("Shortest complete route: %d turns, including handover at C." % turns)Calls ui.say with the values in its parentheses.
  25. func minimum_turns(closed: bool) -> int:Defines minimum turns, 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.
  26. var first := Vector3i(COUNTER.x, COUNTER.y, 0)Creates a variable named first. The expression calls Vector3i with the values in its parentheses. Godot infers the type from the first value.
  27. var pending: Array[Vector3i] = [first]Creates a variable named pending. The expression makes a list whose items keep their order. The type after the colon says what kind of value it can hold.
  28. var distance := {}Creates a variable named distance. The expression makes a dictionary that looks up values by key. Godot infers the type from the first value.
  29. distance[first] = 0Stores the new value in distance[first]. The expression uses the whole number 0.
  30. while not pending.is_empty():Keeps repeating while not pending.is_empty() stays true. The loop body should eventually change the result or leave with break.
  31. var state: Vector3i = pending.pop_front()Creates a variable named state. The expression takes the first item out of the list and returns it. The type after the colon says what kind of value it can hold.
  32. if Vector2i(state.x, state.y) == COUNTER and state.z == 3:Checks Vector2i(state.x, state.y) == COUNTER and state.z == 3. The indented instructions below run only if this test passes.
  33. return distance[state] + 1Finishes the function and hands its result back to the caller. The expression looks up the item or key written between the square brackets.
  34. for candidate in successors(state, closed):Repeats the indented instructions for each value in successors(state, closed).
  35. if not distance.has(candidate):Checks not distance.has(candidate). The indented instructions below run only if this test passes.
  36. distance[candidate] = distance[state] + 1Stores the new value in distance[candidate]. The expression looks up the item or key written between the square brackets.
  37. pending.append(candidate)Adds an item to the end of a list.
  38. return -1Finishes the function and hands its result back to the caller. The expression reads -1.
  39. func successors(state: Vector3i, closed: bool) -> Array[Vector3i]:Defines successors, 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.
  40. var options: Array[Vector3i] = []Creates a variable named options. The expression makes a list whose items keep their order. The type after the colon says what kind of value it can hold.
  41. var position_on_grid := Vector2i(state.x, state.y)Creates a variable named position_on_grid. The expression packs whole-number x and y into one grid position. Godot infers the type from the first value.
  42. for step: Vector2i in STEPS:Repeats the indented instructions for each value in STEPS.
  43. var next := position_on_grid + stepCreates a variable named next. The expression calculates a new value from the expressions on either side of the operator. Godot infers the type from the first value.
  44. if next.x >= 0 and next.y >= 0 and next.x < 7 and next.y < 6:Checks next.x >= 0 and next.y >= 0 and next.x < 7 and next.y < 6. The indented instructions below run only if this test passes.
  45. if GRID[next.y][next.x] != "#" and not (closed and next == DOOR):Checks GRID[next.y][next.x] != "#" and not (closed and next == DOOR). The indented instructions below run only if this test passes.
  46. options.append(Vector3i(next.x, next.y, state.z))Adds an item to the end of a list.
  47. var stall: String = GRID[state.y][state.x]Creates a variable named stall. The expression looks up the item or key written between the square brackets. The type after the colon says what kind of value it can hold.
  48. var bit := 1 if stall == "A" else (2 if stall == "F" else 0)Creates a variable named bit. The expression chooses one of two values by checking the condition in the middle. Godot infers the type from the first value.
  49. if bit != 0 and (state.z & bit) == 0:Checks bit != 0 and (state.z & bit) == 0. The indented instructions below run only if this test passes.
  50. options.append(Vector3i(state.x, state.y, state.z | bit))Adds an item to the end of a list.
  51. return optionsFinishes the function and hands its result back to the caller. The expression reads options.
Read release_lab.gd line by line
  1. extends VBoxContainerStarts with Godot's VBoxContainer type. This script gets the abilities of that type and adds the game's own rules.
  2. var output: LabelDeclares output for later use. A colon followed by a type says which kind of value belongs here.
  3. func start(title_text: String) -> void:Defines start, 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.
  4. set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)Positions this Control relative to the bounds of its parent.
  5. offset_left = 32.0Stores the new value in offset_left. The expression reads 32.0.
  6. offset_top = 28.0Stores the new value in offset_top. The expression reads 28.0.
  7. offset_right = -32.0Stores the new value in offset_right. The expression reads -32.0.
  8. offset_bottom = -28.0Stores the new value in offset_bottom. The expression reads -28.0.
  9. add_theme_constant_override("separation", 12)Calls add_theme_constant_override with the values in its parentheses.
  10. var background := ColorRect.new()Creates a variable named background. The expression creates a new ColorRect object. Godot infers the type from the first value.
  11. background.color = Color("#20342e")Stores the new value in background.color. The expression builds a colour from these values.
  12. background.mouse_filter = Control.MOUSE_FILTER_IGNOREUpdates background.mouse_filter, which controls how this control participates in pointer and touch input. The expression reads Control.MOUSE_FILTER_IGNORE.
  13. get_parent().add_child(background)Calls get_parent with the values in its parentheses.
  14. background.set_anchors_and_offsets_preset(Control.PRESET_FULL_RECT)Positions this Control relative to the bounds of its parent.
  15. get_parent().move_child(background, 0)Calls get_parent with the values in its parentheses.
  16. var title := Label.new()Creates a variable named title. The expression creates a new Label object. Godot infers the type from the first value.
  17. title.text = title_textUpdates title.text, which controls the words a player sees. The expression reads title_text.
  18. title.add_theme_font_size_override("font_size", 32)Calls title.add_theme_font_size_override with the values in its parentheses.
  19. title.add_theme_color_override("font_color", Color("#f3c991"))Calls title.add_theme_color_override with the values in its parentheses.
  20. add_child(title)Adds the new node to the scene tree so that Godot can run and draw it.
  21. output = Label.new()Stores the new value in output. The expression creates a new Label object.
  22. output.custom_minimum_size.y = 151.0Stores the new value in output.custom_minimum_size.y. The expression reads 151.0.
  23. output.autowrap_mode = TextServer.AUTOWRAP_WORD_SMARTStores the new value in output.autowrap_mode. The expression reads TextServer.AUTOWRAP_WORD_SMART.
  24. output.add_theme_font_size_override("font_size", 19)Calls output.add_theme_font_size_override with the values in its parentheses.
  25. add_child(output)Adds the new node to the scene tree so that Godot can run and draw it.
  26. func say(message: String) -> void:Defines say, 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.
  27. output.text = messageUpdates output.text, which controls the words a player sees. The expression reads message.
  28. func option(label: String, command: Callable) -> void:Defines option, 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.
  29. var button := Button.new()Creates a variable named button. The expression creates a new Button object. Godot infers the type from the first value.
  30. button.text = labelUpdates button.text, which controls the words a player sees. The expression reads label.
  31. button.custom_minimum_size.y = 46.0Stores the new value in button.custom_minimum_size.y. The expression reads 46.0.
  32. button.add_theme_font_size_override("font_size", 19)Calls button.add_theme_font_size_override with the values in its parentheses.
  33. button.pressed.connect(command)Links a signal to the function that should run when the event happens.
  34. add_child(button)Adds the new node to the scene tree so that Godot can run and draw it.

Find and fix a bug

If the solver allows crossing the stall separator, check how map[y][x] is used and whether the open-tile test excludes #.

Try a change

Ages 10–12. Find the 13-turn route on paper. Ages 13–16. Add a bread flag to the lab and design a three-item order.