|
| 1 | +import javax.swing.*; |
| 2 | +import java.awt.*; |
| 3 | +import java.awt.event.ActionEvent; |
| 4 | +import java.awt.event.ActionListener; |
| 5 | +import java.util.Random; |
| 6 | + |
| 7 | +public class NumberGuessingGameGUI extends JFrame implements ActionListener { |
| 8 | + private JTextField guessField; |
| 9 | + private JButton guessButton; |
| 10 | + private JTextArea infoArea; |
| 11 | + private int randomNumber; |
| 12 | + private int maxAttempts = 10; |
| 13 | + private int attemptsLeft; |
| 14 | + |
| 15 | + public NumberGuessingGameGUI() { |
| 16 | + setTitle("Number Guessing Game"); |
| 17 | + setSize(400, 300); |
| 18 | + setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); |
| 19 | + setLayout(new BorderLayout()); |
| 20 | + |
| 21 | + guessField = new JTextField(10); |
| 22 | + guessButton = new JButton("Guess"); |
| 23 | + guessButton.addActionListener(this); |
| 24 | + infoArea = new JTextArea(); |
| 25 | + infoArea.setEditable(false); |
| 26 | + |
| 27 | + JPanel inputPanel = new JPanel(); |
| 28 | + inputPanel.add(new JLabel("Enter your guess: ")); |
| 29 | + inputPanel.add(guessField); |
| 30 | + inputPanel.add(guessButton); |
| 31 | + |
| 32 | + add(inputPanel, BorderLayout.NORTH); |
| 33 | + add(new JScrollPane(infoArea), BorderLayout.CENTER); |
| 34 | + |
| 35 | + initGame(); |
| 36 | + } |
| 37 | + |
| 38 | + private void initGame() { |
| 39 | + Random random = new Random(); |
| 40 | + randomNumber = random.nextInt(100) + 1; |
| 41 | + attemptsLeft = maxAttempts; |
| 42 | + infoArea.setText("I've selected a number between 1 and 100.\nTry to guess it in " + |
| 43 | + maxAttempts + " attempts or less.\n"); |
| 44 | + guessField.setText(""); |
| 45 | + guessButton.setEnabled(true); |
| 46 | + } |
| 47 | + |
| 48 | + @Override |
| 49 | + public void actionPerformed(ActionEvent e) { |
| 50 | + if (attemptsLeft > 0) { |
| 51 | + try { |
| 52 | + int userGuess = Integer.parseInt(guessField.getText()); |
| 53 | + attemptsLeft--; |
| 54 | + |
| 55 | + if (userGuess == randomNumber) { |
| 56 | + infoArea.append("Congratulations! You guessed the number " + randomNumber + |
| 57 | + " in " + (maxAttempts - attemptsLeft) + " attempts.\n"); |
| 58 | + infoArea.append("YOUR SCORE IS " + attemptsLeft); |
| 59 | + guessButton.setVisible(false); |
| 60 | + } else if (userGuess < randomNumber) { |
| 61 | + infoArea.append("Try a higher number. Attempts left: " + attemptsLeft + "\n"); |
| 62 | + } else { |
| 63 | + infoArea.append("Try a lower number. Attempts left: " + attemptsLeft + "\n"); |
| 64 | + } |
| 65 | + |
| 66 | + if (attemptsLeft == 0) { |
| 67 | + infoArea.append("Game Over! The number was " + randomNumber + ".\n"); |
| 68 | + guessButton.setEnabled(false); |
| 69 | + } |
| 70 | + } catch (NumberFormatException ex) { |
| 71 | + infoArea.append("Invalid input. Please enter a valid number.\n"); |
| 72 | + } |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public static void main(String[] args) { |
| 77 | + SwingUtilities.invokeLater(() -> { |
| 78 | + NumberGuessingGameGUI game = new NumberGuessingGameGUI(); |
| 79 | + game.setVisible(true); |
| 80 | + }); |
| 81 | + } |
| 82 | +} |
0 commit comments