Repository navigation
Expand file tree
/
Copy pathNumberRecall.java
More file actions
57 lines (47 loc) · 1.76 KB
/
Copy pathNumberRecall.java
File metadata and controls
57 lines (47 loc) · 1.76 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
import java.util.Scanner;
public class NumberRecall {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to the Number Recall game!");
System.out.println("You will be shown a series of numbers and must recall them.");
System.out.println("Are you ready? (Press Enter to start)");
scanner.nextLine();
int level = 1;
boolean gameOver = false;
while (!gameOver) {
System.out.println("Level " + level);
String targetNumber = generateTargetNumber(level);
System.out.println("Memorize the number: " + targetNumber);
sleep(2000);
clearScreen();
System.out.println("Enter the number you recalled:");
String userInput = scanner.nextLine();
if (userInput.equals(targetNumber)) {
System.out.println("Correct!");
level++;
} else {
System.out.println("Incorrect! Game Over.");
gameOver = true;
}
}
System.out.println("Game Over. You reached level " + (level - 1));
scanner.close();
}
private static String generateTargetNumber(int level) {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < level; i++) {
sb.append((int) (Math.random() * 10));
}
return sb.toString();
}
private static void sleep(int milliseconds) {
try {
Thread.sleep(milliseconds);
} catch (InterruptedException e) {
}
}
private static void clearScreen() {
System.out.print("\033[H\033[2J");
System.out.flush();
}
}