Repository navigation
Expand file tree
/
Copy pathWordScrambleGame.java
More file actions
113 lines (91 loc) · 3.05 KB
/
Copy pathWordScrambleGame.java
File metadata and controls
113 lines (91 loc) · 3.05 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
import java.util.*;
public class WordScrambleGame {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random random = new Random();
// 30 words
String[] words = {
"JAVA",
"PYTHON",
"COMPUTER",
"PROGRAM",
"KEYBOARD",
"MONITOR",
"INTERNET",
"DATABASE",
"SOFTWARE",
"HARDWARE",
"ALGORITHM",
"VARIABLE",
"FUNCTION",
"ARRAY",
"STRING",
"OBJECT",
"CLASS",
"METHOD",
"LOOP",
"RECURSION",
"NETWORK",
"SERVER",
"BROWSER",
"MOBILE",
"WEBSITE",
"CODING",
"DEVELOPER",
"DATABASE",
"SECURITY",
"APPLICATION"
};
// To avoid repeating questions
ArrayList<String> wordList = new ArrayList<>(Arrays.asList(words));
Collections.shuffle(wordList, random);
int score = 0;
System.out.println("=================================");
System.out.println(" WORD SCRAMBLE GAME");
System.out.println("=================================");
System.out.println("You have 10 questions.");
System.out.println();
// Ask 10 questions
for (int i = 0; i < 10; i++) {
String word = wordList.get(i);
// Scramble the word
ArrayList<Character> letters = new ArrayList<>();
for (char ch : word.toCharArray()) {
letters.add(ch);
}
Collections.shuffle(letters, random);
StringBuilder scrambled = new StringBuilder();
for (char ch : letters) {
scrambled.append(ch);
}
System.out.println("Question " + (i + 1));
System.out.println("Scrambled word: \"" + scrambled + "\"");
System.out.print("Your answer: ");
String answer = sc.nextLine().trim();
// Check answer
if (answer.equalsIgnoreCase(word)) {
System.out.println("✅ Correct!");
score++;
} else {
System.out.println("❌ Wrong!");
System.out.println("Correct answer: " + word);
}
System.out.println();
}
// Final result
System.out.println("=================================");
System.out.println(" GAME OVER");
System.out.println("=================================");
System.out.println("Your score: " + score + "/10");
if (score == 10) {
System.out.println("🏆 Excellent! Perfect score!");
} else if (score >= 7) {
System.out.println("🎉 Great job!");
} else if (score >= 5) {
System.out.println("👍 Good attempt!");
} else {
System.out.println("💪 Keep practicing!");
}
sc.close();
}
}