Loop Until Cracked
One clue isn't enough — the player needs to keep guessing. Wrap the guess in a loop that runs until the safe opens. This is the loop-with-a-goal: not a FOR that counts to a number, but a loop that ends when something specific happens.
A single guess with a clue still leaves the player stuck after one go. To deduce, they need to keep guessing — narrowing in, clue after clue, until the safe gives. So wrap the whole guess in a loop that runs until the code is cracked.
10 PRINT CHR$(147)
20 S=INT(RND(1)*100)+1
30 INPUT "YOUR GUESS";G
40 IF G=S THEN PRINT "CRACKED IT!":END
50 IF G<S THEN PRINT "THE CODE IS HIGHER"
60 IF G>S THEN PRINT "THE CODE IS LOWER"
70 GOTO 30
The new line is line 70: GOTO 30, which jumps back to the INPUT. After every wrong guess —
whichever hint printed — the program loops to the top and asks again. Only line 40 breaks the
cycle: when G = S, it prints CRACKED IT! and ENDs, stopping the loop dead. Any other guess
falls through to a hint and round again.
This is a loop-with-a-goal, and it's a different shape from the FOR loops you've used. A
FOR counts to a fixed number and stops; this loops until a condition is met — the safe
cracking — however many guesses that takes. Look at the figure: 50, 25, 12, 18, 19, each clue
cutting the field in half, the safe opening on the fifth try. The loop ran exactly as long as the
deduction needed, no more, no less. That "run until something happens" pattern is the spine of
every real game.
Try this
- Beat your best. Always guessing the middle of what's left cracks 1–100 in seven tries at most. See if you can hit it every time.
- Sloppy on purpose. Guess badly — 1, 2, 3, 4… — and watch the loop patiently take dozens of tries. Right now there's no penalty for it. That changes soon.
What's next
The safe opens, but it answers in plain words on a plain screen. In Unit 5 colour does the talking — the background warming red or cooling blue before the player reads a single word.