Skip to content

Keep Cipher

Save the complete game and test a fresh start, replay and quit.

  • Sinclair ZX Spectrum
  • BASIC

Keep the finished program from the previous lesson. There are no new BASIC lines here. Save a tape that starts at the title, then check it in a fresh machine.

Complete Cipher source
cipher.bas
  10 BORDER 0: PAPER 0: INK 7: BRIGHT 0: CLS
  20 RANDOMIZE
  30 PRINT AT 3,10; INK 5; BRIGHT 1;"C I P H E R"
  40 PRINT AT 7,3;"Find the hidden word."
  50 PRINT AT 9,2;"Reveal every matching letter."
  60 PRINT AT 11,3;"Seven mistakes per word."
  70 PRINT AT 13,3;"Repeat guesses cost nothing."
  80 PRINT AT 17,3;"S starts. Q quits."
  90 GO SUB 8000
 100 IF k$="q" THEN STOP
 110 IF k$<>"s" THEN GO TO 90
 200 LET wins=0: LET losses=0: LET round=0: LET last=0: LET total=24
 210 LET pick=INT (RND*total)+1
 220 IF pick=last THEN GO TO 210
 230 LET last=pick: LET round=round+1: RESTORE 9000
 240 FOR i=1 TO pick: READ w$: NEXT i
 250 LET d$="": LET t$="": LET m$="": LET left=7: LET u$="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
 260 FOR i=1 TO LEN w$: LET d$=d$+"_": NEXT i
 270 LET a$="Choose a letter.": GO SUB 2000
 300 GO SUB 2500
 310 GO SUB 8000
 320 IF CODE k$=13 THEN GO TO 600
 330 LET c=CODE k$
 340 IF c<97 OR c>122 THEN GO TO 310
 350 LET g$=CHR$ (c-32): LET seen=0
 355 IF t$="" THEN GO TO 390
 360 FOR i=1 TO LEN t$: IF t$(i)=g$ THEN LET seen=1
 370 NEXT i
 380 IF seen=1 THEN LET a$=g$+" already tried. No cost.": GO TO 300
 390 LET t$=t$+g$: LET found=0: LET u$(c-96 TO c-96)="."
 395 PRINT AT 14+INT ((c-97)/13),3+2*((c-97)-13*INT ((c-97)/13));"."
 400 FOR i=1 TO LEN w$
 410 IF w$(i)=g$ THEN LET d$(i TO i)=g$: LET found=found+1
 420 NEXT i
 430 IF found>0 THEN LET a$=g$+" reveals "+STR$ found+" letter(s)."
 440 IF found=0 THEN LET left=left-1: LET m$=m$+g$: LET a$=g$+" is not in the word."
 450 IF d$=w$ THEN LET wins=wins+1: LET a$="Word found!": GO TO 700
 460 IF left=0 THEN LET losses=losses+1: LET a$="No guesses left. Word revealed.": GO TO 700
 470 GO TO 300
 600 CLS : PRINT AT 3,10; INK 5; BRIGHT 1;"C I P H E R"
 610 PRINT AT 7,3;"PAUSED - word stays hidden."
 620 PRINT AT 10,3;"C continues this word."
 630 PRINT AT 12,3;"R resets words and scores."
 640 PRINT AT 14,3;"Q quits to BASIC."
 650 GO SUB 8000
 660 IF k$="q" THEN STOP
 670 IF k$="r" THEN GO TO 200
 680 IF k$="c" THEN GO SUB 2000: GO TO 300
 690 GO TO 650
 700 GO SUB 2000
 710 PRINT AT 6,16-LEN w$; INK 4; BRIGHT 1;
 720 FOR i=1 TO LEN w$: PRINT w$(i);" ";: NEXT i
 730 IF left=0 THEN PRINT AT 6,16-LEN w$; INK 6; BRIGHT 1;: FOR i=1 TO LEN w$: PRINT w$(i);" ";: NEXT i
 740 PRINT AT 21,1;"SPACE next. R resets. Q quits. "
 750 GO SUB 8000
 760 IF k$="q" THEN STOP
 770 IF k$="r" THEN GO TO 200
 780 IF k$=" " THEN GO TO 210
 790 GO TO 750
