-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordSet.java
More file actions
160 lines (140 loc) · 3.9 KB
/
WordSet.java
File metadata and controls
160 lines (140 loc) · 3.9 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
import java.util.*;
import java.io.*;
public class WordSet {
private static ArrayList<HashSet<String>> allWords;
static {
File f;
Scanner scanner;
allWords = new ArrayList<HashSet<String>>();
try {
f = new File("./words_alpha.txt");
scanner = new Scanner(f);
for (int i = 0; i < 100; i++) {
allWords.add(new HashSet<String>());
}
while (scanner.hasNextLine()) {
String word = scanner.nextLine();
allWords.get(word.length()).add(word);
}
for (int i = 0; i < 100; i++) {
System.out.println("words of length " + i + ": " + allWords.get(i).size());
}
} catch (Exception e) {
e.printStackTrace();
System.out.println("There was an error.");
}
}
public HashSet<String> words;
public int length;
public ArrayList<String> successfulGuesses = new ArrayList<String>();
public ArrayList<String> unsuccessfulGuesses = new ArrayList<String>();
public ArrayList<String> possibleGuesses = new ArrayList<String>();
public ArrayList<String> guess = new ArrayList<String>();
public WordSet(int length) {
this.length = length;
this.words = new HashSet<String>();
Iterator<String> it = allWords.get(length).iterator();
while (it.hasNext()) {
words.add(it.next());
}
String alp = "abcdefghijklmnopqrstuvwxyz";
for (int i = 0; i < alp.length(); i++) {
possibleGuesses.add(i, alp.substring(i, i+1));
}
for (int i = 0; i < length; i++) {
guess.add("_");
}
}
public int[] filter(String s) {
// Filter the set by the character "s"
// HashSet.remove
// Returns the ID taken by the filter
Iterator<String> it = words.iterator();
ArrayList<Integer> ID = new ArrayList<Integer>();
for (int i = 0; i < Math.pow(2,this.length); i++) {
ID.add(0);
}
while (it.hasNext()) {
int a = id(it.next(), s);
ID.set(a, ID.get(a) + 1);
}
int highestID = 0;
for (int i = 0; i < ID.size(); i++) {
if (ID.get(i) > ID.get(highestID)) {
highestID = i;
}
}
System.out.println(words.size());
keepID(highestID, s);
System.out.println(words.size());
int[] ret = new int[this.length];
for (int i = 0; i < length; i++) {
ret[length - 1 - i] = (highestID % 2);
highestID /= 2;
}
return ret;
}
public void keepID(int id, String target) {
ArrayList<String> toRemove = new ArrayList<String>();
//Iterator<String> it = words.iterator();
for (String s : words) {
if (id != id(s, target)) {
toRemove.add(s);
}
}
for (String s : toRemove) {
this.words.remove(s);
}
}
public static int id(String given, String target) {
// returns the id of the string "s" if the target is "target"
int ret = 0;
for (int i = 0; i < given.length(); i++) {
ret *= 2;
if (given.substring(i, i + 1).equals(target)) {
ret++;
}
}
return ret;
}
public void submitGuess(String letter) {
int[] aID = filter(letter);
boolean foundLetter = false;
//can combine for loops if wanted
for (int i = 0 ; i < aID.length ; i++) {
if (aID[i] == 1) {
guess.set(i, letter);
foundLetter = true;
}
}
if (foundLetter) {
successfulGuesses.add(letter);
} else {
unsuccessfulGuesses.add(letter);
}
int ind = 0;
boolean b = false;
for (int i = 0; i < possibleGuesses.size(); i++)
{
if (possibleGuesses.get(i).equals(letter)) {
ind = i;
b = true;
}
}
if (b) {
possibleGuesses.remove(ind);
}
}
public boolean isComplete() {
int count = 0;
for (int i = 0 ; i < guess.size() ; i++) {
if (guess.get(i) != "_") {
count++;
}
}
if (count == length) {
return true;
}
return false;
}
}