Skip to content

Make room for a guess

Draw a board with four spaces ready for an editable guess.

  • Sinclair ZX Spectrum
  • BASIC

Before a lock can judge a guess, the player needs somewhere to enter it. Start with four empty spaces and a drawing routine we can use again.

Use a 48K ZX Spectrum session. Save any program you want to keep before entering NEW, then type this new program. If you need a reminder about entering keyword tokens, use Meet BASIC.

locksmith.bas
  10 BORDER 0: PAPER 0: INK 7: BRIGHT 1: CLS
 200 LET t=1
 220 LET g$="": GO SUB 1000
 240 GO SUB 2000: STOP
1000 CLS
1010 PRINT AT 0,1; INK 5;"LOCKSMITH";AT 0,21; INK 7;"PRACTICE"
1020 PRINT AT 2,1; INK 7;"Build a four-digit guess."
1050 PRINT AT 16,1; INK 5;"------------------------------"
1060 PRINT AT 20,1; INK 7;"Four spaces for four digits."
1080 RETURN
2000 PRINT AT 17,1; INK 5;"TRY ";t;" ";AT 17,7; INK 7;"_ _ _ _"
2020 RETURN

Enter RUN. The board appears, with TRY 1 beside four underscores. The ordinary STOP report at the bottom is expected: this first program draws and stops. It does not read keys yet.

Put the picture in its place

PRINT AT takes a row followed by a column. Both start at zero: row 0 is the top, and column 0 is the left edge. Line 2000 places the editable field on row 17, below the separator on row 16. Starting its underscores at column 7 leaves room for the attempt number.

Semicolons join the items in a PRINT statement without moving automatically to another line. Another AT can place the next item elsewhere. The spaces in "_ _ _ _" separate the four positions without needing four PRINT statements.

Line 10 chooses a black border and paper, white ink and bright colours, then clears the display. The INK values inside PRINT give the heading and separator cyan while the instructions remain white. These local print controls let one statement use more than one colour.

Give each routine a job

t is the attempt number. g$ will hold the guess as a string: an ordered sequence of characters. For now it is empty, written as two quotation marks with nothing between them.

GO SUB 1000 calls the board routine. RETURN at 1080 resumes execution after that call. GO SUB 2000 then draws the editable field, and its RETURN reaches STOP on line 240. The gaps between line numbers leave room for the game we will build around these routines.

Why must the main program stop after drawing the editable field?

Show the explanation

Without STOP, execution would continue into the routine at 1000 as ordinary sequential code. Its RETURN would have no outstanding GO SUB to return from. Calling a routine and falling into it are different paths.

Try changing the initial value of t to 3, then RUN again. TRY 3 should appear without changing the drawing routine. Restore t to 1 afterwards. If the board is displaced, check the order of row and column; if RETURN fails, check line 240 and both GO SUB calls.

Sources

Steven Vickers, edited by Robin Bradbeer, ZX Spectrum BASIC Programming, second edition (Sinclair Research, 1983), chapter 5, chapter 15, chapter 16. These chapters describe subroutine calls, PRINT positioning and colour controls.

Run it here

This unit's program, built for you.

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

Download locksmith.tap