Skip to content

Commit a385ecf

Browse files
committed
some small changes
1 parent 0011016 commit a385ecf

File tree

2 files changed

+128
-50
lines changed

2 files changed

+128
-50
lines changed

src/main/java/vip/floatationdevice/wordlehelper/Common.java

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,22 @@ public class Common
1111
{
1212
public final static String PROGRAM_NAME = "WordleHelper 2.2.0";
1313

14-
//regex: 5 letters
14+
/** regex: 5 letters */
1515
public final static Pattern validWord = Pattern.compile("^[a-zA-Z]{5}$");
16-
//file: answer words file 'common.txt' in the jar
16+
17+
/** file: answer words file 'common.txt' in the jar */
1718
public final static String answerWordsFile = "/common.txt";
18-
//file: all words file 'all.txt' in the jar
19+
20+
/** file: all words file 'all.txt' in the jar */
1921
public final static String allWordsFile = "/all.txt";
20-
//possible answer words (from common.txt)
22+
23+
/** possible answer words (from common.txt) */
2124
public static LinkedList<String> answerWordsList = new LinkedList<>();
22-
//all accepted words (from all.txt)
25+
26+
/** all accepted words (from all.txt) */
2327
public static LinkedList<String> allWordsList = new LinkedList<String>();
2428

25-
//read words from 'common.txt' and store them in answerWordsList
29+
/** read words from 'common.txt' and store them in answerWordsList */
2630
public static void readAnswerWords() throws Exception
2731
{
2832
InputStream is = Common.class.getResourceAsStream(answerWordsFile);
@@ -38,7 +42,7 @@ public static void readAnswerWords() throws Exception
3842
System.out.println("Answer words dictionary size: " + answerWordsList.size());
3943
}
4044

41-
//read words from 'all.txt' and store them in allWordsList
45+
/** read words from 'all.txt' and store them in allWordsList */
4246
public static void readAllWords() throws Exception
4347
{
4448
InputStream is = Common.class.getResourceAsStream(allWordsFile);
@@ -54,7 +58,7 @@ public static void readAllWords() throws Exception
5458
System.out.println("All words dictionary size: " + allWordsList.size());
5559
}
5660

57-
//calculate the possible words
61+
/** calculate the possible words */
5862
public static void calculatePossibleWords(String inputWord, int[] result)
5963
{
6064
String inputWordLower = inputWord.toLowerCase();

src/main/java/vip/floatationdevice/wordlehelper/GUI.java

Lines changed: 116 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import java.awt.event.ActionListener;
1010
import java.awt.event.KeyEvent;
1111
import java.util.Arrays;
12+
import java.util.Random;
1213

1314
import static vip.floatationdevice.wordlehelper.Common.*;
1415

@@ -32,74 +33,82 @@ public class GUI extends JFrame
3233
* +-------------------------------+
3334
*/
3435

35-
//help message
36+
/** help message */
3637
private final static String helpText =
3738
"Each line accepts first 5 letters and then 5 numbers from 0 to 2.\n" +
3839
"After the 5th number is typed the possible words will be calculated.\n" +
3940
"Clear a line by pressing the backspace key.\n\n" +
4041
"· 0 means the letter is not in the word,\n" +
4142
"· 1 means the letter is at the right position,\n" +
4243
"· 2 means the letter is in the wrong position.";
43-
//default text for possible words field
44+
45+
/** default text for possible words field */
4446
private final static String initText =
4547
"Possible words will be shown here\n" +
4648
"Enter 5 letters and then 5 numbers to update them\n" +
4749
"Press the '?' button to see help message\n" +
4850
"Press the 'R' button to reset the program\n\n";
49-
//acceptable chars: 0-2, a-z, backspace
51+
52+
/** acceptable chars: 0-2, a-z, backspace */
5053
private final static char[] acceptableChars = {
5154
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
5255
'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't',
5356
'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '\b'
5457
};
55-
//startup status
58+
59+
/** startup status */
5660
static boolean startupComplete = false;
57-
//possible words field
61+
62+
/** possible words field */
5863
private final JTextArea possibleWordsField = new JTextArea(initText);
59-
//all letter blocks for the result board, 6 lines, 5 letters each line
64+
65+
/** all letter blocks for the result board, 6 lines, 5 letters each line */
6066
private final LetterBlock[][] board = new LetterBlock[6][5];
61-
//tries status
67+
68+
/** tries status: 'Try X / 6' */
6269
private final JLabel triesLabel = new JLabel("Try 0 / 6");
63-
//'words left' status
70+
71+
/** 'words left' status */
6472
private final JLabel wordsLeftLabel = new JLabel();
65-
//[?] button that shows the help message
73+
74+
/** [?] button that shows the help message */
6675
private final JButton helpButton = new JButton("?");
67-
//[R] button that resets the board, the tries and the possible words
76+
77+
/** [R] button that resets the board, the tries and the possible words */
6878
private final JButton resetButton = new JButton("R");
69-
//result board
70-
JPanel resultBoard = new JPanel(null);
71-
//current location of the letter input (1-25)
79+
80+
JPanel mainPanel = new JPanel(null);
81+
82+
// current location of the letter input
7283
private int letterIndexLine = 0;
7384
private int letterIndexColumn = 0;
74-
//current location of the number input (1-25)
85+
// current location of the number input
7586
private int numberIndexLine = 0;
7687
private int numberIndexColumn = 0;
77-
//tries counter
88+
89+
/** tries counter */
7890
private int tries = 0;
7991

80-
//constructor
8192
public GUI()
8293
{
8394
StartupWindow startupWindow = new StartupWindow();
8495
if(!startupComplete)
8596
{
8697
//show a startup window
8798
startupWindow.setVisible(true);
88-
/*try {Thread.sleep(1000); //magic, don't touch
89-
}catch (InterruptedException e){e.printStackTrace();System.exit(-1);}*/
9099
}
91100
//load answer dictionary and all words dictionary
92101
try
93102
{
94103
readAnswerWords();
95104
readAllWords();
96-
wordsLeftLabel.setText(answerWordsList.size() + " words left");
105+
wordsLeftLabel.setText(answerWordsList.size() + " answer words loaded");
97106
//show all words at first
98107
possibleWordsField.append(answerWordsList.toString());
99108
}
100109
catch(Exception e)
101110
{
102-
startupWindow.dispose();
111+
startupWindow.dispose(); // close the startup window
103112
System.out.println("Error loading dictionary:");
104113
e.printStackTrace();
105114
//show error window
@@ -119,16 +128,16 @@ public GUI()
119128
setLocationRelativeTo(null);
120129
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
121130
setLayout(null);
122-
resultBoard.setBounds(0, 0, 640, 480);
123-
resultBoard.setBackground(Color.DARK_GRAY);
124-
add(resultBoard);
131+
mainPanel.setBounds(0, 0, 640, 480);
132+
mainPanel.setBackground(Color.DARK_GRAY);
133+
add(mainPanel);
125134
//set result board
126135
initBoard();
127136
//set tries status
128-
triesLabel.setBounds(10, 330, 100, 20);
137+
triesLabel.setBounds(10, 320, 250, 20);
129138
triesLabel.setForeground(Color.WHITE);
130139
//set words left status
131-
wordsLeftLabel.setBounds(110, 330, 100, 20);
140+
wordsLeftLabel.setBounds(10, 350, 250, 20);
132141
wordsLeftLabel.setForeground(Color.WHITE);
133142
//set possible words field
134143
possibleWordsField.setBounds(270, 10, 360, 420);
@@ -139,7 +148,9 @@ public GUI()
139148
possibleWordsField.setForeground(Color.WHITE);
140149
//set help and reset button
141150
helpButton.setBounds(10, 400, 50, 30);
151+
helpButton.setToolTipText("Show help message");
142152
resetButton.setBounds(70, 400, 50, 30);
153+
resetButton.setToolTipText("Reset the program");
143154
//set global key event listener
144155
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(keyEventDispatcher);
145156
//set help button's action listener
@@ -148,13 +159,15 @@ public GUI()
148159
@Override
149160
public void actionPerformed(ActionEvent e)
150161
{
151-
//debug
162+
KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(keyEventDispatcher);
152163
System.out.println("help button pressed");
153164
//show help dialog
154165
JOptionPane.showMessageDialog(GUI.this,
155166
helpText,
156167
"Help",
157-
JOptionPane.INFORMATION_MESSAGE);
168+
JOptionPane.INFORMATION_MESSAGE
169+
);
170+
KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(keyEventDispatcher);
158171
}
159172
});
160173
//set reset button's action listener
@@ -167,15 +180,15 @@ public void actionPerformed(ActionEvent e)
167180
}
168181
});
169182
//add components
170-
resultBoard.add(triesLabel);
171-
resultBoard.add(wordsLeftLabel);
172-
resultBoard.add(possibleWordsField);
173-
resultBoard.add(helpButton);
174-
resultBoard.add(resetButton);
183+
mainPanel.add(triesLabel);
184+
mainPanel.add(wordsLeftLabel);
185+
mainPanel.add(possibleWordsField);
186+
mainPanel.add(helpButton);
187+
mainPanel.add(resetButton);
175188
for(int i = 0; i < 6; i++)
176189
for(int j = 0; j < 5; j++)
177190
{
178-
resultBoard.add(board[i][j]);
191+
mainPanel.add(board[i][j]);
179192
board[i][j].setBounds(j * 50 + 10, i * 50 + 10, 50, 50);
180193
}
181194
//show magic
@@ -195,7 +208,7 @@ public static void main(String[] args)
195208
System.out.println("Startup time: " + (System.currentTimeMillis() - Launcher.startTime) + "ms");
196209
}
197210

