-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameIntro.java
More file actions
211 lines (166 loc) · 7.66 KB
/
Copy pathGameIntro.java
File metadata and controls
211 lines (166 loc) · 7.66 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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Import necessary libraries
import java.awt.*;
import javax.swing.*;
import java.util.Random;
public class GameIntro {
// Declare instance variables for GUI components
private JFrame mainFrame, gameFrame, instructionsFrame;
private JButton playButton, instructionsButton, quitButton, backButton, checkButton, giveupButton;
private JPanel mainPanel, gamePanel, instructionsPanel, guessPanel;
private JTextField guessField;
private JLabel guessLabel;
// Create a random object to generate a random number between 0 and 100
Random rand = new Random();
private int answer = rand.nextInt(0,101);
// Constructor for the GameIntro class
public GameIntro() {
// Set up main frame
mainFrame = new JFrame("Game Introduction");
mainFrame.setSize(500, 300);
mainFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// Set up main panel
mainPanel = new JPanel(new GridLayout(3, 1, 10, 10));
// Set up play button and add it to the panel
playButton = new JButton("Play");
mainPanel.add(playButton);
// Add action listener using lambda function to show the game window when the play button is clicked
playButton.addActionListener(e -> showGameWindow());
// Set up instructions button and add it to the panel
instructionsButton = new JButton("Instructions");
mainPanel.add(instructionsButton);
// Add action listener using lambda function to show the instructions window when the instructions button is clicked
instructionsButton.addActionListener(e -> showInstructionsWindow());
// Set up quit button and add it to the panel
quitButton = new JButton("Quit");
mainPanel.add(quitButton);
// Add action listener using lambda function to quit the program when the quit button is clicked
quitButton.addActionListener(e -> System.exit(0));
// Add main panel to the frame and make it visible
mainFrame.add(mainPanel);
mainFrame.setVisible(true);
}
// Method to show the game window
private void showGameWindow() {
// Create game Frame and set its properties
gameFrame = new JFrame("Guess the Number Game");
gameFrame.setSize(400, 150);
gameFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
gameFrame.setLocationRelativeTo(null);
// Create game panel
gamePanel = new JPanel();
// Create guess panel and set layout
guessPanel = new JPanel(new FlowLayout());
// Create guess label and add it to guess panel
guessLabel = new JLabel("Enter your guess (0-100): ");
guessPanel.add(guessLabel);
// Create guess text field and add it to guess panel
guessField = new JTextField(5);
guessPanel.add(guessField);
// Create check button and add it to the guessPanel
checkButton = new JButton("Check");
guessPanel.add(checkButton);
// Add action listener using lambda function to call checkGuess() function when button is clicked
checkButton.addActionListener(e -> checkGuess());
// Add guess panel to game panel
gamePanel.add(guessPanel, BorderLayout.CENTER);
// Create back button and add action listener to close game window and show main window
backButton = new JButton("Back");
// Set it's location to the bottom of the panel
gamePanel.add(backButton, BorderLayout.SOUTH);
// Add action listener using lambda function dispose of frame and show main frame when the back button is pressed
backButton.addActionListener(e -> {
gameFrame.dispose();
mainFrame.setVisible(true);
});
giveupButton = new JButton("Give Up");
// Set it's location to the bottom of the panel
gamePanel.add(giveupButton, BorderLayout.SOUTH);
giveupButton.addActionListener(e -> giveUp());
// Add game panel to game frame
gameFrame.add(gamePanel);
gameFrame.setVisible(true);
}
// Method to show instructions
private void showInstructionsWindow() {
// Create new frame for instructions
JFrame instructionsFrame = new JFrame("Instructions");
instructionsFrame.setSize(300, 150);
// Create new instructions panel for instructions
JPanel instructionsPanel = new JPanel(new BorderLayout());
// Create label with instructions. Add some HTML to it to make it a certain size
JLabel instructionsLabel = new JLabel("<html><body style='width: 225px;'>Guess a number between 1-100. You have unlimited guesses. " +
"</body></html>");
//Center the label on the Instructions panel
instructionsPanel.add(instructionsLabel, BorderLayout.CENTER);
// Create back button to go back to main menu
JButton backButton = new JButton("Back");
backButton.addActionListener(e -> instructionsFrame.dispose());
instructionsPanel.add(backButton, BorderLayout.SOUTH);
// Add instructions panel to instructions frame
instructionsFrame.add(instructionsPanel);
instructionsFrame.setVisible(true);
}
// Method to check the user's guess
private void checkGuess() {
try {
// Parse the integer value of the user's guess from the guess field
int guess = Integer.parseInt(guessField.getText());
// Check if the guess is out of range
if (guess < 1 || guess > 100) {
// Display an error message and clear the guess field
JOptionPane.showMessageDialog(gameFrame, "Please enter a number between 1-100.");
guessField.setText("");
}
// Check if the guess is correct
else if (guess == answer) {
// Say that the user got it right and give an option for the user to play again.
int option = JOptionPane.showConfirmDialog(gameFrame, "You got it! The answer is " + answer + ".\nDo you want to play again?", "Play again?", JOptionPane.YES_NO_OPTION);
// Check if the user wants to play again
if (option == 0) {
// Generate a new random answer and clear the guess field
Random rand = new Random();
answer = rand.nextInt(101);
guessField.setText("");
} else {
// Close the game window and show the main window
gameFrame.dispose();
mainFrame.setVisible(true);
}
}
// Check if the guess is too low
else if (guess < answer) {
// Display a message saying the guess is too low and clear the guess field
JOptionPane.showMessageDialog(gameFrame, "Too low! Try again.");
guessField.setText("");
}
else {
// Display a message saying the guess is too high and clear the guess field
JOptionPane.showMessageDialog(gameFrame, "Too high! Try again.");
guessField.setText("");
}
} catch (NumberFormatException ex) {
// Display an error message for an invalid guess(like 9s) and clear the guess field
JOptionPane.showMessageDialog(gameFrame, "Please enter a valid number.");
guessField.setText("");
}
}
// Method to run if the user clicks the Giveup Button
private void giveUp() {
JOptionPane.showMessageDialog(gameFrame, "The answer was " + answer + ".");
// prompt the user to play again or quit
int option = JOptionPane.showConfirmDialog(gameFrame, "Do you want to play again?", "Play again?", JOptionPane.YES_NO_OPTION);
// if the user wants to play again, set up a new game
if (option == 0) {
answer = rand.nextInt(101); // set a new random answer
guessField.setText(""); // clear the guess field
// if the user wants to quit, close the game window and show the main window
} else {
gameFrame.dispose(); // close the game window
mainFrame.setVisible(true); // show the main window
}
}
// Method that runs the program
public static void main(String[] args) {
new GameIntro();
}
}