-4

I've been figuring out my code for hours and I can't find any solution for my code The game GUI shouldn't be display anymore when I have clicked on either my Water , Fire Or Nature button,because every button will add 1 to store into my gamePlayed and the maximum would only store up to 5 times,I really cant figure it this out please help me ! Really appreciate that.

public class Game {
   private JPanel Game;
   private JButton Water;
   private JButton Nature;
   private JButton Fire;
   private int myChoice;
   private int computerChoice;
        int gamePlayed = 0; //store the times of game played

 public Game() {
    JFrame frame = new JFrame("The Elements");
    frame.setContentPane(this.Game);
    frame.setMinimumSize(new Dimension(500, 500));
    frame.pack();
    frame.setVisible(true);


      if (gamePlayed <= 5) {  //if the times of game played is less than 5 times , execute the following code

        Water.addActionListener(new ActionListener() {
            @Override
            //Water counter Fire , Nature counter water
            public void actionPerformed(ActionEvent e) {
                int select;
                select = JOptionPane.showConfirmDialog(null,
                        "You're using the power of water", "Water",
                        JOptionPane.YES_NO_OPTION);

                if (select == JOptionPane.YES_OPTION) {
                    myChoice = 0;
                    computerChoice = computerPlays();
                    conditionsDisplayResults(myChoice, computerChoice);
                    gamePlayed+=1;//add 1 time of played game when the yes option clicked

                }

            }
        });
        Fire.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int select = JOptionPane.showConfirmDialog(null,
                        "You're using the power of fire", "Fire",
                        JOptionPane.YES_NO_OPTION);

                if (select == JOptionPane.YES_OPTION) {
                    myChoice = 1;
                    computerChoice = computerPlays();

                    conditionsDisplayResults(myChoice, computerChoice);
                    gamePlayed+=1;//add 1 time of played game when the yes option clicked
                }
            }
        });
        Nature.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {

                int select = JOptionPane.showConfirmDialog(null,
                        "You're using the power of nature", "Nature",
                        JOptionPane.YES_NO_OPTION);

                if (select == JOptionPane.YES_OPTION) {
                    myChoice = 2;
                    computerChoice = computerPlays();
                    conditionsDisplayResults(myChoice, computerChoice);
                    gamePlayed+=1;//add 1 time of played game when the yes option clicked
                }

            }
        });


  else{ //else which when the times of game played is more than 5 times , execute the following code

        GameResult();
        MainMenu mainMenu = new MainMenu();

    }
}
snieguu
  • 2,073
  • 2
  • 20
  • 39
  • Why the down-votes exactly? Suspected as "debug my code pls?" @ShangHuang, the question is really not clear. What's the error? Are you familiar with [debugging](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems?noredirect=1&lq=1)? – Curiosa Globunznik Nov 27 '19 at 15:02
  • Sorry I just realized that I made a bad question title. Thanks for advising me! I'll keep that in mind! :D – Shang Huang Nov 27 '19 at 15:09
  • I tried to edit the title , is that okay for now? – Shang Huang Nov 27 '19 at 15:11
  • 2
    [Debugging](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems?noredirect=1&lq=1)! [Debugging](https://stackoverflow.com/questions/25385173/what-is-a-debugger-and-how-can-it-help-me-diagnose-problems?noredirect=1&lq=1) will save your life! – Curiosa Globunznik Nov 27 '19 at 15:13

1 Answers1

2

Your condition if (gamePlayed <= 5) is tested one time : at the start of the program. When one of your button adds +1 to gamePlayed, you just don't go again in your initial condition.

There is plenty of possibilities to fix this, the simpliest would be to just exit your program in the buttons click event like :

public class Game
{
    private JPanel  Game;
    private JButton Water;
    private JButton Nature;
    private JButton Fire;
    private int         myChoice;
    private int         computerChoice;
    int                         gamePlayed  = 0;    //store the times of game played

    public Game()
    {
        JFrame frame = new JFrame( "The Elements" );
        frame.setContentPane( this.Game );
        frame.setMinimumSize( new Dimension( 500, 500 ) );
        frame.pack();
        frame.setVisible( true );

        Water.addActionListener( new ActionListener()
        {
            @Override
            //Water counter Fire , Nature counter water
            public void actionPerformed( ActionEvent e )
            {
                int select;
                select = JOptionPane.showConfirmDialog( null, "You're using the power of water", "Water", JOptionPane.YES_NO_OPTION );

                if ( select == JOptionPane.YES_OPTION )
                {
                    myChoice = 0;
                    computerChoice = computerPlays();
                    conditionsDisplayResults( myChoice, computerChoice );
                    gamePlayed += 1;//add 1 time of played game when the yes option clicked

                }

                if ( this.gamePlayed > 5 )
                {
                    exit();
                }
            }
        } );
        Fire.addActionListener( new ActionListener()
        {
            @Override
            public void actionPerformed( ActionEvent e )
            {
                int select = JOptionPane.showConfirmDialog( null, "You're using the power of fire", "Fire", JOptionPane.YES_NO_OPTION );

                if ( select == JOptionPane.YES_OPTION )
                {
                    myChoice = 1;
                    computerChoice = computerPlays();

                    conditionsDisplayResults( myChoice, computerChoice );
                    gamePlayed += 1;//add 1 time of played game when the yes option clicked
                }

                if ( this.gamePlayed > 5 )
                {
                    exit();
                }
            }
        } );
        Nature.addActionListener( new ActionListener()
        {
            @Override
            public void actionPerformed( ActionEvent e )
            {
                int select = JOptionPane.showConfirmDialog( null, "You're using the power of nature", "Nature", JOptionPane.YES_NO_OPTION );

                if ( select == JOptionPane.YES_OPTION )
                {
                    myChoice = 2;
                    computerChoice = computerPlays();
                    conditionsDisplayResults( myChoice, computerChoice );
                    gamePlayed += 1;//add 1 time of played game when the yes option clicked
                }

                if ( this.gamePlayed > 5 )
                {
                    exit();
                }
            }
        } );
    }

    void exit()
    {
        // exit code
    }
}
Lucsartes
  • 321
  • 2
  • 13