Skip to content

Choose a letter

Read one key press and reveal every matching position.

  • Sinclair ZX Spectrum
  • BASIC

Let the player supply the guess. This checkpoint keeps BOTTLE as its answer and offers unlimited attempts. It has no mistake counter, score or ending yet; even a fully revealed word stays on screen. Enter quits to BASIC.

Add lines 270, 300, 310, 320, 330, 340, 350, 430, 440, 470, 8000, 8010, 8020, 8030, 8040.

Replace lines 200, 250.

Delete lines 500, 510, 520.

To delete a BASIC line, enter its number on its own.

cipher.bas
  10 BORDER 0: PAPER 0: INK 7: BRIGHT 0: CLS
 200 LET w$="BOTTLE"
 250 LET d$=""
 260 FOR i=1 TO LEN w$: LET d$=d$+"_": NEXT i
 270 LET a$="Choose a letter."
 300 CLS : PRINT AT 3,2;"CIPHER";AT 6,2;d$;AT 10,2;a$;AT 14,2;"Letter guesses. ENTER quits."
 310 GO SUB 8000
 320 IF CODE k$=13 THEN STOP
 330 LET c=CODE k$
 340 IF c<97 OR c>122 THEN GO TO 310
 350 LET g$=CHR$ (c-32): LET found=0
 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 a$=g$+" is not in the word."
 470 GO TO 300
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

RUN, then press T without pressing Enter. Both Ts should appear. Try lowercase and uppercase letters, Z, a digit and Space. The letter Z produces a miss message; the digit and Space leave the reveal alone. Press Enter when you want to edit the program.

Give a subroutine one job

GO SUB 8000 visits the key routine. RETURN resumes the statement after the call. This lets the game ask for a key without putting the keyboard loop into every screen.

INKEY$ reports the current character, or an empty string when none is available. Line 8010 first waits for release of the previous key. Line 8020 then waits for a new character. Holding T should produce one guess, rather than repeatedly processing T while it is down.

The routine returns the character in k$. CODE gives its numeric character code. Uppercase A–Z occupy 65–90; lowercase a–z occupy 97–122. Line 8030 adds 32 to uppercase letters and CHR$ converts that number back to a character. Both cases therefore leave the routine as lowercase.

Enter has code 13 and exits at 320. The OR condition at 340 rejects a code below 97 or above 122. Only a letter reaches 350, where subtracting 32 makes the uppercase guess that matches our stored word.

Reveal without forgetting

The initial underscore loop runs once. After a guess, GO TO 300 returns to drawing and input, not to that initialisation. Correct guesses from earlier turns remain in d$.

Before each search, found returns to zero. The search changes every matching position, then the message uses STR$ to turn the numeric match count into text. A miss changes the message but leaves d$ alone. CLS currently clears and redraws this small display; a later board will update selected fields.

After T, Z, then B, what will the revealed word contain?

Show the explanation

B_TT__. T reveals positions three and four; Z changes none; B reveals position one. Returning to line 250 after each guess would erase earlier knowledge and break this result.

Try a held letter and then release it before choosing another. Complete BOTTLE: this version keeps accepting letters because it has no completion test. Repeating T reports its two matches again; duplicate handling belongs to the next checkpoint.

Keyboard notes

GO SUB uses H and RETURN uses Y in keyword mode. INKEY$ uses N in extended mode; CODE uses I, CHR$ uses U, and STR$ uses Y in extended mode. Enter comparison symbols with Symbol Shift; <> is Symbol Shift with W.

Sources

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

Run it here

This unit's program, built for you.

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

Download cipher.tap