AJP - Microproject 103116
AJP - Microproject 103116
Academic Year-2024-25
A project report on
Mr.Bharti M.B
DEPARTMENT OF COMPUTER ENGINEERING
Under guidance of H.O.D principle
Mr.Bharti M.B Mr.Bharti M.B Mr.Bharti S.M
1
KARMVER SHANKARRAO KALE EDUCATION SOCIETY’S
GAUTAM POLYTECHNIC INSTITUE
GAUTAMNAGAR-423602
Academic Year-2024-25
2
Introduction
Project Overview
In this game, the computer will select a random number, and players will
input their guesses. After each guess, the program will provide feedback,
indicating whether the guess is too high, too low, or correct. This not only
makes the game challenging but also encourages strategic thinking as
players refine their guesses based on the feedback received.
3
Source Code
import javax.swing.*;
import java.awt.*; import
java.awt.event.*; import
java.util.Random;
public NumberGuessingGame() {
4
// Initialize components guessInput
= new JTextField(10); guessButton =
new JButton("Guess"); restartButton =
new JButton("Restart");
messageLabel = new JLabel("Guess a number between 1
and 100"); attemptsLabel = new JLabel("Attempts left:
7"); scoreLabel = new JLabel("Score: 0");
add(messageLabel);
add(guessInput); add(guessButton);
add(restartButton);
add(attemptsLabel);
add(scoreLabel);
// Set difficulty
maxAttempts = 7; random
= new Random();
resetGame();
5
// Guess button action
guessButton.addActionListener(new ActionListener() {
});
restartButton.addActionListener(new ActionListener() {
});
6
{ int guess =
Integer.parseInt(guessInput.getText());
return;
}
attempts--
;
if (guess == randomNumber) {
messageLabel.setText("You guessed it right!");
score += (maxAttempts - attempts) * 10;
scoreLabel.setText("Score: " + score);
guessButton.setEnabled(false); } else if (guess <
randomNumber) { messageLabel.setText("Too
low! Try again.");
} else {
7
messageLabel.setText("Too high! Try again.");
guessButton.setEnabled(false);
}
} catch (NumberFormatException ex) {
messageLabel.setText("Invalid input. Enter a number.");
8
attemptsLabel.setText("Attempts left: " + attempts);
guessInput.setText("");
guessButton.setEnabled(true);
NumberGuessingGame().setVisible(true);
});
9
Output
10
Conclusion
11
generation, which enhance critical thinking and debugging
skills.
Swing Components
12
Classes
Constructor
NumberGuessingGame():
13
Methods
1. makeGuess():
o This method is called when the user clicks the
"Guess" button. o It checks if the user has remaining
attempts and processes their input. o The input is
validated to ensure it’s a number between 1 and 100. o If
the guess is correct, it updates the message and score. If
the guess is incorrect, it provides feedback on whether
the guess is too low or too high. o If attempts run out
without a correct guess, it informs the user that the game
is over.
2. resetGame():
o This method resets the game state for a new round. o
It generates a new random number and resets the
number of attempts. o The message and attempt labels
are updated, and the input field is cleared.
3. main(String[] args):
o The main method serves as the entry point for the
program. o It uses SwingUtilities.invokeLater() to
ensure that the
14
GUI updates happen on the Event Dispatch Thread,
which is the correct way to manage Swing
components.
GUI Components
Other Variables
15
• int maxAttempts: The maximum number of attempts
allowed (set to 7).
• int score: The user's score based on the number of attempts
used
16
Algorithm:
17
18
Reference
www.tutorialpoint.com www.javatpoint.com
19