-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathListCreator.java
136 lines (108 loc) · 3.96 KB
/
ListCreator.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
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
import java.io.*;
import java.util.*;
public class ListCreator {
private ArrayList <String> eligibleList;
private ArrayList <Integer> yellowIndex;
private ArrayList <Integer> greenIndex;
private ArrayList <Integer> colorless;
private String prevWord;
public ListCreator() throws Exception{
eligibleList = new ArrayList <String>();
FileInputStream fstream = new FileInputStream("sortedFiveLetter.txt");
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String currentWord;
while((currentWord = br.readLine()) != null){
eligibleList.add(currentWord);
}
}
public ArrayList <String> getList(){
return eligibleList;
}
public void updateList(ArrayList <Integer> yellowIndex, ArrayList <Integer> greenIndex, ArrayList <Integer> colorless , String prevWord){
this.yellowIndex = yellowIndex;
this.greenIndex = greenIndex;
this.colorless = colorless;
this.prevWord = prevWord;
// checking out colorless letter
removeAllColorlessLetters();
// checking out yellow letters
configureYellowLetters();
//checkout the green letters
keepAllGreenLetters();
}
private void removeAllColorlessLetters(){
for(int currentIndex: colorless){ // get each colorless letters
String currentLetter = Character.toString(prevWord.charAt(currentIndex - 1)); //current colorless letter
ommitWordsThatHaveCurrentLetter(currentLetter);
}
}
private void configureYellowLetters(){
for(int currentIndex: yellowIndex){
String currentLetter = Character.toString(prevWord.charAt(currentIndex - 1));
ommitWordsThatNotContainsCurrentLetter(currentLetter, currentIndex);
}
}
private void keepAllGreenLetters(){
for(int currentIndex: greenIndex){
String currentLetter = Character.toString(prevWord.charAt(currentIndex - 1));
ommitWordsThatMisplacedGreenLetters(currentLetter, currentIndex);
}
}
private void ommitWordsThatHaveCurrentLetter(String currentLetter){
Iterator <String> itr = eligibleList.iterator();
while(itr.hasNext()){
String words = itr.next(); // get each words of eligible list
if(words.contains(currentLetter)){ // the word has the colorless letter
// check if the letter is on yellow or greenlist
boolean onYellowOrGreen = isTheLetterOnYellowOrGreen(currentLetter);
if(!onYellowOrGreen){ // if its not on yellow or green then remove it
itr.remove();
}
//else keep the word
}
}
}
private boolean isTheLetterOnYellowOrGreen(String currentLetter){
for(int index: yellowIndex){
String yellowLetter = Character.toString(prevWord.charAt(index - 1));
if(yellowLetter.equals(currentLetter)){
return true;
}
}
for(int index: greenIndex){
String greenLetter = Character.toString(prevWord.charAt(index - 1));
if(greenLetter.equals(currentLetter)){
return true;
}
}
return false;
}
private void ommitWordsThatNotContainsCurrentLetter (String currentLetter, int currentIndex){
Iterator<String> itr = eligibleList.iterator();
while(itr.hasNext()){
String words = itr.next(); // get each word of the eligible list
int indexOfCurrentLetter = words.indexOf(currentLetter); // check if the letter is in this word
if( indexOfCurrentLetter == -1 ){ // if the word doesn't contain yellow letter
//remove the word from eligible list
itr.remove();
}
else{ // if the word contains yellow letter
// check if the letter is in the same possition or in the green position
if(((indexOfCurrentLetter+1) == currentIndex) || (greenIndex.contains(indexOfCurrentLetter+1)) ){
// if its in the same position or green position then remove it
itr.remove();
}
}
}
}
private void ommitWordsThatMisplacedGreenLetters(String currentLetter, int currentIndex){
Iterator<String> itr = eligibleList.iterator();
while(itr.hasNext()){
String words = itr.next();
if(words.charAt(currentIndex-1) != currentLetter.charAt(0)){ // the letter is not in the correct position
// remove from the list
itr.remove();
}
}
}
}