AdvertisementYour brand could be hereYour brand hereOn every Darslab page · students, parents and teachersinfo@darslab.comAsk for pricesPricesinfo@darslab.com
☰ Year plan · Computing

Testing against criteria

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 lineBlock in the code
Go to x: 0, y: −140go to x: (0) y: (-140)
Change score by 1change [score v] by (1)
Say ‘You win!’ for 2 secondssay [You win!] for (2) seconds
Worked examples
StepInstruction
1Start when the green flag is clicked
2Go to x: 0, y: −140
3Repeat forever
3.1If the left arrow key is pressed, change x by −15
3.2If 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.
Basketwhenclickedgo to x:0y:-140change x by-15ifkeyleft arrowpressed?thenchange x by15ifkeyright arrowpressed?thenforever
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.
StepInstruction
1Start when the green flag is clicked
2Set score to 0
3Go to x: 0, y: 150
4Repeat until score = 5
4.1Change y by −10
4.2If touching Basket, then
4.2.1Change score by 1
4.2.2Go to a random x from −200 to 200, y: 150
5Say ‘You win!’ for 2 seconds
6Stop 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.
Applewhenclickedsetscoreto0go to x:0y:150change y by-10changescoreby1go to x:pick random-200to200y:150iftouchingBasket?thenrepeat untilscore=5sayYou win!for2secondsstopall
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?
Rabbitwhenspacekey pressedmove100stepssayYum!for2secondsiftouchingCarrot?then
2
Why do we check the algorithm before we check the code against it?
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?
Beewhenclickedgo to x:-100y:0change y by10ifkeyup arrowpressed?thenforever
4
Write the algorithm lines for this code.
Minerwhenspacekey pressedchangecoinsby2saycoinsfor1seconds