Back to Challenge Set 8

Code Challenge: Guessing Game

Difficulty  

divider

The Brief

What they asked for
The computer picks a number between one and a hundred and I
try to find it. Tell me higher or lower each time. When I get
it, tell me how many guesses it took.

Write your requirements list before you write any code. Input, calculate, output, and anything you had to decide for yourself.

This is the biggest program in this set. It is three stars because it needs a loop, a decision inside that loop, a random number, and a count that survives the whole thing — four ideas at once, all of which you already have.


Notes

  • You do not know how many guesses it will take, which is exactly why this is a while loop and not a fixed number of repeats.
  • The count has to survive the loop, so it starts outside it. Lunar Lander's turn counter in Activity 1.17 did exactly this job.
  • Random.Shared.Next(1, 101) gives 1 through 100. The second number is where it stops — Activity 1.16 if you need it again.
  • Test that guessing right on the first try reports one guess, not zero. That is the boundary here, and it is decided by where you put the line that counts.
  • Decide what happens if the player types something that is not a number, and write your decision down. You do not have to handle it — Activity 1.17's optional challenge is where that tool lives.
  • While you are testing, it is much easier to replace the random number with a fixed one. Put it back before you submit.

Sample Output

Sample Output
I'm thinking of a number between 1 and 100.
Your guess: 50 [Enter]
Higher.
Your guess: 75 [Enter]
Lower.
Your guess: 62 [Enter]
Got it! That took 3 guesses.

Play it until you have seen higher, lower, and a first-try win. The first-try win is the one that will be wrong, and it is the one you are least likely to hit by accident.


Submit

Your requirements list and your program file, named cc-guessing-game.

Commence Challenge