Results 1 to 4 of 4
  1. #1
    kev670 is offline Member
    Join Date
    Oct 2010
    Posts
    20
    Rep Power
    0

    Default Java - number guessing game

    Just wondering if someone could help me out here. I'm brand new to java and applet programming in particular so my code might be very questionable.

    it's a simple guess the number program.

    The user has 8 chances.

    My problem is when the user enters their 8th guess the program seems to go through the paint method twice because it never says "guess = 8/8"... instead it seems to go through the method a second time and displays 0/8 guesses. can anyone help me out...

    Also can anyone comment on anything that i should or shouldn't do with my current code. I'm just looking for pointers really... thanks

    Java Code:
    import java.applet.Applet;
    import java.awt.*;    // import the java.awt package
    import java.awt.event.*;  // import the java.awt.event package
    
    
    public class myApplet extends Applet implements ActionListener
    {
        Label prompt;                        // message that prompts user to input
        TextField input;                     // input values are entered here
        double rnd;
        int number;                          // variable that stores random value
        int guessLeft;
        int random;
        int wins = 0, losses = 0;
    
        private Image picture;
    
    //-----------------------------------------------------------------
    
        public void init()
        {
            setSize(500, 500);
            rnd = (Math.random()*100) + 1;
            guessLeft = 0;
            random = 12;//(int) rnd;
            number = -1;
    
            prompt = new Label( "Enter a number between 1 and 100:" );
            input = new TextField(5);
            
            add(prompt);                                    // put prompt on applet
            add(input);                                     // put input TextField on applet
            
            input.addActionListener( this );                // "this" applet handles action events for TextField input
      
        }
    
    //-----------------------------------------------------------------
    
        public void actionPerformed( ActionEvent e )                  // process user's action in TextField input
        {
            number = Integer.parseInt( e.getActionCommand() );         // get the number and convert it to an integer
            guessLeft++;
            input.setText( "" );       // clear data entry field
            if(number == random)
            {
                 wins++;
            }
            if (guessLeft == 8)
            {
                losses++;
            }
           
            
            picture = getImage(getCodeBase(), "pic"+guessLeft+".jpg");
            
    
    
            repaint();                    // Force paint to be invoked
        }
    
     //-----------------------------------------------------------------
    
        public void paint (Graphics g)
        {
            g.drawImage(picture, 80, 80, this);
    
            if (number == random)           //WIN
            {
                picture = getImage(getCodeBase(), "win.jpg");
                g.drawString("You WON ",50,40);
                g.drawString("Wins = " + wins + " / Losses = " + losses + "Guesses = " + guessLeft + "/8", 50, 60);
                g.drawImage(picture, 80, 80, this);
               
                
                guessLeft = 0;
                return;
            }
            else if (number == -1)
            {
                 return;        // makes sure nothing is displayed the first time the game is played
            }
            else if (guessLeft == 8)
            {
                picture = getImage(getCodeBase(), "lose.jpg");
                g.drawString("Sorry but you LOST",50,40);
                g.drawString("Wins = " + wins + " / Losses = " + losses + "Guesses = " + guessLeft + "/8", 50, 60);
                g.drawImage(picture, 80, 80, this);
       
                guessLeft = 0;
            }
            else if (number > random)
            {
                g.drawString("Your guess was too HIGH",50,40);
                g.drawString("Wins = " + wins + " / Losses = " + losses + "Guesses = " + guessLeft + "/8", 50, 60);
                g.drawImage(picture, 80, 80, this);
            }
            else
            {
                g.drawString("Your guess was too LOW",50,40);
                g.drawString("Wins = " + wins + " / Losses = " + losses + "Guesses = " + guessLeft + "/8", 50, 60);
            }
        }
    }
    //-----------------------------------------------------------------
    Last edited by kev670; 10-22-2010 at 12:56 AM.

  2. #2
    joshdgreen's Avatar
    joshdgreen is offline Senior Member
    Join Date
    Oct 2010
    Location
    Colorado Springs, CO
    Posts
    212
    Rep Power
    4

    Default

    When I ran it I got Guess: 8/8
    Sincerely, Joshua Green
    Please REP if I help :)

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    You shouldn't be reading in images from a file or resource in your paint method if you can avoid it as this method has to be as fast as possible. Better to just read in the images in the init method and hold them in instance variables.

  4. #4
    kev670 is offline Member
    Join Date
    Oct 2010
    Posts
    20
    Rep Power
    0

    Default

    Ok, I think everything is working now. I set up a few triggers to try and reset everything when the program needs to reset

    Thanks Fubarable. I'll try to edit the program so that the win/lose images don't load in paint...

    If anyone else has any advice on my code i'm all ears. Trying to get better at this. Cheers

    -edit- Ok, I edited my code so it loads the images outside of the paint method

    Java Code:
    // Addition program (modified fig 1_06, Deitel edition 2)
    // Modified to give proper output in the applet screen
    import java.applet.Applet;
    import java.awt.*;    // import the java.awt package
    import java.awt.event.*;  // import the java.awt.event package
    
    
    public class myApplet extends Applet implements ActionListener
    {
        Label prompt;                        // message that prompts user to input
        TextField input;                     // input values are entered here
        double rnd;
        int number;                          // variable that stores random value
        int guessLeft;
        int random;
        int wins = 0, losses = 0;
        int trigger = 1;
    
        private Image picture, picwin, piclose;
    
    //-----------------------------------------------------------------
    
        public void init()
        {
            setSize(500, 350);
            
            guessLeft = 0;
            
            number = -1;
    
            prompt = new Label( "Enter a number between 1 and 100:" );
    
            input = new TextField(5);
            
            add(prompt);                                    // put prompt on applet
    
            add(input);                                     // put input TextField on applet
            
            input.addActionListener( this );                // "this" applet handles action events for TextField input
      
        }
    
    //-----------------------------------------------------------------
    
        public void actionPerformed( ActionEvent e )                  // process user's action in TextField input
        {
            number = Integer.parseInt( e.getActionCommand() );         // get the number and convert it to an integer
     
            if (trigger == 1)
            {
               rnd = (Math.random()*100) + 1;
    
               random = (int) rnd;
    
               guessLeft = 0;                   // resets guesses if the game has been won but guesses haven't reached 8
    
               trigger = 0;
            }
    
            guessLeft++;
    
            input.setText( "" );       // clear data entry field
    
            if(number == random)
            {
                 wins++;
            }
            if (guessLeft == 8)
            {
                losses++;
            }
             
            picture = getImage(getCodeBase(), "pic"+guessLeft+".jpg");
            picwin = getImage(getCodeBase(), "win.jpg");
            piclose = getImage(getCodeBase(), "lose.jpg");
    
            repaint();                    // Force paint to be invoked
        }
    
     //-----------------------------------------------------------------
    
        public void paint (Graphics g)
        {
            g.drawImage(picture, 80, 80, this);
    
            if (number == random)           //WIN
            {
                g.drawString("You WON ",50,40);
                g.drawString("Wins = " + wins + " / Losses = " + losses, 50, 60);
                g.drawString("Guesses = " + guessLeft + "/8", 250, 60);
                g.drawImage(picwin, 80, 80, this);
    
                trigger = 1;
       
                return;
            }
            else if (number == -1)
            {
                 return;        // makes sure nothing is displayed the first time the game run
            }
    
            else if (guessLeft == 8)
            {
                g.drawString("Sorry but you LOST",50,40);
                g.drawString("Wins = " + wins + " / Losses = " + losses, 50, 60);
                g.drawString("Guesses = " + guessLeft + "/8", 250, 60);
                g.drawImage(piclose, 80, 80, this);
    
                trigger = 1;
                
            }
    
            else if (number > random)
            {
                g.drawString("Your guess was too HIGH",50,40);
                g.drawString("Wins = " + wins + " / Losses = " + losses, 50, 60);
                g.drawString("Guesses = " + guessLeft + "/8", 250, 60);
            }
    
            else
            {
                g.drawString("Your guess was too LOW",50,40);
                g.drawString("Wins = " + wins + " / Losses = " + losses, 50, 60);
                g.drawString("Guesses = " + guessLeft + "/8", 250, 60);
            }
    
        }
    }
    //-----------------------------------------------------------------
    Last edited by kev670; 10-22-2010 at 01:04 AM.

Similar Threads

  1. Help with a word guessing game.
    By The_Round_One in forum New To Java
    Replies: 6
    Last Post: 05-19-2010, 04:22 AM
  2. Replies: 3
    Last Post: 12-20-2009, 03:22 PM
  3. guessing game help
    By yasmin k in forum AWT / Swing
    Replies: 4
    Last Post: 10-31-2009, 05:37 PM
  4. guessing game using GUI
    By yasmin k in forum New To Java
    Replies: 1
    Last Post: 10-26-2009, 12:13 PM
  5. guess number game
    By mistah in forum New To Java
    Replies: 10
    Last Post: 11-23-2008, 03:37 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •