Until You Get It
The loop that runs until something happens, rather than a set number of times. It is the shape of every game.
The counted loop is for when you know how many times. Often you do not. You keep going until something happens: until the player guesses right, runs out of lives, or gives up. You cannot count that in advance.
One guess is not a game
Here is the guessing game from Decisions:
ASK "Guess my number? " INTO guess
IF guess < 7 THEN
SHOW "Too low"
ELSE IF guess > 7 THEN
SHOW "Too high"
ELSE
SHOW "Correct!"
END
Guess my number? 4 Too low
It tells you the guess was low, and then it is over. To guess again you run the whole thing again. That is not a game. It is a single question.
Repeat until you get it
REPEAT
ASK "Guess my number? " INTO guess
IF guess < 7 THEN
SHOW "Too low"
ELSE IF guess > 7 THEN
SHOW "Too high"
END
UNTIL guess = 7
SHOW "Correct! Well done."
Everything between REPEAT and UNTIL happens over and over. At the bottom the computer
asks its question. If the answer is no, it goes back to the top and runs the whole block
again. Only when the answer is yes does it carry on to the line below.
Play it. Guess 4, then 9, then 7:
Guess my number? 4 Too low Guess my number? 9 Too high Guess my number? 7 Correct! Well done.
That shape is the game loop: do the round, check whether it is over, and go round again if it is not. Every game you build has one beating at its centre. This one is small.
A secret worth keeping
The answer is always seven. Play twice and you know it, which makes this a quiz with one question rather than a game.
RANDOM gives you a number you cannot predict:
LET secret = RANDOM 1 TO 10
REPEAT
ASK "Guess my number? " INTO guess
IF guess < secret THEN
SHOW "Too low"
ELSE IF guess > secret THEN
SHOW "Too high"
END
UNTIL guess = secret
SHOW "Correct! Well done."
RANDOM 1 TO 10 picks a whole number from 1 to 10, and both ends can come up. It uses the
same range that FOR does, so there is nothing new to read.
Notice the tests changed too. They ask about secret now rather than about 7, because
nobody knows what the number is any more, including the person who wrote the program. That
is the point: the program works for any secret, and the secret is different every time.
When it’s wrong, see why
- It never stops. The question at the bottom never comes back yes, so the loop can never leave. Check that the winning case does make it true. On most machines you can stop a runaway program by hand, but it is better not to write one.
- It stops after one go. Something outside the loop ended the program, or the work you
meant to repeat sits below the
UNTILrather than above it. - It leaves at the wrong moment. Read the question out loud. “Until the guess is seven” leaves exactly when the guess is seven, which is what you want. Turn it around and the loop either never runs twice or never stops.
What you’ve learnt
- A loop-until repeats until something becomes true, for when you do not know how many times in advance.
- The question sits at the bottom, so the work always happens at least once.
- That do-it, check, go-round-again shape is the game loop, and it is the centre of every game.
- A loop that can never leave is the failure to watch for. The way out has to be reachable.
What’s next
This loop asks its question at the bottom, after the work. In Unit 3 we meet the one that asks first, which can decide not to do the work at all.