Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

This is my code currently below. Everything is working fine but I can't figure out how to keep score of the lowest amount of guesses for the random number. How can I keep track of the high score(high score is the lowest amount of guesses it took to guess the random number given by the system). I'm a beginner programmer and this was my first time making a gui pretty much so I apologize for the mess.

    import java.awt.EventQueue;
import java.util.Scanner;
import java.util.Timer;
import java.util.TimerTask;

import javax.swing.JFrame;
import javax.swing.JLabel;
import java.awt.BorderLayout;
import javax.swing.SwingConstants;


import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.Image;

import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JTable;
import java.awt.Component;
import javax.swing.Box;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.Font;

public class GuessingGUI {

    private JFrame frmGuessinggame;
    private JTextField enterNumber;
    private int min = 1;
    private int max = 100;
    private int guessNumber;
    private int guessedNumber;
    private int previousGuess;
    private int checkGuessedNumber;
    double placeHolder;
    private int i = 0;
    private int l =1;
    private JLabel newLabel;
    private int seconds = 5;
    private int clicked = 0;
    private int saveScore = 100;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    GuessingGUI window = new GuessingGUI();
                    window.frmGuessinggame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public GuessingGUI() {
        initialize();
        while(l>0) {
        placeHolder = Math.random() * 100;
        guessNumber = (int)placeHolder;
        System.out.println(guessNumber);
        --l;
        
        
        
        
        }
        
    }
    
    
    public void reset() {
        // Setup the instance
        guessedNumber = 0;
        guessNumber = 0;
        placeHolder = 0;
        l=0;
        i=0;
        placeHolder = Math.random() * 100;
        guessNumber = (int)placeHolder;
        System.out.println(guessNumber);
        
    }
    


    /**
     * Initialize the contents of the frame.
     */
    private void initialize() {
        frmGuessinggame = new JFrame();
        frmGuessinggame.setTitle("GuessingGame");
        frmGuessinggame.setBounds(100, 100, 635, 416);
        frmGuessinggame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmGuessinggame.getContentPane().setLayout(null);
        
        JLabel winnerLabel = new JLabel("YOU WON!");
        winnerLabel.setForeground(Color.YELLOW);
        winnerLabel.setFont(new Font("Lucida Grande", Font.BOLD, 30));
        winnerLabel.setBounds(15, 94, 193, 124);
        frmGuessinggame.getContentPane().add(winnerLabel);
        winnerLabel.setVisible(false);
        
        JLabel lblNewLabel = new JLabel("Guess:");
        lblNewLabel.setBounds(24, 6, 52, 16);
        frmGuessinggame.getContentPane().add(lblNewLabel);
        
        JLabel guessLabel = new JLabel("");
        guessLabel.setBounds(68, 6, 61, 16);
        frmGuessinggame.getContentPane().add(guessLabel);
        
        JLabel lblNewLabel_1 = new JLabel("Best Score:");
        lblNewLabel_1.setBounds(136, 6, 72, 16);
        frmGuessinggame.getContentPane().add(lblNewLabel_1);
        
        JLabel bestScore = new JLabel("");
        bestScore.setBounds(212, 6, 61, 16);
        frmGuessinggame.getContentPane().add(bestScore);
        
        JLabel lblNewLabel_3 = new JLabel("Enter a number between 1 & 100");
        lblNewLabel_3.setBounds(325, 94, 216, 16);
        frmGuessinggame.getContentPane().add(lblNewLabel_3);
        
        enterNumber = new JTextField();
        enterNumber.setBounds(367, 122, 130, 26);
        frmGuessinggame.getContentPane().add(enterNumber);
        enterNumber.setColumns(10);
        
        JLabel higherLabel = new JLabel("Higher");
        higherLabel.setBounds(212, 138, 61, 16);
        frmGuessinggame.getContentPane().add(higherLabel);
        
        JLabel lowerLabel = new JLabel("Lower");
        lowerLabel.setBounds(212, 178, 61, 16);
        frmGuessinggame.getContentPane().add(lowerLabel);
        
        JLabel lblNewLabel_4 = new JLabel("Number is :");
        lblNewLabel_4.setBounds(15, 335, 78, 16);
        frmGuessinggame.getContentPane().add(lblNewLabel_4);
        
        JLabel numberIs = new JLabel("? ? ?");
        numberIs.setBounds(100, 335, 61, 16);
        frmGuessinggame.getContentPane().add(numberIs);
        
        JButton guessButton = new JButton("GUESS");
        guessButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                
                
                String entertxt= enterNumber.getText(). replaceAll( " " , "" );
                checkGuessedNumber = Integer.parseInt(entertxt);

                
                
                if(1<=checkGuessedNumber && 100>=checkGuessedNumber) {
                    guessedNumber = checkGuessedNumber;
                    System.out.println(guessedNumber);
                    ++i;
                }
                
                if(entertxt != "") {
                    clicked++;
                if(guessedNumber == guessNumber) {
                    System.out.println("Perfect");
                    numberIs.setText(Integer.toString(guessedNumber));
                    enterNumber.setEditable( false );
                winnerLabel.setVisible(true);
                
                /*
                 
                 This is where the score should be switched out and stored I think.
                 If the player guesses a lower number than before then the best score is kept and changed out.
                 
                    saveScore = clicked;
                    if(clicked<=saveScore) {
                    bestScore.setText(Integer.toString(saveScore));
                    }
                */
                    
                }else if( guessNumber > guessedNumber) {
                    higherLabel.setForeground(Color.GREEN);
                }else if (guessNumber < guessedNumber){
                    lowerLabel.setForeground(Color.RED);
                }
                
                guessLabel.setText(Integer.toString(i));
                
                if(guessedNumber == guessNumber) {
                    
                }else {
                enterNumber.setText("");
                }
                }
                
                new java.util.Timer().schedule( 
                
                 new java.util.TimerTask() {
                        @Override
                        public void run() {
                            higherLabel.setForeground(Color.WHITE);
                            lowerLabel.setForeground(Color.WHITE);
                        }
                    }, 
                    1500
            );
                
        
            }
                
            
        });
        
        guessButton.setBounds(367, 290, 117, 29);
        frmGuessinggame.getContentPane().add(guessButton);
        
        JButton btnNewButton = new JButton("GIVE UP!");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                
                String holder = Integer.toString(guessNumber);
                numberIs.setText(holder);
                enterNumber.setEditable( false );
                
            }
        });
        
        btnNewButton.setBounds(265, 248, 117, 29);
        frmGuessinggame.getContentPane().add(btnNewButton);
        
        JButton playAgainButton = new JButton("PLAY AGAIN");
        playAgainButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
            
                reset();
                numberIs.setText("? ? ?");
                enterNumber.setEditable( true );
                enterNumber.setText("");
                guessLabel.setText("");
                winnerLabel.setVisible(false);
                clicked = 0;
                
            }
        });
        playAgainButton.setBounds(471, 248, 117, 29);
        frmGuessinggame.getContentPane().add(playAgainButton);
        
        
        
        

}
}

Image of GUI, bestScore top left.

question from:https://stackoverflow.com/questions/65851320/saving-score-for-guessing-game

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
132 views
Welcome To Ask or Share your Answers For Others

1 Answer

You see where you have: saveScore = clicked;? That's a bad place to put that line of code. If you set saveScore to equal clicked and then in the very next line of code you check to see if clicked is less than or equal to (<=) saveScore, what do you think is going to happen considering in the previous line of code you made saveScore equal to clicked?

Move saveScore = clicked; as the first line of code within the if statement code block directly below it. Make saveScore equal to clicked only after you know clicked is indeed less than saveScrore.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...