-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExercise3.java
More file actions
183 lines (160 loc) · 8.13 KB
/
Copy pathExercise3.java
File metadata and controls
183 lines (160 loc) · 8.13 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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
package chapter3;
import java.util.Scanner;
/**
* ============================================================
* EXERCISE 3 — Number Guessing Game 🎲
* ============================================================
*
* Build a game where:
* 1. Computer picks a random number between 1 and 100
* 2. User guesses the number
* 3. Program says "Too high!", "Too low!", or "Correct!"
* 4. Tracks number of attempts
* 5. Asks if user wants to play again
*
* Concepts used from this chapter:
* - if-else (comparing guess to answer)
* - while loop (keep guessing until correct)
* - do-while (play again loop)
* - break (exit when correct)
* - Scanner (user input)
*
* TRY IT YOURSELF FIRST! Then compare with the solution below.
* ============================================================
*/
public class Exercise3 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String playAgain;
System.out.println("╔══════════════════════════════════════╗");
System.out.println("║ 🎲 NUMBER GUESSING GAME 🎲 ║");
System.out.println("║ Guess a number between 1 and 100 ║");
System.out.println("╚══════════════════════════════════════╝");
do {
// ============================================================
// STEP 1: Generate a random number between 1 and 100
// ============================================================
// Math.random() returns a double between 0.0 (inclusive) and 1.0 (exclusive)
// Multiply by 100 → 0.0 to 99.99
// Add 1 → 1.0 to 100.99
// Cast to int → 1 to 100
int secretNumber = (int) (Math.random() * 100) + 1;
int attempts = 0;
int maxAttempts = 7; // For 1-100, you can always win in 7 tries with binary search!
boolean won = false;
System.out.println("\n🎯 I'm thinking of a number between 1 and 100.");
System.out.println(" You have " + maxAttempts + " attempts. Good luck!\n");
// ============================================================
// STEP 2: Game loop — keep guessing
// ============================================================
while (attempts < maxAttempts) {
attempts++;
System.out.print("Attempt " + attempts + "/" + maxAttempts + " → Your guess: ");
// Input validation
if (!sc.hasNextInt()) {
System.out.println(" ❌ Please enter a valid number!");
sc.next(); // clear invalid input
attempts--; // don't count invalid attempt
continue;
}
int guess = sc.nextInt();
// ============================================================
// STEP 3: Compare guess with secret number
// ============================================================
if (guess < 1 || guess > 100) {
System.out.println(" ⚠️ Please guess between 1 and 100!");
attempts--; // don't count out-of-range attempt
} else if (guess < secretNumber) {
System.out.println(" 📈 Too LOW! Go higher.");
// Hint: show range
if (secretNumber - guess > 20) {
System.out.println(" (Way too low!)");
}
} else if (guess > secretNumber) {
System.out.println(" 📉 Too HIGH! Go lower.");
if (guess - secretNumber > 20) {
System.out.println(" (Way too high!)");
}
} else {
// guess == secretNumber
won = true;
System.out.println(" 🎉 CORRECT! You got it!");
break; // exit the loop
}
// Show remaining attempts
int remaining = maxAttempts - attempts;
if (remaining > 0 && !won) {
System.out.println(" (" + remaining + " attempts remaining)");
}
}
// ============================================================
// STEP 4: Game over — show results
// ============================================================
System.out.println("\n──────────────────────────────────────");
if (won) {
// Rate performance
String rating;
if (attempts == 1) {
rating = "🏆 IMPOSSIBLE! Lucky guess!";
} else if (attempts <= 3) {
rating = "⭐ AMAZING! You're a genius!";
} else if (attempts <= 5) {
rating = "👍 GREAT JOB!";
} else {
rating = "😅 Close one!";
}
System.out.println("You won in " + attempts + " attempt" + (attempts > 1 ? "s" : "") + "!");
System.out.println(rating);
} else {
System.out.println("💀 GAME OVER! You ran out of attempts.");
System.out.println("The number was: " + secretNumber);
}
System.out.println("──────────────────────────────────────");
// ============================================================
// STEP 5: Play again?
// ============================================================
sc.nextLine(); // consume leftover newline
System.out.print("\nPlay again? (yes/no): ");
playAgain = sc.nextLine();
} while (playAgain.equalsIgnoreCase("yes") || playAgain.equalsIgnoreCase("y"));
System.out.println("\nThanks for playing! 👋");
sc.close();
// ============================================================
// EXPECTED OUTPUT (example run):
// ============================================================
/*
* ╔══════════════════════════════════════╗
* ║ 🎲 NUMBER GUESSING GAME 🎲 ║
* ║ Guess a number between 1 and 100 ║
* ╚══════════════════════════════════════╝
*
* 🎯 I'm thinking of a number between 1 and 100.
* You have 7 attempts. Good luck!
*
* Attempt 1/7 → Your guess: 50
* 📉 Too HIGH! Go lower.
* (6 attempts remaining)
* Attempt 2/7 → Your guess: 25
* 📈 Too LOW! Go higher.
* (5 attempts remaining)
* Attempt 3/7 → Your guess: 37
* 📈 Too LOW! Go higher.
* (4 attempts remaining)
* Attempt 4/7 → Your guess: 42
* 🎉 CORRECT! You got it!
*
* ──────────────────────────────────────
* You won in 4 attempts!
* 👍 GREAT JOB!
* ──────────────────────────────────────
*/
// ============================================================
// BONUS CHALLENGES:
// ============================================================
// 1. Add difficulty levels (Easy: 1-50, Medium: 1-100, Hard: 1-500)
// 2. Track best score across multiple games
// 3. Add a hint system ("The number is even/odd")
// 4. Show a progress bar of attempts used
// ============================================================
}
}