Java Random number guessing game

need help with a few areas in my code... almost finished

Page 1 of 1

3 Replies - 45354 Views - Last Post: 05 February 2007 - 06:25 AM Rate Topic: -----

#1 baseballstud154  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-November 06

Java Random number guessing game

Post icon  Posted 01 February 2007 - 01:58 PM

I have almost finished my code but I am having troubles with a few areas... they can be found by looking for the *. Any help would be great thanks.

// Exercise 11.15 Solution: GuessGameFrame.java
// Guess the number
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Graphics;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;

public class GuessGameFrame extends JFrame
{
private static Random generator = new Random();
private int number; // number chosen by application
private int guessCount; // number of guesses
private int lastDistance; // distance between last guess and number
private JTextField guessInputJTextField; // for guessing
private JLabel prompt1JLabel; // first prompt to user
private JLabel prompt2JLabel; // second prompt to user
private JLabel messageJLabel; // displays message of game status
private JButton newGameJButton; // creates new game
private Color background; // background color of application

// set up GUI and initialize values
public GuessGameFrame()
{
super( "Guessing Game" );
/* Write a line of code that calls the superclass constructor and sets the title
of this application to "Guessing Game" */

guessCount = 0; // initialize number of guesses to 0
background = Color.LIGHT_GRAY; // set background to light gray

prompt1JLabel = new JLabel(
"I have a number between 1 and 1000." ); // describe game
prompt2JLabel = new JLabel(
"Can you guess my number? Enter your Guess:" ); // prompt user

guessInputJTextField = new JTextField( 5 ); // to enter guesses
guessInputJTextField.addActionListener( new GuessHandler( ) );
messageJLabel = new JLabel( "Guess result appears here." );

newGameJButton = new JButton( "New Game" ); // button with text
add ( newGameJButton ); // add newGame button to JFrame
/* Write a statement that creaters the "New Game" button */
// create new ButtonHandler for button event handling
newGameJButton.addActionListener(

new ActionListener() // anonymous inner class
{
public void actionPerformed( ActionEvent e )
{
/* Write code that resets the application to an appropriate state
to start a new game. Reset the background color to light gray,
set the JTextFields to their initial text, call method
theGame and repaint the GuessGame JFrame */
} // end method actionPerformed
} // end anonymous inner class
); // end call to addActionListener

/* Write code that will set the layout of the container to a Flowlayout,
then add all the GUI components to the container */
theGame(); // start new game
} // end GuessGameFrame constructor

// choose a new random number
public void theGame()
{
number = ( int ) ( Math.random() * 1000 + 1 );
/* Write a statement that sets instance variable number to a random number
between 1 and 1000 */
} // end method theGame

// change background color
public void paint( Graphics g )
{
super.paint( g );
getContentPane().setBackground( background ); // set background
} // end method paint

// react to new guess
public void react( int guess )
{
guessCount++; // increment guesses
/* Write code that sets instance variable currentDistance to 1000. This
variableís value will be used to determine if th ebackground color
should be set to red or blue to indicate that the last guess was getting
closer to or further from the actual number. */

// first guess
if ( guessCount == 1 )
{
/* Write code to set instance variable lastDistance to the absolute value
of the difference between variables guess and number. This value will

be used with subsequent guesses to help set the background color. */

if ( guess > number )
messageJLabel.setText( "Too High. Try a lower number." );
else
messageJLabel.setText( "Too Low. Try a higher number." );
} // end if
else
{
/* Write code that sets instance variable currentDistance to the absolute
value of the difference between variables guess and number. This
variableís value will be compared with lastDistance to determine the
background color. */

// guess is too high
if ( guess > number )
{
messageJLabel.setText( "Too High. Try a lower number." );

/* Write code that sets Color variable background to red if the
currentDistance is less than or equal to lastDistance; otherwise,
set background to blue. Then assign currentDistance to lastDistance. */
} // end if
else if ( guess < number ) // guess is too low
{
messageJLabel.setText( "Too Low. Try a higher number." );
background = ( currentDistance <= lastDistance ) ?
Color.RED : Color.BLUE;
lastDistance = currentDistance;
} // end else if
else // guess is correct
{
messageJLabel.setText( "Correct!" );

/* Write code that sets Color variable background to red if the
currentDistance is less than or equal to lastDistance; otherwise,
set background to blue. Then assign currentDistance to lastDistance. */
} // end else

repaint();
} // end else
} // end method react

// inner class acts on user input
class GuessHandler implements ActionListener
{
public void actionPerformed( ActionEvent e )
{
/* Write code that will obtain the guess, convert it to an int and
pass that value to the react method */
} // end method actionPerformed
} // end inner class GuessHandler
} // end class GuessGameFrame



