|
| 1 | +import javax.swing.*; |
| 2 | +import java.awt.*; |
| 3 | +import java.awt.event.ActionEvent; |
| 4 | +import java.awt.event.ActionListener; |
| 5 | + |
| 6 | +public class RiddleGame extends JFrame { |
| 7 | + private String[] riddles = { |
| 8 | + "I speak without a mouth and hear without ears. I have no body, but I come alive with wind. What am I?", |
| 9 | + "I'm not alive, but I can grow; I don't have lungs, but I need air; I don't have a mouth, but water kills me. What am I?", |
| 10 | + "I have keys but can't open locks. What am I?", |
| 11 | + "I am taken from a mine, and shut up in a wooden case, from which I am never released, and yet I am used by almost every person. What am I?", |
| 12 | + "The more you take, the more you leave behind. What am I?", |
| 13 | + "I have cities but no houses, forests but no trees, and rivers but no water. What am I?", |
| 14 | + "I'm tall when I'm young and short when I'm old. What am I?" |
| 15 | + }; |
| 16 | + private String[] answers = { |
| 17 | + "An echo", |
| 18 | + "A fire", |
| 19 | + "A piano", |
| 20 | + "Pencil lead (graphite)", |
| 21 | + "Footsteps", |
| 22 | + "A map", |
| 23 | + "A candle" |
| 24 | + }; |
| 25 | + |
| 26 | + private int currentRiddleIndex = 0; |
| 27 | + private JLabel riddleLabel; |
| 28 | + private JTextField answerField; |
| 29 | + private JButton submitButton; |
| 30 | + |
| 31 | + public RiddleGame() { |
| 32 | + setTitle("Riddle Game"); |
| 33 | + setSize(400, 200); |
| 34 | + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 35 | + setLayout(new BorderLayout()); |
| 36 | + |
| 37 | + riddleLabel = new JLabel(riddles[currentRiddleIndex]); |
| 38 | + answerField = new JTextField(20); |
| 39 | + submitButton = new JButton("Submit"); |
| 40 | + |
| 41 | + JPanel centerPanel = new JPanel(); |
| 42 | + centerPanel.setLayout(new GridLayout(3, 1)); |
| 43 | + centerPanel.add(riddleLabel); |
| 44 | + centerPanel.add(answerField); |
| 45 | + centerPanel.add(submitButton); |
| 46 | + |
| 47 | + add(centerPanel, BorderLayout.CENTER); |
| 48 | + |
| 49 | + submitButton.addActionListener(new ActionListener() { |
| 50 | + @Override |
| 51 | + public void actionPerformed(ActionEvent e) { |
| 52 | + checkAnswer(); |
| 53 | + } |
| 54 | + }); |
| 55 | + } |
| 56 | + |
| 57 | + private void checkAnswer() { |
| 58 | + String userAnswer = answerField.getText().trim().toLowerCase(); |
| 59 | + String correctAnswer = answers[currentRiddleIndex].toLowerCase(); |
| 60 | + |
| 61 | + if (userAnswer.equals(correctAnswer)) { |
| 62 | + JOptionPane.showMessageDialog(this, "Correct!"); |
| 63 | + } else { |
| 64 | + JOptionPane.showMessageDialog(this, "Incorrect. Try again."); |
| 65 | + } |
| 66 | + |
| 67 | + currentRiddleIndex++; |
| 68 | + if (currentRiddleIndex < riddles.length) { |
| 69 | + riddleLabel.setText(riddles[currentRiddleIndex]); |
| 70 | + answerField.setText(""); |
| 71 | + } else { |
| 72 | + JOptionPane.showMessageDialog(this, "Congratulations! You've completed all the riddles."); |
| 73 | + System.exit(0); |
| 74 | + } |
| 75 | + } |
| 76 | + |
| 77 | + public static void main(String[] args) { |
| 78 | + SwingUtilities.invokeLater(() -> { |
| 79 | + RiddleGame game = new RiddleGame(); |
| 80 | + game.setVisible(true); |
| 81 | + }); |
| 82 | + } |
| 83 | +} |
0 commit comments