Skip to content
Game 11 Unit 1 of 6 1 hr learning time

The Code and a Guess

DIM hides a four-digit code, and a typed guess is validated and parsed into a second array — turning a string of digits into numbers the program can compare.

17% of Locksmith

Locksmith is "guess the secret code", so two things must exist before any clues: the code, and a way to read the player's guess into a form the program can compare. Both are arrays — the idea from Quiz Master, used here to hold a secret and a guess side by side.

  10 BORDER 0: PAPER 0: INK 7: CLS
 130 RANDOMIZE
 140 DIM c(4)
 150 FOR i = 1 TO 4: LET c(i) = INT (RND * 6) + 1: NEXT i
 160 CLS
 170 PRINT "The code is: ";
 180 FOR i = 1 TO 4: PRINT c(i);: NEXT i
 190 PRINT
 220 INPUT "Your guess (4 digits): "; g$
 230 IF LEN g$ <> 4 THEN GO TO 220
 240 IF g$(1) < "1" OR g$(1) > "6" OR g$(2) < "1" OR g$(2) > "6" OR g$(3) < "1" OR g$(3) > "6" OR g$(4) < "1" OR g$(4) > "6" THEN GO TO 220
 250 DIM g(4)
 260 FOR i = 1 TO 4: LET g(i) = VAL g$(i): NEXT i
 270 PRINT "You guessed: "; g$
ZX Spectrum Locksmith: The code is: 1666 and You guessed: 1666
The hidden code (shown for now) and a guess parsed into its own array, ready to compare.

The code, hidden in an array

Line 140's DIM c(4) makes the code array; line 150 fills each slot with INT (RND * 6) + 1, a digit 1–6, repeats allowed. The player never sees c() directly — they reconstruct it by guessing. While you build the clue logic, lines 170–180 print the code so you can check your work; the finished game draws that curtain. (This "show the secret while developing" habit is worth keeping — verify against the truth, then hide it.)

Parsing a guess into an array

The guess comes in as a string — INPUT g$ — and the program won't compare a string to numbers, so it parses. Line 230 rejects a guess that isn't four characters; line 240 rejects any digit outside 1–6 (validation by g$(1), g$(2)… — the slicing from Cipher). Then lines 250–260 build a second array: DIM g(4), and g(i) = VAL g$(i) turns character i into the number it represents. VAL is the opposite of STR$ — text to number. Now the code and the guess are two arrays of four numbers, lined up and ready to score.

Next: count the exact matches.