Skip to content

Commit 05c4097

Browse files
authored
Add files via upload
0 parents  commit 05c4097

6 files changed

+345
-0
lines changed

.gitignore

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
### IntelliJ IDEA ###
2+
out/
3+
!**/src/main/**/out/
4+
!**/src/test/**/out/
5+
6+
### Eclipse ###
7+
.apt_generated
8+
.classpath
9+
.factorypath
10+
.project
11+
.settings
12+
.springBeans
13+
.sts4-cache
14+
bin/
15+
!**/src/main/**/bin/
16+
!**/src/test/**/bin/
17+
18+
### NetBeans ###
19+
/nbproject/private/
20+
/nbbuild/
21+
/dist/
22+
/nbdist/
23+
/.nb-gradle/
24+
25+
### VS Code ###
26+
.vscode/
27+
28+
### Mac OS ###
29+
.DS_Store

Instructions.java

+45
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import javax.swing.*;
2+
import java.awt.*;
3+
import java.awt.event.ActionEvent;
4+
import java.awt.event.ActionListener;
5+
6+
public class Instructions extends JFrame implements ActionListener {
7+
8+
//Initialize the button
9+
JButton backButton;
10+
11+
//Initialize the constructor
12+
Instructions(){
13+
//Initialize and customize the label and add it to the frame
14+
JLabel label = new JLabel("<html>You have 10 turns to guess the number.<br> Enter a number and a hint will be provided <br> to tell you whether your guess is greater <br> or less than the actual number. The earlier <br> you guess the number the greater will be your score.</html>");
15+
label.setFont(new Font(null, Font.PLAIN, 20));
16+
label.setForeground(Color.WHITE);
17+
add(label);
18+
19+
//Initialize and customize the button and add it to the frame
20+
backButton = new JButton("Back");
21+
backButton.setBackground(Color.BLUE);
22+
backButton.setForeground(Color.WHITE);
23+
backButton.setFont(new Font(null, Font.BOLD, 20));
24+
backButton.setFocusable(false);
25+
backButton.addActionListener(this);
26+
add(backButton);
27+
28+
29+
//Customize the frame
30+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
31+
getContentPane().setBackground(Color.BLACK);
32+
setVisible(true);
33+
setLayout(new FlowLayout());
34+
setResizable(false);
35+
setSize(500, 250);
36+
setLocationRelativeTo(null);
37+
}
38+
39+
//Action Performed Method
40+
@Override
41+
public void actionPerformed(ActionEvent e) {
42+
new WelcomePage();
43+
dispose();
44+
}
45+
}

Main.java

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import javax.swing.*;
2+
3+
public class Main {
4+
public static void main(String[] args) {
5+
SwingUtilities.invokeLater(new Runnable() {
6+
@Override
7+
public void run() {
8+
new WelcomePage();
9+
}
10+
});
11+
}
12+
}

NumberGuesser.iml

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
</component>
11+
</module>

NumberGuesser.java

