-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerator.java
86 lines (75 loc) · 2.32 KB
/
Generator.java
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
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Random;
/**
* @author Zhihui Zhang
*/
public class Generator implements IGenerator {
private WordGuesser w;
private ArrayList<String> wordlist = new ArrayList<>();
public Generator(WordGuesser w) {
this.w = w;
}
/**
* load words from enDictionary.txt to an arraylist
*
* @return a list of words that consists of 5 characters
*/
public ArrayList<String> loadDict(String filename) {
BufferedReader br = null;
try {
// read content from the dictionary
br = new BufferedReader(new FileReader(filename));
br.readLine();
String line;
while ((line = br.readLine()) != null) {
String[] str = line.trim().split("\\t");
if (str.length == 2) {
String word = str[1];
HashSet<Boolean> isLetter = new HashSet<>();
for (int i = 0; i < word.length(); i++) {
if (!Character.isLetter(word.charAt(i))) {
isLetter.add(false);
}
}
// if the word has correct format
if (!isLetter.contains(false) && word.length() == 5) {
wordlist.add(word);
}
}
}
} catch (IOException e) {
e.printStackTrace();
}
return wordlist;
}
/**
* randomly generate a word as the answer
*
* @param word list
* @return the randomly selected word
*/
public String generateWord(ArrayList<String> wordlist) {
int length = wordlist.size();
// generater random integer within limit
Random rand = new Random();
int randomnum = rand.nextInt(length);
String randomword = wordlist.get(randomnum);
return randomword;
}
public WordGuesser getW() {
return w;
}
public void setW(WordGuesser w) {
this.w = w;
}
public ArrayList<String> getWordlist() {
return wordlist;
}
public void setWordlist(ArrayList<String> wordlist) {
this.wordlist = wordlist;
}
}