I made a boolean for my while loop to make it run when the boolean is equal to true
, I wanted to make the play button make the boolean = true
which would trigger the while loop and run the game. But this isn't working for some reason.
Can someone help with making the boolean gameRunning = true;
? I just can't figure out how to change its value from false
to true
.
I tried using atomic booleans
but that didn't work
package panda.org;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.lang.Math;
import java.util.concurrent.atomic.AtomicBoolean;
public class NumberGame implements ActionListener{
JFrame frame;
JLabel rules;
JLabel rulesText;
JLabel rulesText2;
JButton play;
JButton exit;
Font myFont = new Font("Serif Plain", Font.BOLD, 15);
NumberGame() {
frame = new JFrame("NumberGame");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(600, 500);
frame.setLocationRelativeTo(null);
frame.setLayout(null);
frame.setResizable(true);
Image icon = Toolkit.getDefaultToolkit().getImage("C:\\Users\\Gaming MSI\\Pictures\\Saved Pictures\\download (1).png");
frame.setIconImage(icon);
rules = new JLabel("Rules: ");
rules.setFont(myFont);
rules.setBounds(50, 100, 100, 75);
rulesText = new JLabel("We will pick a random number in the range of 1 -> 50.");
rulesText.setBounds(100, 100, 315, 75);
rulesText2 = new JLabel("Your job is to guess that number!");
rulesText2.setBounds(100, 120, 315, 75);
play = new JButton("Play");
play.setBounds(150, 300, 100, 75);
boolean gameRunning = false;
play.addActionListener(e -> {
gameRunning = true;
});
while(gameRunning = true) {
JLabel label = new JLabel("Guess the number from 1 till 50");
label.setFont(myFont);
label.setBounds(150, 75, 315, 75);
JLabel hints = new JLabel("");
hints.setBounds(150, 180, 1000, 100);
JLabel hints2 = new JLabel("");
hints2.setBounds(150, 200, 1000, 100);
JTextField text = new JTextField();
text.setBounds(250, 150, 100, 25);
JButton check = new JButton("Check");
check.setBounds(150, 150, 75, 25);
double randomDouble = Math.random();
randomDouble = randomDouble * 50 + 1;
double randomDouble2 = Math.random();
randomDouble2 = randomDouble2 * (15 - 5 + 1) + 5 ;
double randomDouble3 = Math.random();
randomDouble3 = randomDouble3 * (15 - 5 + 1) + 5 ;
int randomHint = (int) randomDouble2;
int randomHint2 = (int) randomDouble3;
int randomInt = (int) randomDouble;
System.out.println("nb: " + randomInt);
System.out.println("hint: " + randomHint);
System.out.println("hint2: " + randomHint2);
JLabel status = new JLabel("");
status.setBounds(150, 160, 1000, 100);
JLabel closeness = new JLabel("");
closeness.setBounds(150, 220, 1000, 100);
closeness.setForeground(Color.blue);
final int[] failedAttempts = {0};
check.addActionListener(e1 -> {
String nb = text.getText();
int change = Integer.parseInt(nb);
frame.add(status);
if (randomInt == change) {
status.setText("You chose the correct number!");
status.setForeground(Color.green);
hints.setText("");
hints2.setText("");
}
if (randomInt > change) {
closeness.setText("Your answer is smaller than the correct answer");
}
if (randomInt < change) {
closeness.setText("Your answer is larger than the correct answer");
}
if (randomInt != change) {
status.setText("Wrong choice! Try again.");
status.setForeground(Color.red);
failedAttempts[0]++;
if (failedAttempts[0] == 3) {
int plus = randomInt + randomHint;
int minus = randomInt - randomHint2;
hints.setText("Hint: I see you are struggling, here is a low range to make it easier!");
hints2.setText("The lowered range is from " + plus + " to " + minus);
}
}
});
rules.setText("");
rulesText.setText("");
rulesText2.setText("");
frame.add(hints);
frame.add(hints2);
frame.add(label);
frame.add(check);
frame.add(closeness);
frame.add(text);
}
exit = new JButton("Exit");
exit.setBounds(350, 300, 100, 75);
exit.addActionListener(e -> {
int result = JOptionPane.showConfirmDialog(frame,"Are you sure want to exit?", "Exit",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE);
if(result == JOptionPane.YES_OPTION){
System.exit(0);
}
});
frame.add(play);
frame.add(exit);
frame.add(rules);
frame.add(rulesText);
frame.add(rulesText2);
frame.setVisible(true);
}
public static void main(String[] args) {
NumberGame number = new NumberGame();
}
@Override
public void actionPerformed(ActionEvent e) {
}
}