198-
//set every JLabel in the board with '_'
211+
/** initialize every JLabel in the input board */
199212
private void initBoard()
200213
{
201214
for(int i = 0; i < 6; i++)
@@ -205,7 +218,7 @@ private void initBoard()
205218
}
206219
}
207220

208-
//get the word of the current line
221+
/** get the word of the current line */
209222
private String getWord(int line)
210223
{
211224
StringBuilder sb = new StringBuilder();
@@ -218,7 +231,7 @@ private String getWord(int line)
218231
return sb.toString().toLowerCase();
219232
}
220233

221-
//get the numbers of the current line
234+
/** get the numbers of the current line */
222235
private int[] getResultNumbers(int line)
223236
{
224237
int[] result = new int[5];
@@ -229,7 +242,7 @@ private int[] getResultNumbers(int line)
229242
return result;
230243
}
231244

232-
//reset a line
245+
/** reset a line */
233246
private void resetLine(int line)
234247
{
235248
for(int i = 0; i != 5; i++)
@@ -241,15 +254,40 @@ private void resetLine(int line)
241254
letterIndexColumn = 0;
242255
numberIndexColumn = 0;
243256
}
244-
} //global key event dispatcher
257+
}
245258

259+
/** global key event dispatcher */
246260
KeyEventDispatcher keyEventDispatcher = new KeyEventDispatcher()
247261
{
248262
@Override
249263
public boolean dispatchKeyEvent(KeyEvent e)
250264
{
251265
if(e.getID() == KeyEvent.KEY_PRESSED)
266+
{
267+
if(e.getKeyCode() == KeyEvent.VK_F3) // turn on debug mode
268+
{
269+
mainPanel.setBorder(BorderFactory.createLineBorder(Color.red));
270+
possibleWordsField.setBorder(BorderFactory.createLineBorder(Color.red));
271+
triesLabel.setBorder(BorderFactory.createLineBorder(Color.red));
272+
wordsLeftLabel.setBorder(BorderFactory.createLineBorder(Color.red));
273+
for(LetterBlock[] t : board)
274+
for(LetterBlock tt : t)
275+
tt.setBorder(BorderFactory.createLineBorder(Color.red));
276+
helpButton.setBorder(BorderFactory.createLineBorder(Color.red));
277+
resetButton.setBorder(BorderFactory.createLineBorder(Color.red));
278+
System.out.println(answerWordsList);
279+
JOptionPane.showMessageDialog(GUI.this,
280+
"Created by MCUmbrella (https://github.com/MCUmbrella)\n" +
281+
"This software is licensed under the MIT license and provided with absolutely no warranty.\n" +
282+
"You can go to https://github.com/MCUmbrella/WordleHelper to check out the source code,\n" +
283+
"submit code changes or initiate any issues.",
284+
DBG_TITLE[new Random().nextInt(DBG_TITLE.length)],
285+
JOptionPane.INFORMATION_MESSAGE
286+
);
287+
return true;
288+
}
252289
for(char c : acceptableChars)
290+
{
253291
if(e.getKeyChar() == c)
254292
{
255293
switch(e.getKeyChar())
@@ -272,10 +310,12 @@ public boolean dispatchKeyEvent(KeyEvent e)
272310
System.out.println("update possible words: " + getWord(letterIndexLine) + " " + Arrays.toString(getResultNumbers(numberIndexLine)));
273311
calculatePossibleWords(getWord(letterIndexLine), getResultNumbers(numberIndexLine));
274312
possibleWordsField.setText("Possible words:\n" + answerWordsList);
275-
wordsLeftLabel.setText("Words left: " + answerWordsList.size());
313+
wordsLeftLabel.setText(answerWordsList.size() + " answer words left");
276314
//if ArrayList is empty, the game is over
277315
if(answerWordsList.size() == 0)
278316
{
317+
KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(keyEventDispatcher);
318+
triesLabel.setText("Try " + ++tries + " / 6");
279319
System.out.println("no words left");
280320
JOptionPane.showMessageDialog(null,
281321
"No words left!\n\n" +
@@ -289,7 +329,8 @@ public boolean dispatchKeyEvent(KeyEvent e)
289329
//if ArrayList has only one word, that word is the result
290330
else if(answerWordsList.size() == 1)
291331
{
292-
triesLabel.setText("Try " + ++tries + " / " + "6");
332+
KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(keyEventDispatcher);
333+
triesLabel.setText("Try " + ++tries + " / 6");
293334
System.out.println("only one word left: " + answerWordsList.get(0));
294335
JOptionPane.showMessageDialog(null,
295336
"The word we are finding is:\n\n · " + answerWordsList.get(0) + "\n\nThe program will reset",
@@ -304,12 +345,13 @@ else if(answerWordsList.size() == 1)
304345
letterIndexColumn = 0;
305346
numberIndexLine++;
306347
letterIndexLine++;
307-
triesLabel.setText("Try " + ++tries + " / " + "6");
348+
triesLabel.setText("Try " + ++tries + " / 6");
308349
//tries++;
309350
}
310351
//if the last line is reached, the game is over
311352
if(numberIndexLine == 6)
312353
{
354+
KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(keyEventDispatcher);
313355
System.out.println("last line reached");
314356
JOptionPane.showMessageDialog(null,
315357
"Last line reached!\nThe program will reset",
@@ -360,11 +402,13 @@ else if(answerWordsList.size() == 1)
360402
}
361403
}
362404
}
405+
}
406+
}
363407
return false;
364408
}
365409
};
366410

367-
//this is what the 'R' button does
411+
/** this is what the 'R' button does */
368412
void resetGUI()
369413
{
370414
System.out.println("resetting");
@@ -374,4 +418,34 @@ void resetGUI()
374418
dispose();
375419
new GUI();
376420
}
421+
422+
private static final String[] DBG_TITLE = {
423+
PROGRAM_NAME,
424+
"[object Object]",
425+
"Hello world!",
426+
"8412wg5d",
427+
"Also try Minecraft!",
428+
"ok",
429+
"YEEEEEEEEEEEHAW!",
430+
"undefined",
431+
"1145141919810",
432+
"Kid named debug window:",
433+
"LIVE DEBUG WINDOW REACTION",
434+
"Absolutely, 100% Lambda-free!",
435+
"bruh",
436+
"waltuh, put your f3 away waltuh",
437+
"",
438+
"@Wish-+U-&Have-#A-@Nice-~Day!",
439+
"*vine boom sound effect*",
440+
"DBG_TITLE[new Random().nextInt(DBG_TITLE.length)]",
441+
"null",
442+
"owo whats this?",
443+
"F**K NYT!",
444+
"Also try Guilded4J!",
445+
"Never gonna give you up",
446+
"The funny",
447+
"???? ?? ???? ?? ??? ???",
448+
"wo ak le",
449+
"A wild debug window appears!"
450+
};
377451
}

0 commit comments

Comments
 (0)