forked from dimpeshpanwar/Java-Advance-Programs
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGuessTheNumber.java
More file actions
78 lines (63 loc) · 3.1 KB
/
GuessTheNumber.java
File metadata and controls
78 lines (63 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import java.util.Random;
import java.util.Scanner;
public class GuessTheNumber {
private static final int MIN_RANGE = 1;
private static final int MAX_RANGE = 100;
private static final int MAX_ATTEMPTS = 10;
// Main method where the game execution starts
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
Random random = new Random();
boolean playAgain;
System.out.println("🎉 Welcome to Guess The Number! 🎉");
System.out.println("I will pick a number between " + MIN_RANGE + " and " + MAX_RANGE + ".");
System.out.println("You have " + MAX_ATTEMPTS + " attempts to guess it.");
System.out.println("----------------------------------------");
// Main game loop (do-while ensures the game runs at least once)
do {
int targetNumber = random.nextInt(MAX_RANGE - MIN_RANGE + 1) + MIN_RANGE;
int attemptsUsed = 0;
boolean hasGuessedCorrectly = false;
System.out.println("\nI've picked a new number. Start guessing!");
// Attempt loop
while (attemptsUsed < MAX_ATTEMPTS) {
System.out.print("Attempt " + (attemptsUsed + 1) + "/" + MAX_ATTEMPTS + ". Enter your guess: ");
// Input validation
if (!scanner.hasNextInt()) {
System.out.println("🚨 Invalid input. Please enter a number.");
scanner.next(); // Consume the invalid token
continue;
}
int guess = scanner.nextInt();
attemptsUsed++;
if (guess < MIN_RANGE || guess > MAX_RANGE) {
System.out.println("❌ Your guess is out of range. Stick between " + MIN_RANGE + " and " + MAX_RANGE + ".");
// Don't count this as a meaningful attempt
attemptsUsed--;
continue;
}
if (guess == targetNumber) {
hasGuessedCorrectly = true;
break;
} else if (guess < targetNumber) {
System.out.println("⬇️ Too low! Guess higher.");
} else {
System.out.println("⬆️ Too high! Guess lower.");
}
}
// Game end message
if (hasGuessedCorrectly) {
System.out.println("\n👑 CONGRATULATIONS! You guessed the number " + targetNumber + " in " + attemptsUsed + " attempts!");
} else {
System.out.println("\n😔 GAME OVER. You ran out of attempts.");
System.out.println("The number I was thinking of was " + targetNumber + ".");
}
// Ask to play again
System.out.print("\nDo you want to play again? (yes/no): ");
String playChoice = scanner.next().toLowerCase();
playAgain = playChoice.startsWith("y");
} while (playAgain);
System.out.println("\nThanks for playing! Goodbye. 👋");
scanner.close();
}
}