-
Notifications
You must be signed in to change notification settings - Fork 0
/
wordle.java
239 lines (188 loc) · 6.17 KB
/
wordle.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
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// Wordle by Mia S.
// Date: 30.05.2022
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.*;
import javax.swing.border.Border;
public class Wordle extends JFrame {
WordlePanel wordleGUI = new WordlePanel();
// Create JFrame with textfield and button
JFrame frame;
WordlePanel[] panelArray = new WordlePanel[6];
JTextField textField = new JTextField();
JButton enter = new JButton("ENTER");
int counter = 0;
String wordle = pickWord();
// Custom Clue colors
Color clueColorGreen = new Color(109, 188, 101);
Color clueColorYellow = new Color(251,219,82);
Color clueColorGrey = new Color(89, 89, 89);
// Constructor that defines the GUI
Wordle() throws IOException {
frame = new JFrame ("Wordle");
frame.setSize(270, 400);
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.setLayout(new GridLayout(8, 1));
frame.setVisible(true);
frame.setLocationRelativeTo(null);
// Add WordlePanel to JFrame
for (int i = 0; i < 6; i++) {
panelArray[i] = new WordlePanel();
frame.add(panelArray[i]);
}
frame.add(textField);
textField.setHorizontalAlignment(JTextField.CENTER);
textField.setBorder(BorderFactory.createLineBorder(Color.LIGHT_GRAY));
textField.requestFocus();
frame.add(enter);
frame.revalidate();
enter.addActionListener(new UponEnter());
}
public static void main(String[] args) throws IOException {
new Wordle();
} // End of main method
class WordlePanel extends JPanel {
JLabel[] guesses = new JLabel[5];
// Constructor that sets the grid layout of guesses
public WordlePanel() {
this.setLayout(new GridLayout(1, 5));
Border border = BorderFactory.createLineBorder(Color.LIGHT_GRAY);
for (int i = 0; i < 5; i++) {
guesses[i] = new JLabel();
guesses[i].setHorizontalAlignment(SwingConstants.CENTER);
guesses[i].setOpaque(true);
guesses[i].setBorder(border);
this.add(guesses[i]);
}
}
// Method that fills the guesses array with input from user
public void fillGuesses(String s, int i, Color c) {
this.guesses[i].setText(s);
this.guesses[i].setBackground(c);
this.guesses[i].setForeground(Color.WHITE);
this.guesses[i].setFont(new Font("Calibri", Font.BOLD, 16));
}
} // End of WordlePanel class
// UponEnter class that is triggered when the "ENTER" button is clicked and gives the user feedback
class UponEnter implements ActionListener {
public void actionPerformed(ActionEvent event) {
String guess = textField.getText();
if ("ENTER".equals(event.getActionCommand())) {
if(onlyLetters(guess) == false) {
JOptionPane.showMessageDialog(frame, "You can only type in English letters!");
textField.setText(null);
}
else if (fiveChars(guess) == false) {
JOptionPane.showMessageDialog(frame, "Your guess must be an English word of five letters!");
}
else {
evaluateGuess(guess.toUpperCase());
textField.setText(null);
counter++;
}
if (guess.toUpperCase().equals(wordle.toUpperCase())) {
JOptionPane.showMessageDialog(frame, "Correct! You won!");
textField.setEnabled(false);
return;
}
if (counter > 5) {
JOptionPane.showMessageDialog(frame, "Game over! You ran out of guesses! \n The correct word was: " + wordle);
textField.setEnabled(false);
return;
}
textField.requestFocus();
}
else {
return;
}
}
} // End of UponEnter class
// Evaluates the guess from the user, compares it to the wordle-word and uses the fillGuesses method to give color clues to the user
public void evaluateGuess(String guess) {
String[] wordleArray = wordle.split("");
String[] guessArray = guess.split("");
boolean[] used = new boolean[5];
boolean[] notGrey = new boolean[5];
// Green clues: If the letter matches wordle-word letter in the CORRECT position
for (int i = 0; i < 5; i++) {
currentRow().fillGuesses(guessArray[i], i, null);
if (wordleArray[i].equals(guessArray[i])) {
currentRow().fillGuesses(guessArray[i], i, clueColorGreen);
used[i] = true;
notGrey[i] = true;
}
}
// Yellow clues: If the letter matches wordle-word letter in the WRONG position
for (int i = 0; i < 5; i++) {
if (notGrey[i] == true) {
continue;
}
for (int j = 0; j< 5; j++) {
if (used[j] == false && guessArray[i].equals(wordleArray[j])) {
currentRow().fillGuesses(guessArray[i], i, clueColorYellow);
used[j] = true;
notGrey[i] = true;
break;
}
}
}
// Grey clues: If letter is NOT not-grey, then paint it grey
for (int i = 0; i < 5; i++) {
if (notGrey[i] == false) {
currentRow().fillGuesses(guessArray[i], i, clueColorGrey);
}
}
return;
} // End of evaluateGuess method
// Gets the current panel row based on the counter/guess round
public WordlePanel currentRow() {
return this.panelArray[counter];
}
// Checks if input string only consists of letters
public static boolean onlyLetters(String s) {
s = s.toUpperCase();
for (int i = 0; i < s.length(); i++) {
char c = s.charAt(i);
if (!(c >= 'A' && c <= 'Z')) {
return false;
}
}
return true;
}
// Ensures that guess consists of five characters
public boolean fiveChars(String s) {
s = this.textField.getText();
if (s.length() != 5) {
return false;
}
else {
return true;
}
}
// Chooses Wordle-word randomly from text file
public static String pickWord() throws IOException {
BufferedReader reader = new BufferedReader(new FileReader("words.txt"));
String line = reader.readLine();
List<String> words = new ArrayList<String>();
while(line != null) {
String[] wordsLine = line.split(" ");
for (String word : wordsLine) {
words.add(word);
}
line = reader.readLine();
}
Random rand = new Random(System.currentTimeMillis());
String randomWord = words.get(rand.nextInt(words.size()));
reader.close();
return randomWord.toUpperCase();
}
} // End of Wordle JFrame