Skip to content

Commit 3a56731

Browse files
committed
separate something out
1 parent ee5ec96 commit 3a56731

File tree

4 files changed

+68
-33
lines changed

4 files changed

+68
-33
lines changed

pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
</descriptorRefs>
3434
<archive>
3535
<manifest>
36-
<mainClass>vip.floatationdevice.wordlehelper.Main</mainClass>
36+
<mainClass>vip.floatationdevice.wordlehelper.CLI</mainClass>
3737
</manifest>
3838
</archive>
3939
</configuration>

src/main/java/vip/floatationdevice/wordlehelper/Main.java renamed to src/main/java/vip/floatationdevice/wordlehelper/CLI.java

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,13 @@
11
package vip.floatationdevice.wordlehelper;
22

3-
import java.io.BufferedReader;
4-
import java.io.File;
5-
import java.io.FileReader;
63
import java.util.ArrayList;
74
import java.util.NoSuchElementException;
85
import java.util.Scanner;
9-
import java.util.regex.Pattern;
106

11-
public class Main
7+
import static vip.floatationdevice.wordlehelper.Common.*;
8+
9+
public class CLI
1210
{
13-
//regex: 5 letters
14-
final static Pattern validWord = Pattern.compile("^[a-zA-Z]{5}$");
15-
//regex: 5 letters, a space, 5 numbers
16-
final static Pattern checkResult = Pattern.compile("^[a-zA-Z]{5} [0-2]{5}$");
17-
//file: ./words.txt
18-
final static File file = new File("words.txt");
19-
//words remaining to be found
20-
static ArrayList<String> words = new ArrayList<String>();
21-
//how many times you want the program to try to find the word. Can be overridden by command line argument
22-
static int maxTries = 6;
2311
//scanner for input
2412
final static Scanner s = new Scanner(System.in);
2513

@@ -28,10 +16,7 @@ public static void main(String[] args)
2816
System.out.println("WordleHelper Version 1.0");
2917
try
3018
{
31-
BufferedReader reader = new BufferedReader(new FileReader(file));
32-
for (String line = reader.readLine(); line != null; line = reader.readLine()) //put matched words into the ArrayList
33-
if (validWord.matcher(line).find()) words.add(line.toLowerCase());
34-
reader.close();
19+
Common.readDictionary();
3520
}
3621
catch (Exception e)
3722
{
@@ -51,7 +36,6 @@ public static void main(String[] args)
5136
e.printStackTrace();
5237
System.exit(-1);
5338
}
54-
System.out.println("Dictionary size: " + words.size());
5539
System.out.println(
5640
"Enter a Wordle check result with 5 letters, a space and 5 numbers from 0 to 2.\n" +
5741
"Example: apple 01002\n" +
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package vip.floatationdevice.wordlehelper;
2+
3+
import java.io.BufferedReader;
4+
import java.io.File;
5+
import java.io.FileReader;
6+
import java.util.ArrayList;
7+
import java.util.regex.Pattern;
8+
9+
public class Common
10+
{
11+
//regex: 5 letters
12+
public final static Pattern validWord = Pattern.compile("^[a-zA-Z]{5}$");
13+
//regex: 5 letters, a space, 5 numbers
14+
public final static Pattern checkResult = Pattern.compile("^[a-zA-Z]{5} [0-2]{5}$");
15+
//file: ./words.txt
16+
public final static File file = new File("words.txt");
17+
//words remaining to be found
18+
public static ArrayList<String> words = new ArrayList<String>();
19+
//how many times you want the program to try to find the word. Can be overridden by command line argument
20+
public static int maxTries = 6;
21+
22+
//read words from './words.txt' and store them in ArrayList<String> words
23+
public static void readDictionary() throws Exception
24+
{
25+
BufferedReader reader = new BufferedReader(new FileReader(file));
26+
for (String line = reader.readLine(); line != null; line = reader.readLine()) //put matched words into the ArrayList
27+
if (validWord.matcher(line).find()) words.add(line.toLowerCase());
28+
reader.close();
29+
System.out.println("Dictionary size: " + words.size());
30+
}
31+
}

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

Lines changed: 32 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import java.awt.event.ActionListener;
88
import java.awt.event.KeyEvent;
99

10-
import static vip.floatationdevice.wordlehelper.Main.*;
10+
import static vip.floatationdevice.wordlehelper.Common.*;
1111

1212
public class GUI extends JFrame
1313
{
@@ -37,13 +37,13 @@ public class GUI extends JFrame
3737
//acceptable chars: 0-2, a-z, backspace
3838
char[] acceptableChars = "abcdefghijklmnopqrstuvwxyz012\b".toCharArray();
3939
//possible words field
40-
private final JTextArea possibleWords=new JTextArea("Possible words will be shown here\nEnter your first 5 letters and then 5 numbers to see them\nPress the '?' button on the bottom left for help");
40+
private final JTextArea possibleWords=new JTextArea("Possible words will be shown here\nEnter 5 letters and then 5 numbers to update them\nPress the '?' button to see help message\nPress the 'R' button to reset the program\n\n");
4141
//result board, 6 lines, 5 letters each line
4242
private final JLabel[][] board=new JLabel[6][5];
4343
//tries status
4444
private final JLabel tries=new JLabel("Try {}/6");
4545
//'words left' status
46-
private final JLabel wordsLeft=new JLabel("{} words left");
46+
private final JLabel wordsLeft=new JLabel();
4747
//[?] button that shows the help message
4848
private final JButton help=new JButton("?");
4949
//[R] button that resets the board, the tries and the possible words
@@ -90,19 +90,40 @@ public GUI()
9090
if(!startupComplete)
9191
{
9292
//show a startup window
93+
JLabel startupText = new JLabel("Starting WordleHelper...");
94+
startupText.setHorizontalAlignment(SwingConstants.CENTER);
95+
startup.add(startupText);
9396
startup.setSize(200,50);
9497
startup.setLocationRelativeTo(null);
9598
startup.setUndecorated(true);
96-
startup.setAlwaysOnTop(true);
9799
startup.setResizable(false);
98-
JLabel startupText = new JLabel("Starting WordleHelper...");
99-
startupText.setHorizontalAlignment(SwingConstants.CENTER);
100-
startup.add(startupText);
100+
startup.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
101101
startup.setVisible(true);
102102
/*try {
103103
Thread.sleep(1000); //magic, don't touch
104104
}catch (InterruptedException e){}*/
105105
}
106+
//load dictionary
107+
try
108+
{
109+
readDictionary();
110+
wordsLeft.setText(words.size() + " words left");
111+
//show all words at first
112+
possibleWords.append(words.toString());
113+
}
114+
catch (Exception e)
115+
{
116+
System.out.println("Error loading dictionary:");
117+
e.printStackTrace();
118+
//show error window
119+
JOptionPane.showMessageDialog(null,
120+
"Error loading dictionary: "+e.getMessage()+
121+
"\nHave you put it under your workdir?\nYou can get the file from 'https://github.com/MCUmbrella/AWordle/tree/main/dictionary'",
122+
"Error",
123+
JOptionPane.ERROR_MESSAGE
124+
);
125+
System.exit(-1);
126+
}
106127

107128
//set main window
108129
setTitle("WordleHelper 1.1");
@@ -127,16 +148,14 @@ public GUI()
127148
possibleWords.setBounds(270,10,360,420);
128149
possibleWords.setEditable(false);
129150
possibleWords.setLineWrap(true);
130-
//set color
131-
possibleWords.setBackground(Color.GRAY);
151+
possibleWords.setWrapStyleWord(true);
152+
possibleWords.setBackground(new Color(96,96,96));
132153
possibleWords.setForeground(Color.WHITE);
133154
//set help and reset button
134155
help.setBounds(10,400,50,30);
135156
reset.setBounds(70,400,50,30);
136157
//set global key event listener
137-
//help.addKeyListener(boardKeyListener);
138-
KeyboardFocusManager.getCurrentKeyboardFocusManager()
139-
.addKeyEventDispatcher(keyEventDispatcher);
158+
if (!startupComplete) KeyboardFocusManager.getCurrentKeyboardFocusManager().addKeyEventDispatcher(keyEventDispatcher);
140159
//set help button's action listener
141160
help.addActionListener(new ActionListener()
142161
{
@@ -164,6 +183,7 @@ public void actionPerformed(ActionEvent e)
164183
public void actionPerformed(ActionEvent e)
165184
{
166185
System.out.println("resetting");
186+
words.clear();
167187
dispose();
168188
new GUI();
169189
}

0 commit comments

Comments
 (0)