ACN Micro Project
ACN Micro Project
ACN Micro Project
P POLYTECHNIC, KOPARGAON
A PROJECT ON
1
Department Of Computer Technology S.K.B.P.
Polytechnic Kopargaon
CERTIFICATE
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() {
// Set up JFrame
setTitle("Number Guessing Game");
setSize(400, 300);
setDefaultCloseOperation(EXIT_ON_CLOSE);
setLayout(new FlowLayout());
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() {
@Override
public void actionPerformed(ActionEvent e) {
makeGuess();
}
});
6
private void makeGuess() {
if (attempts > 0) { try
{
int guess = Integer.parseInt(guessInput.getText());
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.");
}
8
attemptsLabel.setText("Attempts left: " + attempts);
guessInput.setText("");
guessButton.setEnabled(true);
}
9
Output
10
Conclusion
11
Swing Components
JButton: Two buttons for making a guess and restarting the game.
Classes
Constructor
NumberGuessingGame():
12
o This is the constructor for the NumberGuessingGame
class. It initializes the JFrame, sets its title, size, and
default close operation, and arranges the layout. o It
initializes the various GUI components (text fields,
buttons, and labels).
o The game starts by setting the maximum number of
attempts and generating a random number for the user
to guess. o Action listeners for the guess and restart
buttons are added to handle user interactions.
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.
13
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
GUI updates happen on the Event Dispatch Thread,
which is the correct way to manage Swing
components.
GUI Components
14
• JLabel scoreLabel: Displays the current score.
Other Variables
15
Algorithm:
16
Reference
www.tutorialpoint.com www.javatpoint.com
17