CS 161 Lab D - Guessing Game
Due Thurs Feb 14 at 11:59pm
Overview
In this lab, you will create a simple game to play! The game is a "number guessing game"---the computer will pick a random number between 1 and 50, and then the player will try to guess it. The player will have 8 guesses. The player will enter the number they guess through the keyboard, and the program will let the player know how close their guess is (if they are "hot" or "cold"). When finished, your program's output might look something like:
Welcome to Guessing Game. I have selected a number between 0 and 50. You have 8 guesses. Number of guesses remaining 8 Guess a number: 25 Your guess is cold Number of guesses remaining 7 Guess a number: 20 Your guess is very cold Number of guesses remaining 6 Guess a number: 30 Your guess is warm Number of guesses remaining 5 Guess a number: 35 Your guess is extremely warm Number of guesses remaining 4 Guess a number: 37 Congratulations, you figured it out!
Note that I typed in the 25, 30, 35, and 37.
If you run the program again (and are less lucky/clever), the output might look like:
Welcome to Guessing Game. I have selected a number between 0 and 50. You have 8 guesses. Number of guesses remaining 8 Guess a number: 25 Your guess is icy freezing miserable cold Number of guesses remaining 7 Guess a number: 15 Your guess is icy freezing miserable cold Number of guesses remaining 6 Guess a number: 30 Your guess is very cold Number of guesses remaining 5 Guess a number: 35 Your guess is cold Number of guesses remaining 4 Guess a number: 40 Your guess is warm Number of guesses remaining 3 Guess a number: 45 Your guess is extremely warm Number of guesses remaining 2 Guess a number: 50 Your guess is very warm Number of guesses remaining 1 Guess a number: 48 Your guess is scaldingly hot! Sorry, you couldn't guess my number!
This lab will be completed in pairs. Be sure to review the pair programming guidelines. You also must work with a different partner than last time!
Objectives
- To practice with conditionals and boolean expressions
- To write a while loop
- To practice creating a class and calling its methods.
Necessary Files
There are no necessary files for this class, but you might want to have the textbook handy!
Details
To create this program, you will need two Java classes, one to represent the SecretNumber
and the other to be the main class that runs the GuessingGame
.
-
The
SecretNumber
class represents the secret number, as well as the number of times the user has tried to guess that number. This is a "model" class: it should have attributes, a constructor, and methods.- Think about what types of attributes are needed to keep track of the number to guess and the nubmer of trials remaining
-
The constructor for your SecretNumber class should initialize the instance variables. The number of guesses should be 8. The secret number should be a random number between 0 and 50.
-
Remember that you can generate a random number either by using the
Math.random()
function (to get a double between 0 and 1), or by using the java.util.Random class (you will need to import it!):Random randomGenerator = new Random(); //a random generator int randomNumber = randomGenerator.nextInt(50); //a random nubmer between 0 (inclusive) and 50 (exclusive)
-
Remember that you can generate a random number either by using the
-
Your SecretNumber class will need a method
guessNumber()
that can be called when the user guesses a number. This method should take a number as a parameter. The parameter is then compared with the secret number, and print a message depending on how close the guess is to the secret number. You can see how "close" the guess is by subtracting the guess from the secret number to get the difference. Use aif/else if/else
block to print a different message as the number gets closer, such as:Within 1 "scalding hot" Within 2 "extremely warm" Within 3 "very warm" Within 5 "warm" Within 8 "cold" Within 13 "very cold" Within 20 "extremely cold" More than 20 away "icy freezing miserably cold" Remember that you need to print the message if the difference is positive OR if the difference is negative! (e.g., you should have a boolean operator and check both cases!)
If the guess is wrong, decrease the number of guesses remaining by 1. You should also return whether or not the user guessed correctly.
- You will also need to create a getter for the number of guesses left.
-
The
GuessingGame
class will control the guessing game. This is a "tester" or "main" class, and so should have a main method. In this main method, you should do the following things:- Instantiate a new
SecretNumber
object. - Create a Scanner to get input from the user.
-
Use a while loop to let the player guess multiple times. Think about what the control condition for this loop will be: under what conditions do we want to continue asking for a guess? (Hint: it probably has to do with the number of guesses left and whether they got the guess right or not).
- Inside the loop, you should prompt the user to enter a guess, and then use that guess as a parameter to the
guessNumber
method of your SecretNumber object. - You will want to store whether they got the guess correct or not in a variable. We can then use this variable to print out whether they won or not once the game is over. Hint: should this variable be declared inside or outside of the loop?
- If the user correctly guessed the number, congratulate them. If not, kindly suggest that they try again :)
- Inside the loop, you should prompt the user to enter a guess, and then use that guess as a parameter to the
- Instantiate a new
- Be sure to test and retest your code at each step of the process! You may want to (temporarily) print out the secret number so you know what you are looking for, then you can guessNumbers that are within a certain range to test your conditionals.
- Double-check that your program works perfectly by playing multiple games. If there is ever any behavior that seems wrong, stop and figure out what caused that! "Play computer" and work through the program by hand to see what Java is doing.
- Once you are sure that it works, make sure that both your and your partner's names are in a class comment at the top of both your files. Then submit your program to the submissions folder.
- Finally (as always) be sure to fill out the lab partner evaluation survey on Moodle after you turn in your work!
Submitting
Upload the your SecretNumber.java
and GuessingGame.java
classes to the LabD folder on the submission folder on hedwig. Make sure you upload your work to the correct folder! The lab is due at midnight on the day of the lab.
Extensions
- Try having the game pick a random number between 1 and 50 (inclusive), rather than between 0 and 50 (inclusive).
- When the game is over, tell the user how many guesses it took them to figure out the number, or what the number was if they failed to guess.
- You can use another while to allow the user to play multiple games. Can you keep track of how many games they played and won, and then tell them their batting average at the end?
Grading
This assignment will be graded on approximately the following criteria:
- Your SecretNumber class has an appropriate constructor (that picks a random number) and instance variables [15%]
- Your SecretNumber class has a getter for the number of guesses left [5%]
- Your guessNumber method checks the guess, prints appropriate messages, updates the number of guesses remaining, and returns the result (lots of things!) [40%]
- Your GuessingGame class gets an inputted guess from the player [5%]
- Your GuessingGame class has a main method with a loop. This loop lets the player guess for the specific number of trials [20%]
- You congratulate the player when they win, or console them when they lose [10%]
- You completed your lab partner evaluation [5%]