Week 24 · Unit 10: Be a game developer · 3 lessons
3
Does it match the plan?
Computing 4: Unit 10, pp. 138–151
GoalRun a program and compare what happens with what the algorithm says should happen, then find the parts that do not match.
New words
expected result · what the algorithm says should happenactual result · what really happens when the program runstest · to run a program and compare the actual result with the expected result
Explanation
An algorithm is a plan and a program is the plan written in blocks. After we write a program, we test it. First we write down the expected result from the algorithm. Then we run the program and watch the actual result. If the two are the same, the test is passed. If they are different, there is a bug. A program with a sub-routine has several groups of blocks. We can test each group on its own by clicking it. A test table helps us to keep track: it lists what we do, what should happen and what did happen.
Test
Expected result
Actual result
Passed?
press the d key
the duck moves 30 steps right
?
?
Worked examples
The algorithm for a duck says: when the d key is pressed, change x by 30 and run splash. Splash switches to the next costume, plays a pop sound, makes the duck 10 bigger, waits 0.5 seconds and makes it 10 smaller. Here is the program that was written for it. Each line of the algorithm matches one block.
Test
Expected result
Actual result
Passed?
click define splash
next costume, pop sound, size 110, wait, size 100
same
yes
press d once
x is 30, then splash
same
yes
press d three times
x is 90, size is back to 100
same
yes
A test table for the duck program. The duck starts at x = 0 and size 100. The test groups are run one at a time.
Test
Expected result
Actual result
Passed?
press d once
x is 30, then splash
nothing happens, x is 0
no
press s once
nothing happens
x is 30, then splash
no
click define splash
costume, pop, size 110, wait, size 100
same
yes
Now a different program. Its hat block says “when s key pressed”, but the algorithm says the d key. The test table shows a mismatch: pressing d does nothing, and pressing s moves the duck. To fix it, choose d in the hat block and run the tests again.
Class activity
Plan and test (with a friend): one person writes a four-step algorithm for a sprite, for example “move 10 steps, say Go! for 1 second, turn right 90 degrees, move 10 steps”. The other person writes the blocks on paper cards in a row. Check each card against the algorithm. Then the first person secretly changes one number on a card. Can the first person find it by checking card by card? If you have a computer or tablet, build it in Scratch and click each block.
Practice
1
The algorithm says: when the space key is pressed, move 20 steps, say “Go!” for 1 second, then turn right 90 degrees. Find the two blocks that do not match.
The say block says 2 seconds, but the algorithm says 1 second. The turn block turns left (↺), but the algorithm says right (↻).
2
Write the algorithm for this script as a table of steps. What is the sprite’s y position at the end?
1 Start program when the green flag is clicked. 2 Go to x: 0, y: −50. 3 Change y by 30. 4 Wait 0.5 seconds. 5 Repeat steps 3 and 4 two more times. The final y is −50 + 3 × 30 = 40.
3
A program has the groups of blocks in the picture. Which group would you click to test the sub-routine on its own? What should you see?
Click the define [flip] block. The sprite should switch to its next costume, wait 0.1 seconds and switch to the next costume again.
4
In the duck program the duck starts at x = 0 and the d key changes x by 30. What is x after the d key has been pressed 4 times? What would you write as the expected result in a test table?
x is 4 × 30 = 120. The expected result for “press d four times” is x = 120.