-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPhraseBank.java
More file actions
191 lines (166 loc) · 5.98 KB
/
PhraseBank.java
File metadata and controls
191 lines (166 loc) · 5.98 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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
import java.util.Scanner;
/**
* Store a list of phrases. Generally for use with a Hangman program.
*
*/
public class PhraseBank {
private static final String DEFAULT_FILE_NAME = "HangmanMovies.txt";
// simple phrase bank if we can't find the right file
private static final String[] EMERGENCY_PHRASES = {"THE_POLICE",
"THE_ROLLING_STONES", "THE_WHO", "OF_MONSTERS_AND_MEN",
"REM", "NATHANIEL_RATELIFF_AND_THE_NIGHT_SWEATS",
"STONE_TEMP_PILOTS", "INDIGO_GIRLS", "POSTMODERN_JUKEBOX"};
private static final String EMERGENCY_TOPIC = "Band";
private static final int RANDOM_SEED = 6; // STUDENT VERSION IS 6!!!!
private static int alternateSeed = -1;
private static boolean useDefaultSeed = true;
private ArrayList<String> phrases;
private int currentIndex;
private String topic;
/**
* Create a PhraseBank from the default file.
*/
public PhraseBank(){
this(DEFAULT_FILE_NAME);
}
/**
* Create a PhraseBank from the given file name. If the file cannot be
* found then use the default file. If that cannot be found use
* the emergency phrase bank of bands. Uses default seed or alternate seed.
* @param fileName The name of the file that has the phrase bank.
*/
public PhraseBank(String fileName) {
createPhraseList(fileName);
if (useDefaultSeed) {
Collections.shuffle(phrases, new Random(RANDOM_SEED)); // use this line for non random behavior
} else {
Collections.shuffle(phrases, new Random(alternateSeed));
}
}
/**
* Create a PhraseBank from the given file name. If the file cannot be
* found then use the default file. If that cannot be found use
* the emergency phrase bank of bands. The randomize parameter determines
* if we use the default source of randomness or not.
* @param fileName The name of the file that has the phrase bank.
* @param randomize Whether to randomize the phrases
* or use the default seeds.
*/
public PhraseBank(String fileName, boolean randomize) {
createPhraseList(fileName);
if (!randomize) {
if (useDefaultSeed) {
// predictable behavior
Collections.shuffle(phrases, new Random(RANDOM_SEED));
} else {
Collections.shuffle(phrases, new Random(alternateSeed));
}
} else {
// make it less predictable
Collections.shuffle(phrases);
}
}
/**
* Set the alternate seed for Randomness.
* @param newSeed
*/
public static void setSeed(int newSeed) {
alternateSeed = newSeed;
}
/**
* Set whether we should use the default seed for randomness
* or the alternate seed.
* @param useDefault
*/
public static void setUseDefaultSeed(boolean useDefault) {
useDefaultSeed = useDefault;
}
/**
* Get what the alternate seed for Randomness is.
* @return the current alternate seed
*/
public static int getAlteranteSeed() {
return alternateSeed;
}
// get the list of phrases ready from the fiven file.
private void createPhraseList(String fileName) {
phrases = new ArrayList<>();
currentIndex = -1;
loadWords(fileName);
}
/**
* Call this method to get the next phrase.
* The returned String will contain upper case
* letters and underscores for spaces.
* @return the next phrase
*/
public String getNextPhrase(){
currentIndex = (currentIndex + 1) % phrases.size();
return phrases.get(currentIndex);
}
/**
* Return the topic of this phrase bank.
* @return The topic of this phrase bank.
*/
public String getTopic(){
return topic;
}
// Read the topic and phrases from the file.
// If there is any problem then use the Emergency topic and phrases.
private void loadWords(String fileName) {
try {
Scanner s = new Scanner(new File(fileName));
topic = s.nextLine();
while (s.hasNextLine()) {
String phrase = trim(s.nextLine().trim());
phrases.add(phrase.toUpperCase());
}
s.close();
} catch (IOException e) {
System.out.println("\n***** ERROR IN READING FILE ***** ");
System.out.println("Can't find this file "
+ fileName + " in the current directory.");
System.out.println("Error: " + e);
String currentDir = System.getProperty("user.dir");
System.out.println("Be sure " + fileName + " is in this directory: ");
System.out.println(currentDir);
// problem with reading file, use the emergency topic and phrases
System.out.println();
System.out.println("Program will use the back-up topic of Bands Mike likes");
constructFromEmergencyData();
}
// if no values construct from emergency data
if (phrases.size() == 0) {
constructFromEmergencyData();
}
}
// no files found, use our emergency list of phrases
private void constructFromEmergencyData() {
topic = EMERGENCY_TOPIC;
for (String phrase : EMERGENCY_PHRASES) {
phrases.add(phrase);
}
}
// I assume nextLine is not null.
// Return a String with only characters and
// underscores for spaces.
// No other characters in org are included
// in the result.
private static String trim(String org) {
String result = "";
for (int i = 0; i < org.length(); i++) {
char ch = org.charAt(i);
if ( Character.isLetter(ch)) {
result += ch;
} else if(ch == ' ') {
result += '_';
}
}
return result;
}
}