GoalPlan, trace and debug a program in which three sprites pass messages one after another.
New words
trace · to follow a program step by step to see exactly what it doesmessage chain · sprites that start one another by sending messages in turnbug · a mistake in an algorithm or a programdebug · to find a bug and fix it
Explanation
Some programs have three or more sprites. Each sprite gets its own script, and messages pass the job from one sprite to the next. The first sprite starts with the green flag and broadcasts a message. The second sprite starts when it receives that message. When it has finished, it broadcasts a new message for the third sprite. To plan a chain, write an algorithm for each sprite and draw an arrow for every message. To check it, trace the program and write down who acts at each moment. If the order is wrong, debug the program. Look for message names that do not match or for a missing broadcast block.
Worked examples
This is the whole chain. Only the rabbit starts with the green flag. The turtle and the snail both start with ‘when I receive’.
Order
Sprite
Action
1
Rabbit
move 50 steps, broadcast pass1
2
Turtle
move 30 steps, broadcast pass2
3
Snail
move 10 steps, say Finished!
A trace table shows the order. The rabbit moves 50 steps, then the turtle moves 30 steps, then the snail moves 10 steps and says ‘Finished!’. The sprites move 50 + 30 + 10 = 90 steps in all.
A missing block is a common bug. Here the turtle’s script has no broadcast block, so the snail never receives pass2. The chain stops after the turtle.
Class activity
Human message chain (4 or more players). Each player is a sprite and holds a script card with three lines: ‘When I hear …’, an action, and ‘Say …’ (broadcast). The first player starts when the teacher says ‘flag’. Run the chain. Then swap two of the cards. What happens? Find the bug, fix it and run it again. If you have a computer or tablet, build the rabbit, turtle and snail program and trace it before you run it.
Practice
1
The three scripts are shown out of order. In which order do the sprites speak?
Sun first, then Hen, then Farmer. The sun starts with the flag and broadcasts wake. The hen receives wake and broadcasts feed. The farmer receives feed.
2
In the same program, at which second does the Farmer start to speak, and when does the whole program end?
The sun speaks from 0 to 2 and then broadcasts wake. The hen speaks from 2 to 3 and then broadcasts feed. The farmer starts at second 3 and speaks until second 5, so the program ends at second 5.
3
Aisha wants the dog to bark when the cat meows, and the bird to fly away when the dog barks. How many messages does she need, and who sends each one?
Two messages. The cat sends the first one (for example meow) and the dog receives it. The dog sends the second one (for example bark) and the bird receives it.
4
The snail moves as soon as the green flag is clicked, not after the turtle. What is the bug?
The snail’s script starts with ‘when the green flag is clicked’. It should start with ‘when I receive (pass2)’.