-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNumberGuesser.java
More file actions
166 lines (135 loc) · 5.75 KB
/
NumberGuesser.java
File metadata and controls
166 lines (135 loc) · 5.75 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class NumberGuesser extends JFrame implements ActionListener {
//Initialize counter to track tries remaining
private int counter;
//Initialize score
private int score;
//Generate a random number
static int randomNumber = (int) (Math.random()*101);
//Initialize the components
private JLabel mainLabel;
private JLabel hintLabel;
private JLabel triesLabel;
JButton guessButton;
JButton exitButton;
JTextField guessField;
//Initialize the constructor
NumberGuesser(){
//Set the counter and the score
counter = 10;
score = 1000;
//Initialize the first panel and add the labels
mainLabel = newLabel("Guess The Number!", 50);
hintLabel = newLabel("Enter a number to receive a hint.", 30);
triesLabel = newLabel("Tries Remaining: " + counter, 28);
JPanel panel1 = newPanel();
panel1.add(mainLabel);
panel1.add(hintLabel);
panel1.add(triesLabel);
//Initialize the second panel and add the text field
JPanel panel2 = newPanel();
guessField = new JTextField(5);
guessField.setForeground(Color.WHITE);
guessField.setForeground(Color.BLACK);
guessField.setFont(new Font(null, Font.PLAIN, 20));
panel2.add(guessField);
//Initialize the third panel and add the buttons
guessButton = newButton("Guess");
exitButton = newButton("Exit");
JPanel panel3 = newPanel();
panel3.add(guessButton);
panel3.add(exitButton);
//Add the panels to the frame and customize the frames
add(panel1);
add(panel2);
add(panel3);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(3, 1));
setVisible(true);
setSize(new Dimension(600, 500));
setLocationRelativeTo(null);
setResizable(false);
}
//Method to make and customize a new button
private JButton newButton(String text){
JButton button = new JButton(text);
button.setBackground(Color.BLUE);
button.setForeground(Color.WHITE);
button.setFont(new Font(null, Font.BOLD, 20));
button.setFocusable(false);
button.addActionListener(this);
return button;
}
//Method to make and customize a new panel
private JPanel newPanel(){
JPanel panel = new JPanel();
panel.setBackground(Color.BLACK);
panel.setOpaque(true);
return panel;
}
//Method to make and customize a new label
private JLabel newLabel(String text, int size){
JLabel label = new JLabel();
label.setForeground(Color.WHITE);
label.setFont(new Font(null, Font.PLAIN, size));
label.setText(text);
return label;
}
//Action Performed method
@Override
public void actionPerformed(ActionEvent e) {
//Check if guessButton is pressed
if(e.getSource() == guessButton){
//Check if tries are remaining
if (counter > 1) {
//Check if user input is a number
try {
int userGuess = Integer.parseInt(guessField.getText());
//Check if guess is within the range
if (userGuess >= 0 && userGuess <= 100) {
//Check if guess is correct
if (userGuess == randomNumber) {
hintLabel.setText("That's the correct number!");
guessButton.setEnabled(false);
exitButton.setEnabled(false);
Timer timer = new Timer(1000, e1 -> {
dispose();
JOptionPane.showMessageDialog(null, "Congratulations You Won! Your score is " + score, "You Win", JOptionPane.INFORMATION_MESSAGE);
});
timer.setRepeats(false);
timer.start();
} else if (userGuess > randomNumber) {
//If guess is larger than the actual number
hintLabel.setText("That number is too large...");
score-=100; //Decrease the score by 100
} else if (userGuess < randomNumber) {
//If guess is smaller than the actual number
hintLabel.setText("That number is too small...");
score-=100; //Decrease the score by 100
}
//Decrement the counter
counter--;
} else {
//If guess is out of range
hintLabel.setText("Please enter a number between 0 and 100");
}
} catch (NumberFormatException e1) {
//If input is not a valid number
hintLabel.setText("Please enter a valid number!");
}
//Update the label to show tries remaining
triesLabel.setText("Tries Remaining: " + counter);
} else {
//If no tries left
dispose();
JOptionPane.showMessageDialog(null, "No tries left. Please play again.", "No tries left", JOptionPane.ERROR_MESSAGE);
}
} else if (e.getSource() == exitButton) {
//If exitButton is pressed
dispose();
}
}
}