-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordManager.java
More file actions
57 lines (52 loc) · 1.89 KB
/
Copy pathWordManager.java
File metadata and controls
57 lines (52 loc) · 1.89 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
package org.cis1200.hangman;
import java.util.ArrayList;
import java.io.*;
import java.util.Random;
public class WordManager {
public static ArrayList<String> wordsList;
private BufferedReader br;
private String line;
/*
* Populates ArrayList with words from the given word bank (in this case
* dictionary)
*/
public WordManager(String filePath) {
try {
if (filePath == null || filePath.equals("")) {
throw new IllegalArgumentException("FILEPATH DOES NOT EXIST.");
}
// attempt to read file and populate word list
wordsList = new ArrayList();
// dictionary.txt is a 200 word bank of SAT vocabulary words
br = new BufferedReader(new FileReader(filePath));
while ((line = br.readLine()) != null) {
wordsList.add(line); // add words from txt file using reader
}
} catch (IOException e) {
// if dictionary unreadable or some error occurs, throw exception
System.out.println("THERE IS AN ERROR.");
}
}
/*
* Retrieves a random word from the arraylist of words.
*/
public String getRandomWord() {
Random r = new Random();
return wordsList.get(r.nextInt(wordsList.size())).toUpperCase();
// all words pulled from the dictionary set to uppercase
}
/*
* Hides word that is passed in behind number of dashes equivalent to
* number of letters in word
*/
public static String hideWord(String word) {
if (word == null || word.equals("")) {
throw new IllegalArgumentException("WORD INVALID.");
}
String hWord = "";
for (int i = 0; i < word.length(); i++) {
hWord += "?"; // hides word behind question marks
}
return hWord;
}
}