Is This A Good Question/Topic? 0
  • +

Replies To: Java Random number guessing game

#2 Jayman  Icon User is offline

  • Student of Life
  • member icon

Reputation: 418
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Java Random number guessing game

Posted 01 February 2007 - 02:14 PM

You are going to need to explain the problems you are experiencing with your code.

There are to many comments to read and too much code to go through for you to expect anyone to take the time to read through it.

Be as specific as possible and someone will be happy to help.
Was This Post Helpful? 0
  • +
  • -

#3 baseballstud154  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 18
  • Joined: 01-November 06

Re: Java Random number guessing game

Posted 01 February 2007 - 02:53 PM

The program is to generate a random number between 1 and 1000. Then it is to let the user try and guess that number. The program is then to turn blue if the guess is far away from the random number or turn red if the guess is getting closer to the number generated. This is to continued until the user is able to guess the correct number.

Im sorry that I didn't make my question specific enough, but I hope that this will help you understand what I have not been able to complete and need help with to complete.

First I need help with about 8 or 9 lines of code... it seems like alot but only becuase of how I am trying to explain what is to be done. Anyways I will try and explain the lines that I need help with.

First i need to write a statement that creaters the "New Game" button.

Second i need to write code that resets the application to an appropriate state
to start a new game. Reset the background color to light gray, set the JTextFields to their initial text, call method theGame and repaint the GuessGame JFrame .

Third i need to write code that will set the layout of the container to a Flowlayout, then add all the GUI components to the container.

Fourth i need to write a statement that sets instance variable number to a random number between 1 and 1000.

Fith i need to write code that sets instance variable currentDistance to 1000. This variableís value will be used to determine if th ebackground color
should be set to red or blue to indicate that the last guess was getting
closer to or further from the actual number.

Sixth i need to write code to set instance variable lastDistance to the absolute value of the difference between variables guess and number. This value will
be used with subsequent guesses to help set the background color.

Seventh i need to write code that sets instance variable currentDistance to the absolute value of the difference between variables guess and number. This
variableís value will be compared with lastDistance to determine the background color.

Eighth i need to write code that sets Color variable background to red if the
currentDistance is less than or equal to lastDistance; otherwise, set background to blue. Then assign currentDistance to lastDistance.

Lastly i need to write code that will obtain the guess, convert it to an int and
pass that value to the react method.

If you make any changes to the current code could you please let me know so that I will be able to follow the flow of logic in the new code.

Thanks
Was This Post Helpful? 0
  • +
  • -

#4 keems21  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 6
  • View blog
  • Posts: 185
  • Joined: 03-February 07

Re: Java Random number guessing game

Posted 05 February 2007 - 06:25 AM

It sounds like you've got your work cut out for you.

Well here's my one suggeston: for now just forget all about the GUI stuff and focus on the game aspect of your program. Play this game in the console. Use System.out.println() to ask for a guess, then read the guess into a variable with a Scanner or a Reader of some sort. From there, figure out how to determine if the guess is too high or too low.

If you get this aspect of your program organized and figured out, adding everything else like the changing colors should become trivial.

Good luck with your program. If you have any specific questions, I'll try and help you as best I can.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1