-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberGuesserGameGui.java
More file actions
103 lines (90 loc) · 3.43 KB
/
NumberGuesserGameGui.java
File metadata and controls
103 lines (90 loc) · 3.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class NumberGuesserGameGui extends JFrame {
private int randomNumber;
private int triesLeft;
private int start;
private int end;
private static final int DIFF = 10;
private JTextField inputField;
private JLabel messageLabel;
private JButton guessButton;
private JButton resetButton;
public NumberGuesserGameGui() {
setupUI();
setupGame();
}
private void setupUI() {
setTitle("Number Guesser");
setSize(500, 200);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLocationRelativeTo(null);
setLayout(new BorderLayout());
JPanel centerPanel = new JPanel(new FlowLayout());
messageLabel = new JLabel();
inputField = new JTextField(10);
guessButton = new JButton("Guess");
resetButton = new JButton("Reset");
centerPanel.add(messageLabel);
centerPanel.add(inputField);
centerPanel.add(guessButton);
centerPanel.add(resetButton);
add(centerPanel, BorderLayout.CENTER);
add(Box.createVerticalStrut(30), BorderLayout.NORTH);
addListeners();
resetButton.setVisible(false);
}
private void addListeners() {
guessButton.addActionListener(e -> makeGuess());
resetButton.addActionListener(e -> resetGame());
}
private int calculateTries(int start, int end) {
return (int) (Math.log(end - start + 1) / Math.log(2)) + 2;
}
private void setupGame() {
Random rand = new Random();
start = rand.nextInt(1000);
end = start + rand.nextInt(5000) + 1000;
randomNumber = rand.nextInt(end - start + 1) + start;
triesLeft = calculateTries(start, end);
messageLabel.setText("Guess a number between " + start + " and " + end + ". Allowed difference: " + DIFF + ". Tries: " + triesLeft);
inputField.setEnabled(true);
guessButton.setEnabled(true);
inputField.setText("");
resetButton.setVisible(false);
}
private void makeGuess() {
try {
int guess = Integer.parseInt(inputField.getText());
triesLeft--;
int difference = Math.abs(randomNumber - guess);
if (guess == randomNumber) {
messageLabel.setText("Congratulations! You've won! Num was " + randomNumber);
endGame();
} else if (triesLeft == 0) {
messageLabel.setText("Game Over! The number was " + randomNumber);
endGame();
} else {
String rangeMessage = (difference > DIFF ? "Outside range." : "Within range.");
String directionMessage = (guess < randomNumber ? " Try higher." : " Try lower.");
messageLabel.setText(rangeMessage + directionMessage + " Tries left: " + triesLeft + " (Range: " + start + "-" + end + ")");
}
} catch (NumberFormatException e) {
messageLabel.setText("Please enter a valid number!");
}
inputField.setText("");
}
private void endGame() {
inputField.setEnabled(false);
guessButton.setEnabled(false);
resetButton.setVisible(true);
}
private void resetGame() {
dispose();
new NumberGuesserGameGui().setVisible(true);
}
public static void main(String[] args) {
new NumberGuesserGameGui().setVisible(true);
}
}