Skip to content
Game 9 Unit 4 of 6 1 hr learning time

A List of Words

Move the word out of the program and into DATA — a whole list of them — then pick one at random with RESTORE and a counted READ. The game becomes data-driven.

67% of Cipher

One hard-coded word is a demo. A game has variety — and the cleanest way to get it is to keep the words as data, separate from the logic that plays them. This is Cipher's headline idea: a list of words in DATA, with a random pick each round.

  10 BORDER 0: PAPER 0: INK 7: CLS
  40 RANDOMIZE
 120 DATA "SPECTRUM","COMPUTER","KEYBOARD","PROGRAM","SCREEN"
 130 DATA "PRINTER","CASSETTE","JOYSTICK","MEMORY","CIRCUIT"
 140 DATA "DISPLAY","LOADING","BORDER","COLOUR","PIXEL"
 150 DATA "BINARY","CURSOR","VOLUME","SYSTEM","BEEPER"
 160 LET total = 20
 180 LET pick = INT (RND * total) + 1
 190 RESTORE
 200 FOR i = 1 TO pick: READ w$: NEXT i
 210 LET d$ = ""
 220 FOR i = 1 TO LEN w$: LET d$ = d$ + "_": NEXT i
 230 LET lives = 7
 240 LET z$ = ""
 250 CLS
 260 INVERSE 1: PRINT AT 0, 0; "       *** CIPHER ***           ": INVERSE 0
 280 PRINT AT 4, 2;
 290 FOR i = 1 TO LEN d$
 300 IF d$(i) = "_" THEN INK 7: PRINT "_ ";
 310 IF d$(i) <> "_" THEN INK 4: PRINT d$(i); " ";
 320 NEXT i
 330 INK 7
 340 PRINT AT 7, 2; "Lives: ";
 350 INK 4: FOR i = 1 TO lives: PRINT "*";: NEXT i
 360 FOR i = lives + 1 TO 7: PRINT " ";: NEXT i
 370 INK 7
 380 PRINT AT 9, 2; "Tried: "; z$; "  "
 390 PRINT AT 12, 2;
 400 INPUT "Guess: "; g$: IF g$ >= "a" AND g$ <= "z" THEN LET g$ = CHR$ (CODE g$ - 32)
 410 LET already = 0
 420 FOR i = 1 TO LEN z$
 430 IF z$(i) = g$ THEN LET already = 1
 440 NEXT i
 450 IF already = 1 THEN PRINT AT 14, 2; INK 6; "Already tried!  ": PAUSE 50: GO TO 250
 460 LET z$ = z$ + g$
 470 LET found = 0
 480 FOR i = 1 TO LEN w$
 490 IF w$(i) = g$ THEN LET d$(i TO i) = g$: LET found = 1
 500 NEXT i
 510 IF found = 0 THEN LET lives = lives - 1: BEEP 0.1, -5
 520 IF found = 1 THEN BEEP 0.1, 10
 530 IF d$ = w$ THEN PRINT "You cracked it!": STOP
 540 IF lives = 0 THEN PRINT "The word was "; w$: STOP
 550 GO TO 250
ZX Spectrum Cipher: a fresh eight-letter word with a couple of letters revealed, chosen at random from the list
A different word each run — chosen from twenty in DATA, not written into the code.

DATA as content

You met DATA and READ in Hi-Lo, where they fed eight bytes into a custom character. Here they do the job they exist for: holding content. Lines 120–150 list twenty words — all computer terms — as DATA, and READ w$ pulls one out. The logic below never mentions a specific word; it works on whatever w$ holds. That separation is the whole point. Add a word to the DATA, bump the total, and the game has new content without a single change to its rules. That is what data-driven means: the content lives apart from the code that runs it.

Picking the n-th word with RESTORE

READ walks the DATA forward, one item per call, and never rewinds on its own — so to pick a random word you reset and re-read. Line 180 rolls pick, a number from 1 to 20. Line 190's RESTORE rewinds the DATA to the start. Line 200 — FOR i = 1 TO pick: READ w$: NEXT i — reads that many words and keeps the last. RESTORE then a counted READ is the standard way to reach the n-th item in a DATA block. It re-reads everything up to your choice, which on twenty words is instant, and RANDOMIZE (line 40) makes the choice fresh each run.

Next: track wins and losses, and play word after word.