-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordGame.java
More file actions
126 lines (98 loc) · 3.02 KB
/
Copy pathWordGame.java
File metadata and controls
126 lines (98 loc) · 3.02 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
import java.io.File;
import java.io.FileNotFoundException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Scanner;
public class WordGame {
/**
*
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
int wordCount = countWords("words.txt");
String[] words = readWords("words.txt", wordCount);
String gameWord = getGameWord(words);
System.out.println(gameWord);
char[] letters = gameWord.toCharArray();
Character[] letters2 = new Character[letters.length];
for (int i = 0; i < letters.length; i++) {
letters2[i] = letters[i];
}
Collections.shuffle(Arrays.asList(letters2));
System.out.println();
for (int i = 0; i < letters2.length; i++) {
System.out.print(letters2[i] + " ");
}
System.out.println();
}
/**
* Count words in file
* @param filename
* @return the count of the words:
* @throws FileNotFoundException
*/
public static int countWords(String filename) throws FileNotFoundException {
File f = new File(filename);
Scanner fileIn = new Scanner(f);
int count = 0;
while (fileIn.hasNext()) {
fileIn.next();
count++;
}
fileIn.close();
return count;
}
/**
*
* @param filename
* @param wordCount
* @return the array of the Strings
* @throws FileNotFoundException
*/
public static String[] readWords(String filename, int wordCount) throws FileNotFoundException {
File f = new File(filename);
Scanner fileIn = new Scanner(f);
String[] words = new String[wordCount];
int count = 0;
while (fileIn.hasNext()) {
words[count] = fileIn.next();
count++;
}
fileIn.close();
return words;
}
/**
*
* @param words
* @return the gameWords
*/
public static String getGameWord(String[] words) {
Collections.shuffle(Arrays.asList(words));
for (int i = 0; i < words.length; i++) {
if (words[i].length() == 7) {
if (checkUniqueLetters(words[i])) {
return words[i];
}
}
}
return ""; // There are no 7 unique letter words
}
/**
*
* @param word
* @return boolean if uniqueLetters matches:
*/
public static boolean checkUniqueLetters(String word) {
char[] letters = word.toCharArray();
boolean[] seen = new boolean[26]; // To track seen letters
for (char letter : letters) {
int index = Character.toLowerCase(letter) - 'a'; // Normalize to lowercase
if (index < 0 || index >= 26 || seen[index]) {
return false; // Not a valid letter or already seen
}
seen[index] = true; // Mark letter as seen
}
return true; // All letters are unique
}
}