-
Notifications
You must be signed in to change notification settings - Fork 0
/
GuessingGame.java
105 lines (97 loc) · 3.64 KB
/
GuessingGame.java
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
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import java.awt.Dimension;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
@SuppressWarnings("serial")
public class GuessingGame extends JFrame {
private JTextField txtGuess;
private JLabel lblOutput;
private int numOfTries;
JButton btnPlayAgain;
private int theNumber;
public void checkGuess(){
String guessText = txtGuess.getText();
String message = "";
numOfTries++;
try {
int guess = Integer.parseInt(guessText);
if (guess < theNumber)
message = guess + " is too low. Try again.";
else if (guess > theNumber)
message = guess + " is too high. Try again.";
else {
message = guess +
" is correct. You win after " + numOfTries + " tries. Let's play again!";
//newGame();
btnPlayAgain.setVisible(true);
txtGuess.requestFocus();
txtGuess.selectAll();
}
} catch (Exception e){
message = "Enter a whole number between 1 and 100.";
} finally {
lblOutput.setText(message);
txtGuess.requestFocus();
txtGuess.selectAll();
}
}
public void newGame() {
theNumber = (int)(Math.random() * 100 + 1);
numOfTries = 0;
btnPlayAgain.setVisible(false);
}
public GuessingGame() {
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Cody's Hi-Lo Guessing Game");
getContentPane().setLayout(null);
JLabel lblCodysHilo = new JLabel("Cody's Hi-Lo Guessing Game");
lblCodysHilo.setFont(new Font("Tahoma", Font.BOLD, 15));
lblCodysHilo.setHorizontalAlignment(SwingConstants.CENTER);
lblCodysHilo.setBounds(10, 37, 414, 24);
getContentPane().add(lblCodysHilo);
JLabel lblGuessANumber = new JLabel("Guess a number between 1 and 100:");
lblGuessANumber.setHorizontalAlignment(SwingConstants.CENTER);
lblGuessANumber.setBounds(10, 98, 272, 14);
getContentPane().add(lblGuessANumber);
txtGuess = new JTextField();
txtGuess.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
checkGuess();
}
});
txtGuess.setBounds(292, 95, 43, 20);
getContentPane().add(txtGuess);
txtGuess.setColumns(10);
JButton btnGuess = new JButton("Guess!");
btnGuess.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
checkGuess();
}
});
btnGuess.setBounds(172, 149, 89, 23);
getContentPane().add(btnGuess);
lblOutput = new JLabel("Enter a number above, and click Guess!");
lblOutput.setHorizontalAlignment(SwingConstants.CENTER);
lblOutput.setBounds(10, 209, 414, 14);
getContentPane().add(lblOutput);
btnPlayAgain = new JButton("New Game");
btnPlayAgain.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
newGame();
}
});
btnPlayAgain.setBounds(261, 146, 117, 29);
getContentPane().add(btnPlayAgain);
}
public static void main(String[] args) {
GuessingGame theGame = new GuessingGame();
theGame.newGame();
theGame.setSize(new Dimension(450,300));
theGame.setVisible(true);
}
}