2000 CLS
2010 PRINT AT 1,2; INK 5; BRIGHT 1;"CIPHER";AT 1,19;"WORD ";round
2020 PRINT AT 3,2;"Won ";wins;"   Lost ";losses
2110 PRINT AT 12,2; INK 5;"UNTRIED LETTERS"
2120 FOR j=1 TO 26
2160 PRINT AT 14+INT ((j-1)/13),3+2*((j-1)-13*INT ((j-1)/13)); INK 7;
2170 PRINT u$(j);
2190 NEXT j
2200 PRINT AT 21,1;"Letter guesses. ENTER pauses."
2205 GO SUB 2500
2210 RETURN
2500 PRINT AT 6,16-LEN d$; INK 7; BRIGHT 1;
2510 FOR i=1 TO LEN d$: PRINT d$(i);" ";: NEXT i
2520 PRINT AT 9,2; INK 6;"MISTAKES LEFT: ";left
2530 PRINT AT 10,2; INK 6;
2540 FOR i=1 TO 7
2550 IF i<=left THEN PRINT "O ";
2560 IF i>left THEN PRINT ". ";
2570 NEXT i
2580 PRINT AT 17,2;"Misses: ";m$
2590 LET b$=a$+"                              ": PRINT AT 19,1; INK 6;b$( TO 30)
2600 RETURN
8000 LET k$=INKEY$
8010 IF INKEY$<>"" THEN GO TO 8010
8020 LET k$=INKEY$: IF k$="" THEN GO TO 8020
8030 IF CODE k$>=65 AND CODE k$<=90 THEN LET k$=CHR$ (CODE k$+32)
8040 RETURN
9000 DATA "WINDOW","BOTTLE","KETTLE","LADDER","RABBIT","GARDEN"
9010 DATA "POCKET","CANDLE","PILLOW","BUTTON","BASKET","BRIDGE"
9020 DATA "ORANGE","SILVER","HAMMER","ROCKET","JACKET","FOREST"
9030 DATA "CAMERA","MARKET","CARPET","SHELL","TUNNEL","STREAM"

Pause with Enter and choose Q to return to BASIC. Enter this direct command, with no line number:

BASIC
SAVE "cipher" LINE 10

LINE 10 records the starting line for a later load. At the tape prompt, press Enter and wait for the 0 OK report. In Emu198x, export the recorded tape to a new .tap file using Tape → Export Recording… or Cmd/Ctrl+Shift+E. Choose a new filename: export does not overwrite an existing file. The Meet BASIC saving guide explains the distinction between recording inside the machine and keeping a file on the host.

Open a fresh 48K machine, insert that exported tape and enter LOAD "", or use the emulator’s tape autoload. Do not rely on a program already sitting in memory. The title should appear. S must start a new session with zero wins and losses and seven mistakes.

Check a complete cycle

Use these checks together:

  • Guess a letter. Every matching position should appear; a miss should reduce the allowance once.
  • Repeat that same guess. Neither the count nor the miss list should change again.
  • Try a digit and Space during play. Neither is a letter guess.
  • Pause with Enter, then C. The word, revealed letters, misses and count must survive.
  • Finish a word or spend all seven mistakes. The answer and result should remain until another key is chosen.
  • Press Space for another word. Keep the score, clear the histories and restore seven mistakes.
  • Use R in a menu to reset, then Q in a menu to quit. During play, Q and R must still be usable letters.

Does loading this saved tape resume a partly played word?

Show the explanation

No. Auto-start at line 10 runs the title and S reaches the session initialisation at 200. That clears scores and chooses a word. This procedure saves a reusable program, not a resumable game session.

Inspect the relationships

The hidden word, revealed word, attempt history and available alphabet are separate strings because they answer different questions. A repeated guess changes none of those records. A new matching guess changes knowledge; a new miss changes the remaining allowance. The display follows those stored facts.

A useful next experiment is to trace two possible guesses on paper before playing them. Which positions might they reveal? Which candidate words would remain? The program knows its answer, but the player’s uncertainty is what makes the choice interesting. Keep the source and exported tape together so you can return to the working game after an experiment.

Sources

Steven Vickers, edited by Robin Bradbeer, ZX Spectrum BASIC Programming, second edition (Sinclair Research, 1983), chapter 20.

Run it here

This unit's program, built for you.

Program
cipher.tap
Machine
ZX Spectrum 48K (PAL)

Download cipher.tap