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

Take a Guess

Now hide the secret and let the player put a number against it. INPUT takes their guess, a single IF weighs it against the code — cracked, or not. The first real contest between player and machine.

33% of Safe Cracker

The machine has a secret; now let the player challenge it. Hide the code this time — no printing it — and take a guess instead. One number against another: either it matches, or it doesn't.

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 PRINT "NOT THIS TIME."
60 PRINT "THE CODE WAS";S
A C64 screen reading YOUR GUESS? 50, NOT THIS TIME, THE CODE WAS 19.
A guess weighed against the secret: 50 against a hidden 19. Wrong this time — and the program owns up to the code, since with only one guess there's nowhere else to go.

Line 30, INPUT "YOUR GUESS"; G, prints the prompt and waits for the player to type a number, storing it in G. You met INPUT in the primer and in Oracle; here it takes a number, not a word. Then line 40 is the contest: IF G = S THEN PRINT "CRACKED IT!". If the guess equals the secret, the safe opens. If not, the IF is false, the program falls past it, and line 50 admits the miss.

This is one guess and one verdict — right or wrong, no second chance yet. It's deliberately unforgiving: with a single try against 100 numbers, the player will almost always miss, so the program shows the code afterwards. That's fine for now — the point is the mechanism, a guess measured against a secret with a single IF. The next unit makes a miss worth something.

Try this

  • Guess the shown code. Run Unit 1, note the number, then run this and type it — CRACKED IT! proves the equality test fires when the numbers match.
  • Out of range. Type 0, or 200, or a letter. A guess outside 1–100 can never be right — worth thinking about how a finished game should handle it.

What's next

One guess against 100 numbers is nearly hopeless. In Unit 3 a wrong guess earns a clue — higher or lower — and the hunt gains a direction.