|
| 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 | +} |
0 commit comments