JAVA MINI PROJECT
GUESS THE NUMBER GAME
Aim
To develop a Java console application called "Guess the Number Game," where a player attempts to guess a
randomly generated number within a specified range (0 to 10). The player has a maximum of 5 attempts to
guess the number correctly, and each correct guess earns 10 points. After each game, the user is informed of
their score and whether they passed or failed. The game offers an option to play again or exit.
Algorithm
1. Initialize Game:
o Create a Scanner object to handle user input.
o Create a Random object to generate a random number for guessing.
2. Display Welcome Message:
o Display introductory messages and explain the rules of the game.
3. Game Loop:
o Repeat steps 4–10 while the user chooses to play again.
4. Generate Random Number:
o Use the Random object to generate a random number between 0 and 10. This is the number the
player will attempt to guess.
5. Initialize Variables:
o Set attempts to 5 (the maximum number of attempts).
o Set score to 0 (initial score).
o Set guessedCorrectly to false (to check if the number is guessed correctly).
6. Attempt Loop:
o For each attempt from 1 to 5:
Prompt the player to enter their guess.
If the guess is correct:
Set guessedCorrectly to true.
Increase score by 10.
Display "Correct! You've earned 10 points."
Break the loop to stop further attempts.
Else If the guess is lower than the target number:
Display "Too low! Try again."
Else (the guess is higher):
Display "Too high! Try again."
7. Display Game Results:
o Display "Game Over."
o Show the player’s score out of a maximum of 50.
o If score is 21 or higher, display "Congratulations! You're a winner!"
o Else, display "You failed the game. Try again!"
8. Play Again Prompt:
o Ask the player if they want to play again.
o If the player enters "yes," restart the game by looping back to step 4.
o Else (player enters "no"), exit the game.
9. Exit Game:
o Display a "Thank you for playing!" message.
o Close the Scanner object to end the program.
Program:
[Link];
[Link];
public class GuessTheNumberGame {
public static void main(String[] args) {
Scanner scanner = new Scanner([Link]);
Random random = new Random();
booleanplayAgain;
[Link]("Welcome to the Guess the Number Game!");
do {
intnumberToGuess = [Link](11);
int attempts = 5;
int score = 0;
booleanguessedCorrectly = false;
[Link]("\nI have selected a number between 0 and 10.");
[Link]("You have 5 chances to guess it correctly. Each correct guess gives you 10
points.");
for (int i = 1; i <= attempts; i++) {
[Link]("Attempt " + i + ": Enter your guess: ");
int guess = [Link]();
if (guess == numberToGuess) {
guessedCorrectly = true;
score += 10;
[Link]("Correct! You've earned 10 points.");
break;
} else if (guess <numberToGuess) {
[Link]("Too low! Try again.");
} else {
[Link]("Too high! Try again.");
}
}
[Link]("\nGame Over.");
[Link]("Your score: " + score + " out of 10");
if (score >= 10) {
[Link]("Congratulations! You're a winner!");
} else {
[Link]("You failed the game. Try again!");
[Link]("\nWould you like to play again? (yes/no): ");
String response = [Link]().toLowerCase();
playAgain = [Link]("yes");
} while (playAgain);
[Link]("Thank you for playing! Goodbye.");
[Link]();
}
Output:
Welcome to the Guess the Number Game!
I have selected a number between 0 and 10.
You have 5 chances to guess it correctly. Each correct guess gives you 10 points.
Attempt 1: Enter your guess: 3
Too high! Try again.
Attempt 2: Enter your guess: 2
Too high! Try again.
Attempt 3: Enter your guess: 1
Too high! Try again.
Attempt 4: Enter your guess: 0
Correct! You've earned 10 points.
Game Over.
Your score: 10 out of 10
Congratulations! You're a winner!
Would you like to play again? (yes/no): no
Thank you for playing! Goodbye.
Result:
The "Guess the Number Game" project successfully implements the game mechanics as intended. The game
initializes correctly, generates a random number, allows for user guesses, tracks the score, and provides an option to play
again. The output accurately reflects the game’s flow, providing feedback on guesses and final results. Overall, the
implementation is effective and user-friendly.