• Work with functions
  • Work with interactive while loops
  • Use decisions
  • Introduce random values

This lab is inspired by a famous childrens game known as the number-guessing game. We suppose two people are playing.

The rules are:

  • Person A chooses a positive integer less than N and keeps it in his or her head.
  • Person B makes repeated guesses to determine the number. Person A must indicate whether the guess is higher or lower.
  • Person A must tell the truth.

So as an example:

  • George and Andy play the game.
  • George chooses a positive number less than 100 (29) and puts it in his head.
  • Andy guesses 50. George says Lower. Andy now knows that [ M a t h P r o c e s s i n g E r r o r ] .
  • Andy guesses 25. George says Higher. Andy now knows that [ M a t h P r o c e s s i n g E r r o r ] .
  • Andy guesses 30. George says Lower. Andy now knows that the [ M a t h P r o c e s s i n g E r r o r ] .
  • Andy starts thinking that he is close to knowing the correct answer. He decides to guess 29. Andy guesses the correct number. So George says, Good job! You win.

We are going to elaborate this game in small steps. You might save the intermediate versions under new names.

The computer code for the game is going to be acting like Player A.

Part 1: No Hints; Fixed Secret Number

You will want to use the UI class, so either copy ui.cs into your project, or (for Xamarin Studio) create a new project in a solution in which you already have added the ui library project, and add the ui project as a r e f e r e n c e for the lab project. Make sure your program has namespace IntroCS; to match the UI class.

You are going to play a game, and later may repeat it, so put the code for playing the number game in a function called Game :

static void Game()

For now your write a Main function to just call Game() .

In Game :

  • For the simplest versions, which help testing, have the program assign a specific secret number (like 29), and call it secret . Admittedly, this is not much fun for the player the second time!
  • Prompt the player for a guess. Use UI.PromptInt . Every time the player guesses wrong, print Wrong!. A later version will give clues. Keep prompting for another number until the player guesses correctly. (Since you, the programmer, knows the secret number, this need not go on forever.)
  • When the player guesses the right number, print Correct! You win!

Sample play could look like:

Guess the number: 55
Guess the number: 12
Guess the number: 29
Good job! You win!

You could also make the game stop immediately, (since you know the secret number):

Guess the number: 29
Good job! You win!

Part 2: Add Hints

In Game : Instead of just printing Wrong! when the player is incorrect, print Lower! or Higher! as appropriate. For example:

Guess the number: 55
Guess the number: 12
Guess the number: 25
Guess the number: 29
Good job! You win!

Part 3: Add a Random Secret Number

In Game , make the following alterations and additions:

  • For now set an int variable big to 100. We will make sure the secret number is less than big .
  • Have the Game function print In this game you guess a positive number less than 100. For future use it is best if you have the printing statement reference the variable big , rather than the literal 100 .
  • Thus far the secret number was fixed in the program. Now we are going to let it vary, by having the game generate a r a n d o m number. For your convenience, we are going to give you the C# code to compute the random number. Assuming we want a secret number so [ M a t h P r o c e s s i n g E r r o r ] , we can use the code:
Random r = new Random();
int secret = r.Next(1, big);
  • In case you are wondering, we are creating a n e w o b j e c t of the c l a s s Random which serves as the random number generator. Well cover this in more detail when we get to the C l a s s e s a n d O b j e c t O r i e n t e d P r o g r a m m i n g (classes.html#classes) chapter. Here is some illustration using a Random object in csharp. Your answers will not be the same!
csharp> Random r = new Random();
csharp> r.Next(1, 100); 55
csharp> r.Next(1, 100); 31
csharp> r.Next(1, 100); 79
csharp> r.Next(2, 5); 2
csharp> r.Next(2, 5); 4
csharp> r.Next(2, 5); 3
csharp> r.Next(2, 5); 3
  • In general the minimum possible value of the number returned by r.Next is the first parameter, and the value returned is always l e s s than the second parameter, n e v e r e q u a l . You can see that r.Next() is smart enough to give what appears to be a randomly chosen number every time. Example (where secret ended up as 68):
Guess a number less than 100!
Guess the number: 60
Guess the number: 72
Guess the number: 66
Guess the number:
Good job! You win!
  • For debugging purposes, you might want to have secret be printed out right away. (Eliminate that part when everything works!)

Part 4: Let the Player Set the Range of Values

In Game : Instead of declaring big and automatically initializing it to 100, make big be a parameter, so the heading looks like:

static void Game(int big)

In Main :

Prompt the player for the limit on the secret number. An exchange might look like:

Enter a secret number bound: 10

Pass the value given by the player to the Game function (so it will be big inside Game ).

Hence the program might start with:

Enter a secret number bound: 10
In this game you guess a number less than 10!
Guess the number:
5 Higher!
Guess the number: 7
Guess the number: 6
Good job! You win!

Part 5: Count the Guesses

In Game : When the player finally wins, print the number of guesses the player made. For example, for the game sequence shown above, the last line would become:

Good job! You win on guess 3!

You need to keep a count, adding 1 with each guess.

Academic Honesty!
It is not our intention to break the school's academic policy. Projects posted are only used as a reference and should not be submitted as is. We are not held liable for any misuse of the solutions. Please see the frequently asked questions page for further questions and inquiries.
Kindly fill out the form. Please provide a valid email address and we'll get back to you in less than 24 hours. We will be sending an invoice through PayPal upon confirmation. We are a non profit organization however we need an amount to keep this organization running, and to be able to complete our research and development.