Repository navigation
Expand file tree
/
Copy pathMemoryNumberGame.java
More file actions
89 lines (62 loc) · 2.45 KB
/
Copy pathMemoryNumberGame.java
File metadata and controls
89 lines (62 loc) · 2.45 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
import java.util.*;
public class MemoryNumberGame {
public static void main(String[] args) throws InterruptedException {
Scanner sc = new Scanner(System.in);
Random random = new Random();
int score = 0;
int rounds = 5;
System.out.println("=================================");
System.out.println(" MEMORY NUMBER GAME");
System.out.println("=================================");
for (int round = 1; round <= rounds; round++) {
int length = round + 4;
int[] numbers = new int[length];
// Generate random numbers
for (int i = 0; i < length; i++) {
numbers[i] = random.nextInt(10);
}
System.out.println("\nRound " + round);
System.out.println("Remember:");
for (int number : numbers) {
System.out.print(number + " ");
}
System.out.println();
// Give time to remember
Thread.sleep(4000);
// Clear screen
for (int i = 0; i < 30; i++) {
System.out.println();
}
System.out.println("Enter the numbers:");
String answer = sc.nextLine().trim();
// Create correct answer
StringBuilder correctAnswer = new StringBuilder();
for (int number : numbers) {
correctAnswer.append(number).append(" ");
}
String correct = correctAnswer.toString().trim();
// Check answer
if (answer.equals(correct)) {
System.out.println("🎉 Correct!");
score++;
} else {
System.out.println("❌ Wrong!");
System.out.println("Correct answer: " + correct);
}
// Score is always out of 5
System.out.println("Score: " + score + "/" + rounds);
}
System.out.println("\n=================================");
System.out.println(" GAME OVER");
System.out.println("=================================");
System.out.println("Final Score: " + score + "/" + rounds);
if (score == 5) {
System.out.println("🏆 Perfect Memory!");
} else if (score >= 3) {
System.out.println("🎉 Great Memory!");
} else {
System.out.println("💪 Keep practicing!");
}
sc.close();
}
}