简体   繁体   中英

Java GUI Repeatable Process

I recently took up Java and I decided to construct a mini rock paper scissors game. Although, I think it turned out working but badly designed it still has a small problem. I cannot seem to figure out how to make the whole process repeatable because I have to restart the program so that the computer can choose once again between rock, paper or scissors. This is my code:

public class Panel extends JPanel implements ActionListener {

    int randomNum = new Random().nextInt(3) + 1;

    String AIGuess;
    String myGuess;

    boolean playAgain = true;

    JButton rock = new JButton("Rock");
    JButton paper = new JButton("Paper");
    JButton scissors = new JButton("Scissors");

    JLabel AIGuessDisplay = new JLabel(AIGuess);

    public Panel() {
        setLayout(new FlowLayout());
        add(rock);
        rock.addActionListener(
                  new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            myGuess = "Rock";
                            if (randomNum == 1) {
                                AIGuess = "Rock";
                                JOptionPane.showMessageDialog(null, "Tie");
                            }
                            if (randomNum == 2) {
                                AIGuess = "Paper";
                                JOptionPane.showMessageDialog(null, "You Lose");
                            }
                            if (randomNum == 3) {
                                AIGuess = "Scissors";
                                JOptionPane.showMessageDialog(null, "You Win");
                            }
                        }
                      }
                    );
        add(paper);
        paper.addActionListener(
                  new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            myGuess = "Paper";
                            if (randomNum == 1) {
                                AIGuess = "Rock";
                                JOptionPane.showMessageDialog(null, "You Win");
                            }
                            if (randomNum == 2) {
                                AIGuess = "Paper";
                                JOptionPane.showMessageDialog(null, "Tie");
                            }
                            if (randomNum == 3) {
                                AIGuess = "Scissors";
                                JOptionPane.showMessageDialog(null, "You Lose");
                            }
                        }
                      }
                    );

        add(scissors);
        scissors.addActionListener(
                  new ActionListener() {
                        public void actionPerformed(ActionEvent e) {
                            myGuess = "Scissors";
                            if (randomNum == 1) {
                                AIGuess = "Rock";
                                JOptionPane.showMessageDialog(null, "You Lose");
                            }
                            if (randomNum == 2) {
                                AIGuess = "Paper";
                                JOptionPane.showMessageDialog(null, "You Win");
                            }
                            if (randomNum == 3) {
                                AIGuess = "Scissors";
                                JOptionPane.showMessageDialog(null, "Tie");
                            }
                        }
                      }
                    );
        add(AIGuessDisplay);
    }

    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub

    }   
}

Your question is missing the main and setup of the panel, but basically, it seems that you only assign a value to randomNum once - at its declaration.

Therefore, that randomNum is not going to change, and thus the computer selection will be the same all the time.

You should therefore add a method, say newComputerSelection() , which will put a new random number in randomNum . And call this method from your action listeners before making your decisions.

In fact, you don't even have to have randomNum as a field. You can just get a new random number in that method, and immediately decide based on it what the AIGuess will be, instead of deciding inside each of the action listeners. In the action listeners, you just need to compare the user's choice to the AIGuess and decide if it's win, lose or tie.

Next step would be to eliminate AIGuess and just return the value of it from the newComputerSelection method. There are more improvements you can make, but basically, you need to change the computer choice on each new turn .

If you just really want to avoid restarting your program to be able to play again, you can add a new JButton in your code below the add(AIGuessDisplay); .

    add(AIGuessDisplay);

    JButton playAgain = new JButton("Play Again");
    add(playAgain);
    playAgain.addActionListener(
        new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                randomNum = new Random().nextInt(3) + 1;                            
            }
        }
    );        
}

That button will generate a fresh value for randomNum.

If you don't like adding a new JButton, another option to improve your program is to add the code randomNum = new Random().nextInt(3) + 1; in each actionperformed as shown below so that the randomNum will have a new value everytime any button(Rock, Paper, or Scissor) is clicked.

new ActionListener() {
    public void actionPerformed(ActionEvent e) {
        randomNum = new Random().nextInt(3) + 1;

but this modification differs now from how your program originally works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM