-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGNGUI1.java
More file actions
182 lines (111 loc) · 3.43 KB
/
Copy pathGNGUI1.java
File metadata and controls
182 lines (111 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class GNGUI1 {
private int secret;
private int low = 1;
private int high = 100;
private int attempts = 0;
private JFrame frame;
private JTextField inputField;
private JButton guessButton;
private JLabel messageLabel;
private JLabel rangeLabel;
private JLabel attemptsLabel;
// HINT
private JLabel hintLabel;
public GNGUI1() {
secret = (int)(Math.random() * 100) + 1;
frame = new JFrame("Вгадай число! ??");
frame.setSize(400, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new GridLayout(6, 1));
JLabel titleLabel = new JLabel("Я загадав число від 1 до 100. Спробуй вгадати!",
SwingConstants.CENTER);
titleLabel.setFont(new Font("Arial", Font.BOLD, 14));
frame.add(titleLabel);
rangeLabel = new JLabel("Діапазон чисел: [" + low + " — " + high + "]",
SwingConstants.CENTER);
frame.add(rangeLabel);
inputField = new JTextField();
frame.add(inputField);
guessButton = new JButton("Ввести");
frame.add(guessButton);
JPanel bottomPanel = new JPanel(new GridLayout(1, 2));
messageLabel = new JLabel(" ", SwingConstants.CENTER);
messageLabel.setForeground(Color.BLUE);
attemptsLabel = new JLabel("Спроб: 0", SwingConstants.CENTER);
bottomPanel.add(messageLabel);
bottomPanel.add(attemptsLabel);
frame.add(bottomPanel);
// HINT
hintLabel = new JLabel(" ", SwingConstants.CENTER);
hintLabel.setForeground(new Color(120, 0, 200));
frame.add(hintLabel);
guessButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
checkGuess();
}
});
inputField.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
checkGuess();
}
});
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
private void checkGuess() {
String text = inputField.getText();
int guess;
try {
guess = Integer.parseInt(text);
} catch (NumberFormatException e) {
messageLabel.setText("Введи правильне число!");
messageLabel.setForeground(Color.RED);
return;
}
if (guess < low || guess > high) {
messageLabel.setText("Число поза діапазоном!");
messageLabel.setForeground(Color.RED);
return;
}
attempts++;
attemptsLabel.setText("Спроб: " + attempts);
if (guess == secret) {
messageLabel.setText("?? Вітаю! Ти вгадав число!");
messageLabel.setForeground(Color.GREEN);
guessButton.setEnabled(false);
inputField.setEnabled(false);
hintLabel.setText(" ");
} else if (guess < secret) {
messageLabel.setText("Моє число більше.");
messageLabel.setForeground(Color.ORANGE);
low = Math.max(low, guess + 1);
} else {
messageLabel.setText("Моє число менше.");
messageLabel.setForeground(Color.ORANGE);
high = Math.min(high, guess - 1);
}
rangeLabel.setText("Діапазон чисел: [" + low + " — " + high + "]");
// HINT SYSTEM
giveHint();
inputField.setText("");
inputField.requestFocus();
}
// HINT
private void giveHint() {
int range = high - low;
if (range <= 3) {
hintLabel.setText("Ти дуже близько! ??");
} else if (range <= 10) {
hintLabel.setText("Діапазон вузький — спробуй уважніше!");
} else {
int mid = (low + high) / 2;
hintLabel.setText("Порада: спробуй число близьке до " + mid);
}
}
public static void main(String[] args) {
SwingUtilities.invokeLater(() -> new GNGUI());
}
}