Challenge 34
🎲
Guess the Number!
Create a "guess the number" game - the player should be prompted to choose from one of two modes:
- Single player: the machine will generate a random number
- Multiplayer: another person will enter a random number - CALL CLEAR() can be used to clear their input from the output console, so the guessing player can't see
Whatever option is chosen, the number to guess should be within a fixed range - for example, 1 to 100
The player will take guesses and will be notified if their guess is too high, too low or correct. An example game is shown below. Assume the number to guess is 72
Examples:
Player guesses 40 outputs "Too low, try again"
Player guesses 65 outputs "Too low, try again"
Player guesses 80 outputs "Too high, try again"
Player guesses 75 outputs "Too high, try again"
Player guesses 72 outputs "That's correct! The number was 72. It took you 5 guesses to win"
Extension:
For the guesser, there is an optimal strategy - do you know what it is?
Likewise, if a person is using this strategy, there will be numbers that will be the most difficult to guess - do you know what they would be?