Objectives:
This lab is inspired by a famous game played by children (and grown-ups, too) known as the number-guessing game. It is often played by two people but could be played by any number of people.
The rules are:
So as an example:
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.
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 Main function can just call Game().
Copy in the functions from previous code so you can use InputInt.
In Game:
Sample play could look like:
Guess the number: 55Wrong!Guess the number: 12Wrong!Guess the number: 29Good job! You win!
You could also make the game stop immediately, (since you know the secret number):
Guess the number: 29Good job! You win!
In Game: Instead of just printing “Wrong!” when the player is incorrect, print “Lower!” or “Higher!” as appropriate. For example:
Guess the number: 55Lower!Guess the number: 12Higher!Guess the number: 25Higher!Guess the number: 29Good job! You win!
In Game, make the following alterations and additions:
Thus far the secret number was fixed in the program. Now we are going to let it vary, by having the game generate a random 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 \(1 \leq secret < big\), we can use the code:
Random r = new Random();
int secret = r.Next(1, big);
In case you are wondering, we are creating a new object of the class Random which serves as the random number generator. We’ll cover this in more detail when we get to the Classes and Objects section. Here is some illustration 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);
3
csharp> r.Next(2, 5);
4
csharp> r.Next(2, 5);
3
csharp> r.Next(2, 5);
2
In general the minimum possible value of the number returned by r.Next is the first parameter, and the value returned is always less than the second parameter.
You can see that r.Next() is smart enough to give what appears to be a randomly chosen number every time. If you call it 100 times, it is likely that you’ll see the same number twice!
Example (where secret ended up as 68):
Guess a number less than 100!Guess the number: 60Higher!Guess the number: 72Lower!Guess the number: 66Lower!Guess the number: 68Good job! You win!
For debugging purposes, you might want to have secret be printed out right away (but eliminate that part when everything works)!
In Game: Instead of setting big automatically 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.
Hence the program might start with:
Enter a secret number bound: 10Guess a number less than 10!Guess the number: 5Higher!Guess the number: 7Lower!Guess the number: 6Good job! You win!
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.
Should you finish everything early, try the following:
(20% extra credit) In Main:
Use an outer while loop to allow the game to be played repeatedly. Change the prompt for the bound in Main to:
Enter a secret number bound (or 0 to quit):
Continue to play games until the player enters 0 for the bound.
(40% extra credit) Write the opposite program, where the user is the one who knows the secret number and the computer guesses numbers until the answer is obtained. You can keep the same Main, that sets big. The new Game will tell the user to put a number in his/her head, and press return to continue. (You can throw away the string entered - this is just to cause a pause.) Then the computer guesses. For simplicity let the human enter “L” for lower, “H” for higher, and “E” for equal (when the computer wins). As you saw in the initial example with George and Andy, each hint reduces the range of the possible secret numbers. Have the computer guess a random number in the exact range that remains possible.
To do this you must note the asymmetry of the parameters for the method Next: suppose n = r.Next(low, higher), then
\(low \leq number < higher\)
The first parameter may be returned, but second parameter is never returned.
You will need two parameters low and higher that keep bracketing the allowed range. The simplest thing is to set them so they will be the parameters for the following call to Next.
That would mean initially low is 1 and higher is equal to big. With each hint you adjust one or the other of low and higher so they get closer together. The game ends after the human enters “E”.
Have the computer complain that the human is cheating (and stop the game) if the computer guesses the only posible value, and the human does not respond with “E”.