-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordGuessGame.java
More file actions
64 lines (51 loc) · 2.15 KB
/
WordGuessGame.java
File metadata and controls
64 lines (51 loc) · 2.15 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
import javax.swing.*;
import java.awt.*;
import java.util.Random;
public class WordGuessGame extends Game {
private String[] wordList = {
"array", "linkedlist", "stack", "queue", "deque"
};
public void start() {
// for a random word
Random random = new Random();
String wordToGuess = wordList[random.nextInt(wordList.length)];
// for starting the game
JFrame gameFrame = new JFrame("Guess the Linear Data Structure Name :");
gameFrame.setSize(800, 700);
gameFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
gameFrame.getContentPane().setBackground(Color.decode("#424242"));
// Panel to hold components
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(3, 1));
// text field for input
JTextField guessField = new JTextField();
guessField.setFont(new Font("Arial", Font.BOLD, 22));
panel.add(guessField);
// Label for instructions and results
JLabel messageLabel = new JLabel("Guess the Linear Data Structure", JLabel.CENTER);
messageLabel.setFont(new Font("Arial", Font.BOLD, 22));
panel.add(messageLabel);
// Button to submit guess
JButton guessButton = new JButton("Submit Guess");
guessButton.setFont(new Font("Arial", Font.BOLD, 22));
// Action listener for button
guessButton.addActionListener(e -> {
String userGuess = guessField.getText().toLowerCase();
// Check if the guess is correct
if (userGuess.equals(wordToGuess)) {
messageLabel.setText("Correct! The word was " + wordToGuess + ".");
guessButton.setEnabled(false);
displayMessage(gameFrame, "You guessed the word! The word was: " + wordToGuess);
} else {
messageLabel.setText("Wrong guess! Try again.");
}
});
panel.add(guessButton);
// Adding panel to frame
gameFrame.add(panel, BorderLayout.CENTER);
gameFrame.setVisible(true);
}
public static void main(String[] args) {
new WordGuessGame().start();
}
}