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();
}
}