mardi 28 avril 2015

Consolidating Action Listeners of Buttons In Random Number Game

I'm relatively new to Java, and I had a question about a game that I wrote. It's a random number game where you try to guess a number from 1-10 in the least number of guesses.

I got the program to work, but the code seems very inefficient. As it currently is, I am creating a JButton for each number, then creating a separate Action Listener for each button, like this (A lot has been omitted just for simplicity's sake and because it was irrelevant to the question):

Note: int number = The random number that is chosen by the program, and int guessCounter = The number that keeps track of the number of guesses

 public class random extends JFrame{

 //Creating the Buttons

 JButton one = new JButton("1");
 JButton two = new JButton("2");
 JButton three = new JButton("3");

    public static void main(String[] args){

    //Creating Action Listeners

        OneClass uno = new OneClass();
        one.addActionListener(uno);

        TwoClass dos = new TwoClass();
        two.addActionListener(dos);

        ThreeClass tres = new ThreeClass();
        three.addActionListener(tres);

        //Action Listener For Button 1

        private class OneClass implements ActionListener{
            public void actionPerformed(ActionEvent event){
                if(number == 1){
                    guessCounter++;
                    JOptionPane.showMessageDialog(null,"Correct! You guessed the correct number in " + guessCounter + " guesses","Correct!", JOptionPane.PLAIN_MESSAGE);
                }else if(number > 1){
                    guessCounter++;
                    JOptionPane.showMessageDialog(null,"Incorrect. Guess Higher. You have guessed " + guessCounter + " time(s).","Incorrect", JOptionPane.PLAIN_MESSAGE);
                    one.setEnabled(false);
                }else if(number < 1){

                }
            }

        }

        //Action Listener For Button 2

        private class TwoClass implements ActionListener{
            public void actionPerformed(ActionEvent event){
                if(number == 2){
                    guessCounter++;
                    JOptionPane.showMessageDialog(null,"Correct! You guessed the correct number in " + guessCounter + " guesses","Correct!", JOptionPane.PLAIN_MESSAGE);
                }else if(number > 2){
                    guessCounter++;
                    JOptionPane.showMessageDialog(null,"Incorrect. Guess Higher. You have guessed " + guessCounter + " time(s).","Incorrect", JOptionPane.PLAIN_MESSAGE);
                    two.setEnabled(false);
                }else if(number < 2){
                    guessCounter++;
                    JOptionPane.showMessageDialog(null, "Incorrect. Guess Lower. You have guessed " + guessCounter + " time(s)","Incorrect", JOptionPane.PLAIN_MESSAGE);
                    two.setEnabled(false);
                }
            }
        }

        //Action Listener For Button 3

        private class ThreeClass implements ActionListener{
            public void actionPerformed(ActionEvent event){
                if(number == 3){
                    guessCounter++;
                    JOptionPane.showMessageDialog(null,"Correct! You guessed the correct number in " + guessCounter + " guesses","Correct!", JOptionPane.PLAIN_MESSAGE);
                }else if(number > 3){
                    guessCounter++;
                    JOptionPane.showMessageDialog(null,"Incorrect. Guess Higher. You have guessed " + guessCounter + " time(s).","Incorrect", JOptionPane.PLAIN_MESSAGE);
                    three.setEnabled(false);
                }else if(number < 3){
                    guessCounter++;
                    JOptionPane.showMessageDialog(null, "Incorrect. Guess Lower. You have guessed " + guessCounter + " time(s)","Incorrect", JOptionPane.PLAIN_MESSAGE);
                    three.setEnabled(false);
                }
            }

One can see where the inefficiency can be so prevalent when each Action Listener is this long. My question is, is it possible to condense this (Even if the Action Listener for each button requires that the random number be compared to a different number)? This is already very long, and will become extremely long as soon as I start adding more difficult levels with more numbers to chose from.




Aucun commentaire:

Enregistrer un commentaire