Lucky Number now has a secret worth finding. We’ll give its clues a clear visual setting, add brief sounds and let the player choose another round.
Continue with Choose a new secret. A fresh entry can use the complete first listing below; the preceding lessons explain the guessing and validation code. For an empty Spectrum in this page, press Run it here at the end of the lesson, then Start.
Make the words easy to read
Add lines 6, 7, 8 and 9:
5 RANDOMIZE
6 BORDER 0
7 PAPER 0
8 INK 7
9 CLS
10 LET secret=INT (RND*10)+1
15 LET tries=0
20 PRINT "Guess a whole number 1 to 10"
30 INPUT "Your guess ";g
31 IF g<1 THEN GO TO 100
32 IF g>10 THEN GO TO 100
33 IF g<>INT g THEN GO TO 100
35 CLS
36 LET tries=tries+1
40 IF g=secret THEN PRINT "Correct!"
50 IF g<secret THEN PRINT "Too low"
60 IF g>secret THEN PRINT "Too high"
70 IF g<>secret THEN GO TO 30
80 PRINT "Guesses: ";tries
90 STOP
100 CLS
110 PRINT "Whole number from 1 to 10"
120 GO TO 30BORDER 0 makes the surrounding border black. PAPER 0 selects black for the
background of characters; INK 7 selects white for their foreground. CLS then
clears the main display using the new paper colour. Changing PAPER alone does
not repaint every existing character cell.
Enter BORDER with B after a line number. For PAPER, press and release
Shift+left Option/Alt, then press left Option/Alt+C. For INK, use the
same extended-mode chord followed by left Option/Alt+X. Release the keys
between chords. CLS is V after the line number.
Black and white provide a clear starting contrast. The ordinary colour numbers
are 0 black, 1 blue, 2 red, 3 magenta, 4 green, 5 cyan, 6 yellow and 7 white.
Try INK 6 in line 8, compare the readability, then restore 7. The player still
receives words for every clue; remembering a colour is not part of the rules.
Let the clue sound as well
Add lines 41, 51 and 61:
5 RANDOMIZE
6 BORDER 0
7 PAPER 0
8 INK 7
9 CLS
10 LET secret=INT (RND*10)+1
15 LET tries=0
20 PRINT "Guess a whole number 1 to 10"
30 INPUT "Your guess ";g
31 IF g<1 THEN GO TO 100
32 IF g>10 THEN GO TO 100
33 IF g<>INT g THEN GO TO 100
35 CLS
36 LET tries=tries+1
40 IF g=secret THEN PRINT "Correct!"
41 IF g=secret THEN BEEP 0.1,12
50 IF g<secret THEN PRINT "Too low"
51 IF g<secret THEN BEEP 0.1,0
60 IF g>secret THEN PRINT "Too high"
61 IF g>secret THEN BEEP 0.1,-12
70 IF g<>secret THEN GO TO 30
80 PRINT "Guesses: ";tries
90 STOP
100 CLS
110 PRINT "Whole number from 1 to 10"
120 GO TO 30BEEP takes two values separated by a comma: duration in seconds, then pitch
in semitones above middle C. Negative pitches are below middle C. Our notes last
one tenth of a second. The too-low clue plays middle C, the too-high clue an
octave below it, and success an octave above it. Think of the wrong-guess sounds
as suggestions to move up or down, alongside the written clues.
After THEN, enter extended mode with Shift+left Option/Alt, release it,
then use left Option/Alt+Z for BEEP. Type 0.1,0, 0.1,-12 or 0.1,12
as shown. A pitch number is not a volume setting.
For a listening test, temporarily use secret 7 in line 10 and guess 4, 9, 7. Expect three short notes with the success note highest. Restore the random line. Repeat with host audio muted: the words must still give all the information. If there is no sound, check the host volume and the emulator’s sound setting (the Sound box in the page’s Spectrum panel) before changing the guessing logic.
BASIC waits while each BEEP plays. A long note would delay the next input;
our short notes acknowledge the result without adding a long pause. This is a
design cost of the sound, not just a choice of tune.
Offer another round
Add lines 81–84. Keep line 90 and the validation handler at 100–120:
5 RANDOMIZE
6 BORDER 0
7 PAPER 0
8 INK 7
9 CLS
10 LET secret=INT (RND*10)+1
15 LET tries=0
20 PRINT "Guess a whole number 1 to 10"
30 INPUT "Your guess ";g
31 IF g<1 THEN GO TO 100
32 IF g>10 THEN GO TO 100
33 IF g<>INT g THEN GO TO 100
35 CLS
36 LET tries=tries+1
40 IF g=secret THEN PRINT "Correct!"
41 IF g=secret THEN BEEP 0.1,12
50 IF g<secret THEN PRINT "Too low"
51 IF g<secret THEN BEEP 0.1,0
60 IF g>secret THEN PRINT "Too high"
61 IF g>secret THEN BEEP 0.1,-12
70 IF g<>secret THEN GO TO 30
80 PRINT "Guesses: ";tries
81 INPUT "Again? y or n ";a$
82 IF a$="y" THEN GO TO 9
83 IF a$="n" THEN GO TO 90
84 GO TO 81
90 STOP
100 CLS
110 PRINT "Whole number from 1 to 10"
120 GO TO 30INPUT stores the reply in a$, a string variable. Line 82 accepts lower-case
y; line 83 accepts lower-case n. Any other answer reaches line 84 and repeats
the prompt. It does not print another upper-screen message, so repeated invalid
replies do not fill the screen or hide the final count.
GO TO 9 clears the previous result, chooses the next secret at line 10 and
resets tries at line 15. It leaves the colour setup and the original seeding
alone. The next random value comes from the existing sequence.
Why would returning to line 30 make a poor new round?
It would keep the same secret and the old count. Line 30 is the next guess within a round; line 9 is the start of a new round. Both are loops, but they repeat different amounts of work.
Play two rounds. Try an invalid replay answer, then y, then finish the second
round and answer n. Expect a fresh count in round two and a 9 STOP report
on exit. That report is our deliberate stop, not a crash. Try a correct first
guess in a labelled fixed-secret test: its count should be 1 in both rounds.

Save with SAVE "lucky" and keep a tape as described in
Remember a name; keep the program.
In the page, the editor below holds the final listing: make it match yours and
press Download tape for lucky.tap. Working locally, use Emu198x’s tape export.
Reopen the saved tape in a fresh Spectrum and check a round.
Opens in your Spectrum.
Edit this listing, then run it. Changes are marked against the original code.
Give someone the game without extra spoken instructions: can they find the range, interpret a clue and leave when they have finished? Use that observation to improve the wording. Lucky Number is now a complete small game.
Next, Choose an answer begins a new program.
Sources
Steven Vickers, edited by Robin Bradbeer, ZX Spectrum BASIC Programming, second edition (Sinclair Research, 1983), chapter 16, “Colours”, pp. 111–116; chapter 19, “BEEP”, pp. 133–138; chapters 2 and 3 for INPUT, comparisons and GO TO.
Run it here
This unit's program, built for you.
- Program
- step-03.tap
- Machine
- ZX Spectrum 48K (PAL)