-
Notifications
You must be signed in to change notification settings - Fork 0
12 14_GuessNumber_Medium
Write an application that plays "guess the number" as follows: Your application choose the number to be guessed by selecting an integer at random in the range 1-1000. The application then displays the following in a label:
I have a number between 1 and 1000. Can you guess my number?
Please enter your first guess.
A JTextField should be used to input the guess. As each guess is input, the background color should change to either red or blue. Red indicates that the user is getting "warmer," and blue, "colder." A JLabel should display either "Too High" or "Too Low" to help the user zero in. When the user gets the correct answer, "Correct!" should be displayed, and the JTextField used for input should be changed to be uneditable. A JButton should be provided to allow the user to play the game again. When the JButton is clicked, a new random number should be generated and the input JTextField changed to be uneditable.
To start the application run the driver class GuessNumberDriver
. Then enter guesses in the text box until you arrive at the correct answer. The background color will change to blue when you are closer and red when you guess is farther. Hints such as "too high" or "too low" will be given to make the game easier. To see the correct number go to File -> Show Number. To start a new game without finishing the current one go to File -> New Game. Once a game is completed the background will change to green and a new game button will appear. To exit the game simply close the application window.
When running GuessNumberDriver
it creates a new JFrame
, creates a new GuessNumber
which extends JPanel
, initializes the menu, adds the GuessNumber
to the JFrame
and shows the frame. The File -> Show Number menu option creates a new message dialog showing the number the user is guessing for in the current game while New Game resets the current game and starts a new one.
Upon initialization GuessNumber
creating the layout of the application which uses GridLayout
of 5 x 1 containing a JLabel
which tells the user how to play the game, JTextField
where the user enters their guess, JButton
"Guess" where the user clicks when they'd like to make a guess, another JLabel
giving hints whether their guess is "Too High" or "Too Low" and finally another JButton
"New Game" which is always hidden unless a game has ended and the user is asked to click if they'd like to start a new game. A new number between MIN_VALUE
(1) and MAX_VALUE
(1000) is generated. Action listeners for the text box and the guess button are added to process the guess while the new game button has a action listener that creates a new game by making a call to newGame(boolean)
. processGuess()
gets the input from text box and checks if the guess is a valid guess. If it isn't then the hint label reminds the user what to guess between. If a valid guess is given then makeGuess(int)
is called which returns a GuessResult
which is an enum of CLOSER
, FURTHER
, FIRST
and CORRECT
and also sets the previous guess to the current guess made. The background color and hint label are changed according to the guess and it's result. Once a correct answer is given toggleView()
toggles the enabled state of the new game button, guess button and guess text box as well as toggling the visibility of the new game button which effectively changes states between a new game screen and a running game.
UML Diagram:
The java documents are served from a local web server on this machine. To start the web server, navigate to the directory immediately above where the source code is checked out (i.e. ~/git ) and then use "python -m SimpleHTTPServer" in that directory.
cd ~/git
python -m SimpleHTTPServer&
Note: if you are running python 3 (which you can check via opening a terminal and typing: python --version), then the command is:
python3 -m http.server