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

Pick a Secret

Every guessing game needs a secret. Make the machine choose a hidden code between 1 and 100 with RND, and print it just this once to prove it lands in range. The number you'll spend the rest of the game hunting starts here.

17% of Safe Cracker

A guessing game needs something to guess. So before anything else, make the machine choose a secret — a hidden number the player will hunt for. You've used RND to pick fortunes and roll dice; here it sets the combination of a safe.

10 PRINT CHR$(147)
20 S=INT(RND(1)*100)+1
30 PRINT "THE SECRET CODE IS";S
40 PRINT "(SHOWN THIS ONCE, TO PROVE IT EXISTS)"
A C64 screen reading THE SECRET CODE IS 19, with a note that it's shown this once to prove it exists.
The secret, picked by the machine: 19 this run. You print it once here to prove RND lands a whole number in range — then it goes into hiding for the real game.

Line 20 is the whole job: S = INT(RND(1)*100)+1. It's the die-roll formula from Tally, widened. RND(1) gives a fraction; *100 stretches it to 0 up to (just under) 100; INT drops the decimals to leave 0 to 99; +1 shifts it to 1 to 100. So S holds a whole number somewhere in that range, fresh each run.

Printing it on line 30 is a one-time check — you'd never show the player the answer in the real game. But right now it proves two things: the secret exists (it's a real number in S) and it lands in range (1 to 100, never 0, never 101). Get the range right here and the rest of the game is fair; get it wrong and the safe could hide a code no guess can reach.

Try this

  • Change the range. Make it *1000 for a code from 1 to 1000. A bigger range is a harder safe — more guesses to close in. The range is the difficulty.
  • Run it a few times. Each run picks a different secret. That unpredictability is what makes every game of Safe Cracker its own puzzle.

What's next

The machine has a secret. In Unit 2 the player takes their first shot at it — one guess, weighed against the code, right or wrong.