Week 25 · Unit 10: Be a game developer · 3 lessons
2
Match code to algorithm
Computing 5: Unit 10, pp. 129–144
GoalCheck line by line that a program matches its algorithm.
New words
algorithm · the steps of a program, planned in order before the code is writtenmatch · to agree with, so that each line of the algorithm has its blockmismatch · a place where the code and the algorithm disagreebug · a mistake in a program
Explanation
We evaluate a program in two steps. First we check that the algorithm is correct and meets the criteria. Then we check that the code matches the algorithm. Go through the algorithm line by line. Find the block for each line, and check the numbers, the names and the order. A block with no line is extra, and a line with no block is missing. Either one is a mismatch, and a mismatch is a bug. Always check the algorithm first, because a mistake in the plan would be copied into the code.
Algorithm line
Block in the code
Go to x: 0, y: −140
go to x: (0) y: (-140)
Change score by 1
change [score v] by (1)
Say ‘You win!’ for 2 seconds
say [You win!] for (2) seconds
Worked examples
Step
Instruction
1
Start when the green flag is clicked
2
Go to x: 0, y: −140
3
Repeat forever
3.1
If the left arrow key is pressed, change x by −15
3.2
If the right arrow key is pressed, change x by 15
This is the algorithm for the Basket sprite in ‘Catch the apple’. The code under it has one block for each line.
Here is the Basket code. Step 3 is the forever block. Steps 3.1 and 3.2 are the two if blocks inside it. Every line has a block and every block has a line, so the code matches.
Step
Instruction
1
Start when the green flag is clicked
2
Set score to 0
3
Go to x: 0, y: 150
4
Repeat until score = 5
4.1
Change y by −10
4.2
If touching Basket, then
4.2.1
Change score by 1
4.2.2
Go to a random x from −200 to 200, y: 150
5
Say ‘You win!’ for 2 seconds
6
Stop all
This is the algorithm for the Apple sprite. Step 4 repeats until the score is 5. Steps 4.2.1 and 4.2.2 happen only when the apple touches the basket.
Here is the Apple code. It matches the algorithm line by line. We have not yet tested it, so we do not know whether it meets all six criteria.
Class activity
Line-up game (pairs, strips of paper). Write a five-line algorithm for a simple sprite on separate strips, for example ‘Start when the space key is pressed’ and ‘Move 20 steps’. Your partner writes a block on a second strip for each line, and then places the block strips beside the algorithm strips. Check each pair for matching numbers. Now secretly change one number on a block strip and swap again. Can your partner find the mismatch?
Practice
1
The algorithm for the Rabbit says: 1 Start when the space key is pressed. 2 Move 10 steps. 3 If touching Carrot, then say ‘Yum!’ for 2 seconds. Which block of this code does not match?
The block ‘move (100) steps’ does not match. The algorithm says 10 steps, so the block should be ‘move (10) steps’.
2
Why do we check the algorithm before we check the code against it?
If the algorithm has a mistake, the code will copy it. Finding the mistake in the plan is quicker than finding it in the code.
3
The algorithm for a Bee says: 1 Start when the green flag is clicked. 2 Go to x: −100, y: 0. 3 Repeat forever: 3.1 If the up arrow key is pressed, change y by 10. 3.2 If the down arrow key is pressed, change y by −10. What is missing from this code?
Step 3.2 is missing. The code has no if block for the down arrow key.
4
Write the algorithm lines for this code.
1 Start when the space key is pressed. 2 Change coins by 2. 3 Say the value of coins for 1 second.