-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWordle.java
More file actions
142 lines (121 loc) · 5.01 KB
/
Copy pathWordle.java
File metadata and controls
142 lines (121 loc) · 5.01 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
import edu.willamette.cs1.wordle.WordleDictionary;
import edu.willamette.cs1.wordle.WordleGWindow;
import java.awt.Color;
public class Wordle {
private WordleGWindow gw;
private int row;
private String secretWord;
//creates base program like window, enter listener, and picks a target word
public Wordle() {
gw = new WordleGWindow();
gw.addEnterListener((guess) -> enterAction(guess));
row = 1;
//picks random target word
int random = (int) (Math.random() * WordleDictionary.FIVE_LETTER_WORDS.length);
secretWord = WordleDictionary.FIVE_LETTER_WORDS[random];
/* MILESTONE 1
for (int i = 0; i < secretWord.length(); i++) {
String letter = secretWord.substring(i, i+1);
gw.setSquareLetter(0, i, letter);
}
*/
}
//when enter is pressed it checks if it's within the wordlist and then colors it if it is
public void enterAction(String guess) {
guess = guess.toLowerCase();
if (!inWordList(guess)) {
gw.showMessage("Not in word list!");
} else {
gw.showMessage("In word list!");
setColors(guess);
keyboardColor(guess);
nextOrEnd(guess);
}
}
//loops through the dictionary to see if the guess is valid
private boolean inWordList(String guess) {
for (int i = 0; i < WordleDictionary.FIVE_LETTER_WORDS.length; i++) {
if (guess.equals(WordleDictionary.FIVE_LETTER_WORDS[i])) {
return true;
}
}
return false;
}
//sets square colors with gray as the default, green for matches, and yellow for letters in wrong position
private void setColors(String guess) {
for (int i = 0; i < 5; i++) {
gw.setSquareColor(row - 1, i, Color.gray);
}
for (int i = 0; i < 5; i++) {
String guessLetter = guess.substring(i, i + 1);
String targetLetter = secretWord.substring(i, i + 1);
if (guessLetter.equals(targetLetter)) {
gw.setSquareColor(row - 1, i, Color.green);
}
}
String leftoverLetters = unusedLetters(guess);
for (int i = 0; i < 5; i++) {
String guessLetter = guess.substring(i, i + 1);
String targetLetter = secretWord.substring(i, i + 1);
//If the letter doesn't match, it checks if it is in the word at all. If it is, it gets colored yellow and then removed so it doesn't make two squares yellow.
if (!guessLetter.equals(targetLetter)) {
int place = leftoverLetters.indexOf(guessLetter);
if (place != -1) {
gw.setSquareColor(row - 1, i, Color.yellow);
leftoverLetters = leftoverLetters.substring(0, place) + leftoverLetters.substring(place + 1);
}
}
}
}
//makes a string of letters that weren't matched perfectly and uses it to figure out yellow squares
private String unusedLetters(String guess) {
String leftoverLetters = "";
for (int i = 0; i < 5; i++) {
String guessLetter = guess.substring(i, i + 1);
String targetLetter = secretWord.substring(i, i + 1);
if (!guessLetter.equals(targetLetter)) {
leftoverLetters += targetLetter;
}
}
return leftoverLetters;
}
//colors the keyboard keys gray if not in word, green if perfect match, and yellow if in wrong position
private void keyboardColor(String guess) {
for (int i = 0; i < 5; i++) {
String guessLetter = guess.substring(i, i + 1);
boolean letterFound = false;
for (int j = 0; j < 5; j++) {
if (guessLetter.equals(secretWord.substring(j, j + 1))) {
letterFound = true;
}
}
if (!letterFound) {
gw.setKeyColor(guessLetter.toUpperCase(), Color.gray);
}
}
for (int i = 0; i < 5; i++) {
String guessLetter = guess.substring(i, i + 1);
String targetLetter = secretWord.substring(i, i + 1);
if (guessLetter.equals(targetLetter)) {
gw.setKeyColor(guessLetter.toUpperCase(), Color.green);
} else if (gw.getSquareColor(row - 1, i).equals(Color.yellow)) {
if (gw.getKeyColor(guessLetter.toUpperCase()) != Color.green) {
gw.setKeyColor(guessLetter.toUpperCase(), Color.yellow);
}
}
}
}
//shows win message or shows the word or advances to the next row depending on varying conditions
private void nextOrEnd(String guess) {
if (guess.equals(secretWord)) {
gw.showMessage("Good job, you found it!");
} else if (row == 6) {
gw.showMessage("The word was: " + secretWord);
} else {
gw.setCurrentRow(row++);
}
}
public static void main(String[] args) {
new Wordle();
}
}