Remember an order
Store panel choices as text, find individual items and extend the order without losing its beginning.
Our four signals can identify choices. A memory game needs to keep an order of choices too: panel 3, then panel 1, then panel 4 is different from 1, 3, 4. We’ll store that order and examine it on screen before asking the cue routine to play it.
Continue with the complete program from Give a choice a signal,
or load the spark version saved there. That lesson explains the board and cue
routines used here. We’ll keep both routines while replacing the automatic
1–4 demonstration with a small display of the stored data.
Keyboard notes for this lesson
In extended mode, K gives LEN. Left Option/Alt+8 and +9 enter brackets;
left Option/Alt+K enters +.
Keep three choices in one string
A string holds characters in order. Use s$ to hold "314": three characters,
each identifying one panel. The dollar sign makes s$ a string variable;
s without it would be a different, numeric variable.
Replace 280 and 330, add 290, and delete 300, 310 and 320:
280 LET s$="314"
290 PRINT AT 18,0;"Order: ";s$
330 STOP
Keep every other line.
The quotes tell BASIC where the text begins and ends; they are not part of the
stored order. PRINT AT 18,0 puts the diagnostic below the panels. The semicolon
continues printing immediately after Order: , whose final space separates the
label from the value.
Run it. The board stays at rest, with Order: 314 underneath and the intentional
9 STOP statement report below. There are no cues in this checkpoint: line 330
stops before execution can fall into the drawing routine.
Complete program with a stored order
10 BORDER 0
20 PAPER 0
30 INK 7
40 CLS
50 PRINT "BRIGHT SPARK"
100 LET lit=0
110 FOR p=1 TO 4
120 GO SUB 500
130 NEXT p
240 PRINT AT 2,7;"1"
250 PRINT AT 2,23;"2"
260 PRINT AT 11,7;"3"
270 PRINT AT 11,23;"4"
280 LET s$="314"
290 PRINT AT 18,0;"Order: ";s$
330 STOP
500 LET pr=3
510 LET pc=2
520 LET colour=2
530 LET inkcol=7
540 LET note=0
550 IF p=2 THEN LET pc=18
560 IF p=2 THEN LET colour=1
570 IF p=2 THEN LET note=4
580 IF p=3 THEN LET pr=12
590 IF p=3 THEN LET colour=4
600 IF p=3 THEN LET inkcol=0
610 IF p=3 THEN LET note=7
620 IF p=4 THEN LET pr=12
630 IF p=4 THEN LET pc=18
640 IF p=4 THEN LET colour=6
650 IF p=4 THEN LET inkcol=0
660 IF p=4 THEN LET note=12
670 PAPER colour
680 INK inkcol
690 BRIGHT lit
700 FOR r=pr TO pr+5
710 PRINT AT r,pc;" "
720 NEXT r
770 IF lit=1 THEN PRINT AT pr+2,pc+5;"*"
780 PAPER 0
790 INK 7
800 BRIGHT 0
810 RETURN
900 LET lit=1
910 GO SUB 500
920 BEEP 0.15,note
930 LET lit=0
940 GO SUB 500
950 PAUSE 10
960 RETURN
We could keep a separate variable for each choice, but then every extra position
would need another name. A string already supplies ordered positions. Our chosen
representation has a useful constraint: one character means one choice. It
works for panels 1–4. If we added panel 11, "112" could mean 1, 1, 2 or 11, 2;
we would need a different representation or a way to separate identifiers.
Ask how many, then which one
Add 300, 310 and 320:
300 PRINT AT 19,0;"Length: ";LEN s$
310 PRINT AT 20,0;"First: ";s$(1)
320 PRINT AT 21,0;"Last: ";s$(LEN s$)
LEN s$ gives the number of characters in s$. It is a function:
it works out a value that PRINT can use. It does not change the string.
s$(1) selects the character at position 1. Sinclair BASIC counts string positions
from 1, so the first character here is "3".
| Position | Character | Panel it identifies |
|---|---|---|
| 1 | "3" |
3 |
| 2 | "1" |
1 |
| 3 | "4" |
4 |
The position and the panel number answer different questions. Position 1 says where to look in the order; the character there says which panel to use.
s$(LEN s$) combines the operations. BASIC finds the length, 3, then selects
position 3. That gives "4", the last character. This expression keeps finding
the last item when we change the length, provided the string is not empty.
Only select positions that exist: for this three-character order, use 1–3.
Run it. Beneath Order: 314, the display should read Length: 3, First: 3
and Last: 4. These diagnostics occupy rows 18–21, leaving the panel labels
and rectangles untouched.
The first character and the length both print as 3. Are they the same kind of value?
Show the explanation
No. s$(1) is the one-character string "3"; LEN s$ is the number 3.
Printing either produces the same visible digit. The distinction matters when
we join text or perform arithmetic. Our cue routine expects a numeric panel
identifier; we’ll convert a stored digit when we build playback.
Complete program with length and position diagnostics
10 BORDER 0
20 PAPER 0
30 INK 7
40 CLS
50 PRINT "BRIGHT SPARK"
100 LET lit=0
110 FOR p=1 TO 4
120 GO SUB 500
130 NEXT p
240 PRINT AT 2,7;"1"
250 PRINT AT 2,23;"2"
260 PRINT AT 11,7;"3"
270 PRINT AT 11,23;"4"
280 LET s$="314"
290 PRINT AT 18,0;"Order: ";s$
300 PRINT AT 19,0;"Length: ";LEN s$
310 PRINT AT 20,0;"First: ";s$(1)
320 PRINT AT 21,0;"Last: ";s$(LEN s$)
330 STOP
500 LET pr=3
510 LET pc=2
520 LET colour=2
530 LET inkcol=7
540 LET note=0
550 IF p=2 THEN LET pc=18
560 IF p=2 THEN LET colour=1
570 IF p=2 THEN LET note=4
580 IF p=3 THEN LET pr=12
590 IF p=3 THEN LET colour=4
600 IF p=3 THEN LET inkcol=0
610 IF p=3 THEN LET note=7
620 IF p=4 THEN LET pr=12
630 IF p=4 THEN LET pc=18
640 IF p=4 THEN LET colour=6
650 IF p=4 THEN LET inkcol=0
660 IF p=4 THEN LET note=12
670 PAPER colour
680 INK inkcol
690 BRIGHT lit
700 FOR r=pr TO pr+5
710 PRINT AT r,pc;" "
720 NEXT r
770 IF lit=1 THEN PRINT AT pr+2,pc+5;"*"
780 PAPER 0
790 INK 7
800 BRIGHT 0
810 RETURN
900 LET lit=1
910 GO SUB 500
920 BEEP 0.15,note
930 LET lit=0
940 GO SUB 500
950 PAUSE 10
960 RETURN
Extend the order without replacing its beginning
Add 285, between the assignment and the first diagnostic:
285 LET s$=s$+"2"
For strings, + joins the characters together;
this is called concatenation. BASIC reads the existing s$, joins "2"
to its end, then stores the resulting string back in s$.
It does not add the numbers 314 and 2. Both operands are text, so "314"+"2"
becomes "3142". The original three choices keep their order.
Before running, predict the four diagnostic values.
What changes after appending the choice?
Show the explanation
The order becomes 3142, its length becomes 4, and the last item becomes 2.
The first item stays 3. Line 320 needs no edit: LEN s$ now supplies position
4 instead of 3.
Run and compare. Then run again. The order should still be 3142, because line
280 starts from "314" on every run before line 285 appends once. A game that
grows across rounds will need to initialise its sequence once per game and
extend it after each successful round; these are different jobs.
Try replacing the quoted "2" in line 285 with "4". Predict the last item
and length, then run. The order becomes 3144: repeated choices are allowed,
and there are still four positions. Restore "2" afterwards.
If the length remains 3, check that line 285 exists and comes before the
printing lines. If the first item changes, check which side of + holds s$:
putting the new character first prepends it instead of appending it.
Complete program to keep
10 BORDER 0
20 PAPER 0
30 INK 7
40 CLS
50 PRINT "BRIGHT SPARK"
100 LET lit=0
110 FOR p=1 TO 4
120 GO SUB 500
130 NEXT p
240 PRINT AT 2,7;"1"
250 PRINT AT 2,23;"2"
260 PRINT AT 11,7;"3"
270 PRINT AT 11,23;"4"
280 LET s$="314"
285 LET s$=s$+"2"
290 PRINT AT 18,0;"Order: ";s$
300 PRINT AT 19,0;"Length: ";LEN s$
310 PRINT AT 20,0;"First: ";s$(1)
320 PRINT AT 21,0;"Last: ";s$(LEN s$)
330 STOP
500 LET pr=3
510 LET pc=2
520 LET colour=2
530 LET inkcol=7
540 LET note=0
550 IF p=2 THEN LET pc=18
560 IF p=2 THEN LET colour=1
570 IF p=2 THEN LET note=4
580 IF p=3 THEN LET pr=12
590 IF p=3 THEN LET colour=4
600 IF p=3 THEN LET inkcol=0
610 IF p=3 THEN LET note=7
620 IF p=4 THEN LET pr=12
630 IF p=4 THEN LET pc=18
640 IF p=4 THEN LET colour=6
650 IF p=4 THEN LET inkcol=0
660 IF p=4 THEN LET note=12
670 PAPER colour
680 INK inkcol
690 BRIGHT lit
700 FOR r=pr TO pr+5
710 PRINT AT r,pc;" "
720 NEXT r
770 IF lit=1 THEN PRINT AT pr+2,pc+5;"*"
780 PAPER 0
790 INK 7
800 BRIGHT 0
810 RETURN
900 LET lit=1
910 GO SUB 500
920 BEEP 0.15,note
930 LET lit=0
940 GO SUB 500
950 PAUSE 10
960 RETURN
Save this version as spark. The extra text is a diagnostic: it lets us inspect
the order while building the game. Next we’ll replace it with WATCH status and
walk through the stored choices, calling the existing cue routine for each one.
The player will then see and hear the order instead of reading its answer.
Sources
Steven Vickers, edited by Robin Bradbeer, ZX Spectrum BASIC Programming, second
edition (Sinclair Research, 1983), chapter 7 (string expressions and joining text),
chapter 8, p. 51 (string positions and subscripting), and chapter 9, p. 57 (LEN
and extended-mode entry).