+166
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
import javax.swing.*;
2+
import java.awt.*;
3+
import java.awt.event.ActionEvent;
4+
import java.awt.event.ActionListener;
5+
6+
public class NumberGuesser extends JFrame implements ActionListener {
7+
8+
//Initialize counter to track tries remaining
9+
private int counter;
10+
//Initialize score
11+
private int score;
12+
//Generate a random number
13+
static int randomNumber = (int) (Math.random()*101);
14+
//Initialize the components
15+
private JLabel mainLabel;
16+
private JLabel hintLabel;
17+
private JLabel triesLabel;
18+
JButton guessButton;
19+
JButton exitButton;
20+
JTextField guessField;
21+
22+
//Initialize the constructor
23+
NumberGuesser(){
24+
//Set the counter and the score
25+
counter = 10;
26+
score = 1000;
27+
28+
//Initialize the first panel and add the labels
29+
mainLabel = newLabel("Guess The Number!", 50);
30+
hintLabel = newLabel("Enter a number to receive a hint.", 30);
31+
triesLabel = newLabel("Tries Remaining: " + counter, 28);
32+
JPanel panel1 = newPanel();
33+
panel1.add(mainLabel);
34+
panel1.add(hintLabel);
35+
panel1.add(triesLabel);
36+
37+
//Initialize the second panel and add the text field
38+
JPanel panel2 = newPanel();
39+
guessField = new JTextField(5);
40+
guessField.setForeground(Color.WHITE);
41+
guessField.setForeground(Color.BLACK);
42+
guessField.setFont(new Font(null, Font.PLAIN, 20));
43+
panel2.add(guessField);
44+
45+
//Initialize the third panel and add the buttons
46+
guessButton = newButton("Guess");
47+
exitButton = newButton("Exit");
48+
JPanel panel3 = newPanel();
49+
panel3.add(guessButton);
50+
panel3.add(exitButton);
51+
52+
53+
//Add the panels to the frame and customize the frames
54+
add(panel1);
55+
add(panel2);
56+
add(panel3);
57+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
58+
setLayout(new GridLayout(3, 1));
59+
setVisible(true);
60+
setSize(new Dimension(600, 500));
61+
setLocationRelativeTo(null);
62+
setResizable(false);
63+
64+
}
65+
66+
67+
//Method to make and customize a new button
68+
private JButton newButton(String text){
69+
JButton button = new JButton(text);
70+
button.setBackground(Color.BLUE);
71+
button.setForeground(Color.WHITE);
72+
button.setFont(new Font(null, Font.BOLD, 20));
73+
button.setFocusable(false);
74+
button.addActionListener(this);
75+
return button;
76+
}
77+
78+
//Method to make and customize a new panel
79+
private JPanel newPanel(){
80+
JPanel panel = new JPanel();
81+
panel.setBackground(Color.BLACK);
82+
panel.setOpaque(true);
83+
return panel;
84+
}
85+
86+
//Method to make and customize a new label
87+
private JLabel newLabel(String text, int size){
88+
JLabel label = new JLabel();
89+
label.setForeground(Color.WHITE);
90+
label.setFont(new Font(null, Font.PLAIN, size));
91+
label.setText(text);
92+
return label;
93+
}
94+
95+
//Action Performed method
96+
@Override
97+
public void actionPerformed(ActionEvent e) {
98+
99+
//Check if guessButton is pressed
100+
if(e.getSource() == guessButton){
101+
102+
//Check if tries are remaining
103+
if (counter > 1) {
104+
105+
//Check if user input is a number
106+
try {
107+
108+
int userGuess = Integer.parseInt(guessField.getText());
109+
110+
//Check if guess is within the range
111+
if (userGuess >= 0 && userGuess <= 100) {
112+
113+
//Check if guess is correct
114+
if (userGuess == randomNumber) {
115+
116+
hintLabel.setText("That's the correct number!");
117+
guessButton.setEnabled(false);
118+
exitButton.setEnabled(false);
119+
Timer timer = new Timer(1000, e1 -> {
120+
dispose();
121+
JOptionPane.showMessageDialog(null, "Congratulations You Won! Your score is " + score, "You Win", JOptionPane.INFORMATION_MESSAGE);
122+
});
123+
timer.setRepeats(false);
124+
timer.start();
125+
126+
} else if (userGuess > randomNumber) {
127+
//If guess is larger than the actual number
128+
hintLabel.setText("That number is too large...");
129+
score-=100; //Decrease the score by 100
130+
131+
} else if (userGuess < randomNumber) {
132+
//If guess is smaller than the actual number
133+
hintLabel.setText("That number is too small...");
134+
score-=100; //Decrease the score by 100
135+
136+
}
137+
138+
//Decrement the counter
139+
counter--;
140+
141+
} else {
142+
//If guess is out of range
143+
hintLabel.setText("Please enter a number between 0 and 100");
144+
145+
}
146+
147+
} catch (NumberFormatException e1) {
148+
//If input is not a valid number
149+
hintLabel.setText("Please enter a valid number!");
150+
}
151+
152+
//Update the label to show tries remaining
153+
triesLabel.setText("Tries Remaining: " + counter);
154+
155+
} else {
156+
//If no tries left
157+
dispose();
158+
JOptionPane.showMessageDialog(null, "No tries left. Please play again.", "No tries left", JOptionPane.ERROR_MESSAGE);
159+
160+
}
161+
} else if (e.getSource() == exitButton) {
162+
//If exitButton is pressed
163+
dispose();
164+
}
165+
}
166+
}

WelcomePage.java

+82
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
import javax.swing.*;
2+
import java.awt.*;
3+
import java.awt.event.ActionEvent;
4+
import java.awt.event.ActionListener;
5+
6+
public class WelcomePage extends JFrame implements ActionListener {
7+
8+
//Initialize the components
9+
JLabel mainLabel;
10+
JButton playButton;
11+
JButton instructionsButton;
12+
13+
//Initialize the constructor
14+
WelcomePage(){
15+
//Initialize first panel and add main label
16+
mainLabel = newLabel("Welcome to the Number Guessing Game!", 25);
17+
JPanel panel1 = newPanel();
18+
panel1.add(mainLabel);
19+
20+
//Initialize the second panel and add buttons
21+
playButton = newButton("Play");
22+
instructionsButton = newButton("Instructions");
23+
JPanel panel2 = newPanel();
24+
panel2.add(playButton);
25+
panel2.add(instructionsButton);
26+
27+
28+
//Add the panels to the frame and customize it
29+
add(panel1);
30+
add(panel2);
31+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
32+
setLayout(new GridLayout(2, 1));
33+
setSize(500, 500);
34+
setVisible(true);
35+
setLocationRelativeTo(null);
36+
}
37+
38+
//Method to make and customize a new button
39+
private JButton newButton(String text){
40+
JButton button = new JButton(text);
41+
button.setBackground(Color.BLUE);
42+
button.setForeground(Color.WHITE);
43+
button.setFont(new Font(null, Font.BOLD, 20));
44+
button.setFocusable(false);
45+
button.addActionListener(this);
46+
return button;
47+
}
48+
49+
//Method to make and customize a new panel
50+
private JPanel newPanel(){
51+
JPanel panel = new JPanel();
52+
panel.setBackground(Color.BLACK);
53+
panel.setOpaque(true);
54+
panel.setLayout(new FlowLayout());
55+
return panel;
56+
}
57+
58+
//Method to make and customize a new label
59+
private JLabel newLabel(String text, int size){
60+
JLabel label = new JLabel();
61+
label.setForeground(Color.WHITE);
62+
label.setFont(new Font(null, Font.PLAIN, size));
63+
label.setText(text);
64+
return label;
65+
}
66+
67+
//Action Performed Method
68+
@Override
69+
public void actionPerformed(ActionEvent e) {
70+
71+
//If playButton is pressed
72+
if (e.getSource() == playButton){
73+
new NumberGuesser();
74+
dispose();
75+
76+
} else if (e.getSource() == instructionsButton) {
77+
//If instructionsButton is pressed
78+
new Instructions();
79+
dispose();
80+
}
81+
}
82+
}

0 commit comments

Comments
 (0)