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

Win and Lose

Both endings get their own screen: a cracked-code celebration counting the guesses it took, and an out-of-guesses screen that finally reveals the answer.

83% of Locksmith

The two exits from the guess loop deserve more than a one-line message. A win should celebrate and tell you how sharp you were; a loss should reveal the code you were chasing. Both are the end-screen pattern from Volume 1, pointed at Locksmith's two outcomes.

  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 AT 14, 2; "Code: ";
 180 FOR i = 1 TO 4: PRINT c(i);: NEXT i
 190 INVERSE 1: PRINT AT 0, 0; "      *** LOCKSMITH ***        ": INVERSE 0
 200 FOR t = 1 TO 10
 210 PRINT AT 20, 0; "Guess "; t; " of 10: ";
 220 INPUT g$
 230 IF LEN g$ <> 4 THEN GO TO 210
 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 210
 250 DIM g(4)
 260 FOR i = 1 TO 4: LET g(i) = VAL g$(i): NEXT i
 270 LET bulls = 0
 280 FOR i = 1 TO 4
 290 IF g(i) = c(i) THEN LET bulls = bulls + 1
 300 NEXT i
 310 LET total = 0
 320 FOR d = 1 TO 6
 330 LET cc = 0: LET gc = 0
 340 FOR i = 1 TO 4
 350 IF c(i) = d THEN LET cc = cc + 1
 360 IF g(i) = d THEN LET gc = gc + 1
 370 NEXT i
 380 IF cc <= gc THEN LET total = total + cc
 390 IF gc < cc THEN LET total = total + gc
 400 NEXT d
 410 LET cows = total - bulls
 430 PRINT AT 2 + t, 2; g$; "  "; bulls; " bull  "; cows; " cow"
 490 IF bulls = 4 THEN GO TO 540
 500 NEXT t
 510 GO TO 600
 540 CLS
 550 LET a$ = "*** LOCKSMITH ***": LET y = 6: GO SUB 9000
 560 PRINT AT 9, 6; "Code cracked!"
 570 PRINT AT 11, 6; "You got it in "; t; " guesses"
 580 BEEP 0.1, 10: BEEP 0.1, 15: BEEP 0.1, 20
 590 STOP
 600 CLS
 610 LET a$ = "*** LOCKSMITH ***": LET y = 6: GO SUB 9000
 620 PRINT AT 9, 6; "Out of guesses!"
 630 PRINT AT 11, 6; "The code was ";
 640 FOR i = 1 TO 4: PRINT c(i);: NEXT i
 650 BEEP 0.3, -10: STOP

9000 PRINT AT y, (32 - LEN a$) / 2; BRIGHT 1; a$
9010 RETURN
ZX Spectrum Locksmith win screen: Code cracked! You got it in 1 guesses
The win screen counts the guesses it took — a score to beat next time.

Two routes out

The win test (line 490) and the loop's natural end now jump to separate routines instead of printing in place. The win screen clears, recentres the title with GO SUB 9000, says "Code cracked!" and reports t — the turn you solved it on, which doubles as a score: cracking it in four guesses beats cracking it in nine. A rising three-note BEEP rewards it. The lose screen reveals the code and sounds a low buzzer. Same GO SUB title routine for both, so the two endings share a look.

Counting the guesses turns a binary win/lose into something to improve — the deduction game rewards efficiency, and the number on the win screen is the thing a player replays to push down.

Next: the finishing touches — peg clues, a title, and the curtain over the code.