9
9
import java .awt .event .ActionListener ;
10
10
import java .awt .event .KeyEvent ;
11
11
import java .util .Arrays ;
12
+ import java .util .Random ;
12
13
13
14
import static vip .floatationdevice .wordlehelper .Common .*;
14
15
@@ -32,74 +33,82 @@ public class GUI extends JFrame
32
33
* +-------------------------------+
33
34
*/
34
35
35
- // help message
36
+ /** help message */
36
37
private final static String helpText =
37
38
"Each line accepts first 5 letters and then 5 numbers from 0 to 2.\n " +
38
39
"After the 5th number is typed the possible words will be calculated.\n " +
39
40
"Clear a line by pressing the backspace key.\n \n " +
40
41
"· 0 means the letter is not in the word,\n " +
41
42
"· 1 means the letter is at the right position,\n " +
42
43
"· 2 means the letter is in the wrong position." ;
43
- //default text for possible words field
44
+
45
+ /** default text for possible words field */
44
46
private final static String initText =
45
47
"Possible words will be shown here\n " +
46
48
"Enter 5 letters and then 5 numbers to update them\n " +
47
49
"Press the '?' button to see help message\n " +
48
50
"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 */
50
53
private final static char [] acceptableChars = {
51
54
'a' , 'b' , 'c' , 'd' , 'e' , 'f' , 'g' , 'h' , 'i' , 'j' ,
52
55
'k' , 'l' , 'm' , 'n' , 'o' , 'p' , 'q' , 'r' , 's' , 't' ,
53
56
'u' , 'v' , 'w' , 'x' , 'y' , 'z' , '0' , '1' , '2' , '\b'
54
57
};
55
- //startup status
58
+
59
+ /** startup status */
56
60
static boolean startupComplete = false ;
57
- //possible words field
61
+
62
+ /** possible words field */
58
63
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 */
60
66
private final LetterBlock [][] board = new LetterBlock [6 ][5 ];
61
- //tries status
67
+
68
+ /** tries status: 'Try X / 6' */
62
69
private final JLabel triesLabel = new JLabel ("Try 0 / 6" );
63
- //'words left' status
70
+
71
+ /** 'words left' status */
64
72
private final JLabel wordsLeftLabel = new JLabel ();
65
- //[?] button that shows the help message
73
+
74
+ /** [?] button that shows the help message */
66
75
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 */
68
78
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
72
83
private int letterIndexLine = 0 ;
73
84
private int letterIndexColumn = 0 ;
74
- //current location of the number input (1-25)
85
+ // current location of the number input
75
86
private int numberIndexLine = 0 ;
76
87
private int numberIndexColumn = 0 ;
77
- //tries counter
88
+
89
+ /** tries counter */
78
90
private int tries = 0 ;
79
91
80
- //constructor
81
92
public GUI ()
82
93
{
83
94
StartupWindow startupWindow = new StartupWindow ();
84
95
if (!startupComplete )
85
96
{
86
97
//show a startup window
87
98
startupWindow .setVisible (true );
88
- /*try {Thread.sleep(1000); //magic, don't touch
89
- }catch (InterruptedException e){e.printStackTrace();System.exit(-1);}*/
90
99
}
91
100
//load answer dictionary and all words dictionary
92
101
try
93
102
{
94
103
readAnswerWords ();
95
104
readAllWords ();
96
- wordsLeftLabel .setText (answerWordsList .size () + " words left " );
105
+ wordsLeftLabel .setText (answerWordsList .size () + " answer words loaded " );
97
106
//show all words at first
98
107
possibleWordsField .append (answerWordsList .toString ());
99
108
}
100
109
catch (Exception e )
101
110
{
102
- startupWindow .dispose ();
111
+ startupWindow .dispose (); // close the startup window
103
112
System .out .println ("Error loading dictionary:" );
104
113
e .printStackTrace ();
105
114
//show error window
@@ -119,16 +128,16 @@ public GUI()
119
128
setLocationRelativeTo (null );
120
129
setDefaultCloseOperation (JFrame .EXIT_ON_CLOSE );
121
130
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 );
125
134
//set result board
126
135
initBoard ();
127
136
//set tries status
128
- triesLabel .setBounds (10 , 330 , 100 , 20 );
137
+ triesLabel .setBounds (10 , 320 , 250 , 20 );
129
138
triesLabel .setForeground (Color .WHITE );
130
139
//set words left status
131
- wordsLeftLabel .setBounds (110 , 330 , 100 , 20 );
140
+ wordsLeftLabel .setBounds (10 , 350 , 250 , 20 );
132
141
wordsLeftLabel .setForeground (Color .WHITE );
133
142
//set possible words field
134
143
possibleWordsField .setBounds (270 , 10 , 360 , 420 );
@@ -139,7 +148,9 @@ public GUI()
139
148
possibleWordsField .setForeground (Color .WHITE );
140
149
//set help and reset button
141
150
helpButton .setBounds (10 , 400 , 50 , 30 );
151
+ helpButton .setToolTipText ("Show help message" );
142
152
resetButton .setBounds (70 , 400 , 50 , 30 );
153
+ resetButton .setToolTipText ("Reset the program" );
143
154
//set global key event listener
144
155
KeyboardFocusManager .getCurrentKeyboardFocusManager ().addKeyEventDispatcher (keyEventDispatcher );
145
156
//set help button's action listener
@@ -148,13 +159,15 @@ public GUI()
148
159
@ Override
149
160
public void actionPerformed (ActionEvent e )
150
161
{
151
- //debug
162
+ KeyboardFocusManager . getCurrentKeyboardFocusManager (). removeKeyEventDispatcher ( keyEventDispatcher );
152
163
System .out .println ("help button pressed" );
153
164
//show help dialog
154
165
JOptionPane .showMessageDialog (GUI .this ,
155
166
helpText ,
156
167
"Help" ,
157
- JOptionPane .INFORMATION_MESSAGE );
168
+ JOptionPane .INFORMATION_MESSAGE
169
+ );
170
+ KeyboardFocusManager .getCurrentKeyboardFocusManager ().addKeyEventDispatcher (keyEventDispatcher );
158
171
}
159
172
});
160
173
//set reset button's action listener
@@ -167,15 +180,15 @@ public void actionPerformed(ActionEvent e)
167
180
}
168
181
});
169
182
//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 );
175
188
for (int i = 0 ; i < 6 ; i ++)
176
189
for (int j = 0 ; j < 5 ; j ++)
177
190
{
178
- resultBoard .add (board [i ][j ]);
191
+ mainPanel .add (board [i ][j ]);
179
192
board [i ][j ].setBounds (j * 50 + 10 , i * 50 + 10 , 50 , 50 );
180
193
}
181
194
//show magic
@@ -195,7 +208,7 @@ public static void main(String[] args)
195
208
System .out .println ("Startup time: " + (System .currentTimeMillis () - Launcher .startTime ) + "ms" );
196
209
}
197
210
198
- //set every JLabel in the board with '_'
211
+ /** initialize every JLabel in the input board */
199
212
private void initBoard ()
200
213
{
201
214
for (int i = 0 ; i < 6 ; i ++)
@@ -205,7 +218,7 @@ private void initBoard()
205
218
}
206
219
}
207
220
208
- // get the word of the current line
221
+ /** get the word of the current line */
209
222
private String getWord (int line )
210
223
{
211
224
StringBuilder sb = new StringBuilder ();
@@ -218,7 +231,7 @@ private String getWord(int line)
218
231
return sb .toString ().toLowerCase ();
219
232
}
220
233
221
- // get the numbers of the current line
234
+ /** get the numbers of the current line */
222
235
private int [] getResultNumbers (int line )
223
236
{
224
237
int [] result = new int [5 ];
@@ -229,7 +242,7 @@ private int[] getResultNumbers(int line)
229
242
return result ;
230
243
}
231
244
232
- // reset a line
245
+ /** reset a line */
233
246
private void resetLine (int line )
234
247
{
235
248
for (int i = 0 ; i != 5 ; i ++)
@@ -241,15 +254,40 @@ private void resetLine(int line)
241
254
letterIndexColumn = 0 ;
242
255
numberIndexColumn = 0 ;
243
256
}
244
- } //global key event dispatcher
257
+ }
245
258
259
+ /** global key event dispatcher */
246
260
KeyEventDispatcher keyEventDispatcher = new KeyEventDispatcher ()
247
261
{
248
262
@ Override
249
263
public boolean dispatchKeyEvent (KeyEvent e )
250
264
{
251
265
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
+ }
252
289
for (char c : acceptableChars )
290
+ {
253
291
if (e .getKeyChar () == c )
254
292
{
255
293
switch (e .getKeyChar ())
@@ -272,10 +310,12 @@ public boolean dispatchKeyEvent(KeyEvent e)
272
310
System .out .println ("update possible words: " + getWord (letterIndexLine ) + " " + Arrays .toString (getResultNumbers (numberIndexLine )));
273
311
calculatePossibleWords (getWord (letterIndexLine ), getResultNumbers (numberIndexLine ));
274
312
possibleWordsField .setText ("Possible words:\n " + answerWordsList );
275
- wordsLeftLabel .setText ("Words left: " + answerWordsList .size ());
313
+ wordsLeftLabel .setText (answerWordsList .size () + " answer words left" );
276
314
//if ArrayList is empty, the game is over
277
315
if (answerWordsList .size () == 0 )
278
316
{
317
+ KeyboardFocusManager .getCurrentKeyboardFocusManager ().removeKeyEventDispatcher (keyEventDispatcher );
318
+ triesLabel .setText ("Try " + ++tries + " / 6" );
279
319
System .out .println ("no words left" );
280
320
JOptionPane .showMessageDialog (null ,
281
321
"No words left!\n \n " +
@@ -289,7 +329,8 @@ public boolean dispatchKeyEvent(KeyEvent e)
289
329
//if ArrayList has only one word, that word is the result
290
330
else if (answerWordsList .size () == 1 )
291
331
{
292
- triesLabel .setText ("Try " + ++tries + " / " + "6" );
332
+ KeyboardFocusManager .getCurrentKeyboardFocusManager ().removeKeyEventDispatcher (keyEventDispatcher );
333
+ triesLabel .setText ("Try " + ++tries + " / 6" );
293
334
System .out .println ("only one word left: " + answerWordsList .get (0 ));
294
335
JOptionPane .showMessageDialog (null ,
295
336
"The word we are finding is:\n \n · " + answerWordsList .get (0 ) + "\n \n The program will reset" ,
@@ -304,12 +345,13 @@ else if(answerWordsList.size() == 1)
304
345
letterIndexColumn = 0 ;
305
346
numberIndexLine ++;
306
347
letterIndexLine ++;
307
- triesLabel .setText ("Try " + ++tries + " / " + " 6" );
348
+ triesLabel .setText ("Try " + ++tries + " / 6" );
308
349
//tries++;
309
350
}
310
351
//if the last line is reached, the game is over
311
352
if (numberIndexLine == 6 )
312
353
{
354
+ KeyboardFocusManager .getCurrentKeyboardFocusManager ().removeKeyEventDispatcher (keyEventDispatcher );
313
355
System .out .println ("last line reached" );
314
356
JOptionPane .showMessageDialog (null ,
315
357
"Last line reached!\n The program will reset" ,
@@ -360,11 +402,13 @@ else if(answerWordsList.size() == 1)
360
402
}
361
403
}
362
404
}
405
+ }
406
+ }
363
407
return false ;
364
408
}
365
409
};
366
410
367
- // this is what the 'R' button does
411
+ /** this is what the 'R' button does */
368
412
void resetGUI ()
369
413
{
370
414
System .out .println ("resetting" );
@@ -374,4 +418,34 @@ void resetGUI()
374
418
dispose ();
375
419
new GUI ();
376
420
}
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
+ };
377
451
}
0 commit comments