Skip to content

Commit 87fc264

Browse files
committed
complete GUI; massive changes, cant explain
1 parent 3a56731 commit 87fc264

File tree

8 files changed

+353
-107
lines changed

8 files changed

+353
-107
lines changed

README.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22
A program that helps you beat the Wordle game
33
## Usage:
44
```shell
5-
java -jar WordleHelper.jar [maxTries]
5+
java -jar WordleHelper.jar [-c [maxTries]]
66
```
7+
Or just double-click the program icon to launch the program in GUI mode.
8+
### Options:
9+
* `-c`: Launch the program in command line mode (GUI mode by default).
10+
* `maxTries`: The maximum number of tries to find the word (only in CLI mode).<br>
11+
712
**CAUTION:** The external resource is needed - a TXT dictionary file.
813
This can be found at [MCUmbrella/AWordle/dictionary](https://github.com/MCUmbrella/AWordle/tree/main/dictionary).
914
You may need to put it under your workdir and rename it to `words.txt`.

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66

77
<groupId>vip.floatationdevice</groupId>
88
<artifactId>wordlehelper</artifactId>
9-
<version>1.1</version>
9+
<version>2.0</version>
1010

1111
<properties>
1212
<maven.compiler.source>8</maven.compiler.source>
@@ -33,7 +33,7 @@
3333
</descriptorRefs>
3434
<archive>
3535
<manifest>
36-
<mainClass>vip.floatationdevice.wordlehelper.CLI</mainClass>
36+
<mainClass>vip.floatationdevice.wordlehelper.Launcher</mainClass>
3737
</manifest>
3838
</archive>
3939
</configuration>

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

Lines changed: 10 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package vip.floatationdevice.wordlehelper;
22

3-
import java.util.ArrayList;
43
import java.util.NoSuchElementException;
54
import java.util.Scanner;
65

@@ -13,7 +12,6 @@ public class CLI
1312

1413
public static void main(String[] args)
1514
{
16-
System.out.println("WordleHelper Version 1.0");
1715
try
1816
{
1917
Common.readDictionary();
@@ -24,10 +22,10 @@ public static void main(String[] args)
2422
e.printStackTrace();
2523
System.exit(-1);
2624
}
27-
if (args.length != 0)
25+
if (args.length > 1)
2826
try
2927
{
30-
maxTries = Integer.parseInt(args[0]);
28+
maxTries = Integer.parseInt(args[1]);
3129
if(maxTries < 1) throw new NumberFormatException();
3230
}
3331
catch (Exception e)
@@ -50,55 +48,26 @@ public static void main(String[] args)
5048
{
5149
System.out.print("Enter check result (try " + (i + 1) + "/" + maxTries + "): ");
5250
String input = s.nextLine();
53-
if(checkResult.matcher(input).find() && words.contains(input.substring(0, 5))) //if the input is valid
51+
if(checkResult.matcher(input).find() && possibleWordsList.contains(input.substring(0, 5))) //if the input is valid
5452
{
5553
String inputWord = input.substring(0, 5);
5654
int[] result = new int[5];
5755
for(int j = 0; j != 5; j++) result[j] = Integer.parseInt(input.substring(j + 6, j + 7));
58-
for (int loc = 0; loc != 5; loc++)
56+
calculatePossibleWords(inputWord, result);
57+
if(possibleWordsList.size() == 0)
5958
{
60-
switch (result[loc])
61-
{
62-
case 2://the char is in another location
63-
{
64-
//keep the words that have the char
65-
ArrayList<String> temp = new ArrayList<String>();
66-
for (String word : words) if (word.contains(inputWord.charAt(loc)+"")) temp.add(word);
67-
words = temp;
68-
break;
69-
}
70-
case 1://the char is in the right location
71-
{
72-
//keep the words that have the same char at the same location
73-
ArrayList<String> temp = new ArrayList<String>();
74-
for (String word : words) if (word.charAt(loc) == inputWord.charAt(loc)) temp.add(word);
75-
words = temp;
76-
break;
77-
}
78-
case 0://the char is not in the answer word
79-
{
80-
//remove the words that have the same char that is not matched
81-
ArrayList<String> temp = new ArrayList<String>();
82-
for (String word : words) if (!word.contains(inputWord.charAt(loc)+"")) temp.add(word);
83-
words = temp;
84-
break;
85-
}
86-
}
87-
}
88-
if(words.size() == 0)
89-
{
90-
System.out.println("No words left! Is there something wrong with the program or your input?");
59+
System.out.println("No words left!\nIs there a:\n · problem with your input?\n · word that is not in the dictionary?\n · bug in the program?");
9160
System.exit(0);
9261
}
93-
else if (words.size() == 1)
62+
else if (possibleWordsList.size() == 1)
9463
{
95-
System.out.println("The word is: " + words.get(0));
64+
System.out.println("The word is: " + possibleWordsList.get(0));
9665
System.exit(0);
9766
}
9867
else
9968
{
100-
System.out.println("Possible words: " + words);
101-
System.out.println("Words left: " + words.size());
69+
System.out.println("Possible words: " + possibleWordsList);
70+
System.out.println("Words left: " + possibleWordsList.size());
10271
}
10372
i++;
10473
}
Lines changed: 47 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package vip.floatationdevice.wordlehelper;
22

3+
import java.awt.*;
34
import java.io.BufferedReader;
45
import java.io.File;
56
import java.io.FileReader;
@@ -8,24 +9,67 @@
89

910
public class Common
1011
{
12+
public final static String PROGRAM_NAME = "WordleHelper 2.0";
13+
1114
//regex: 5 letters
1215
public final static Pattern validWord = Pattern.compile("^[a-zA-Z]{5}$");
1316
//regex: 5 letters, a space, 5 numbers
1417
public final static Pattern checkResult = Pattern.compile("^[a-zA-Z]{5} [0-2]{5}$");
1518
//file: ./words.txt
1619
public final static File file = new File("words.txt");
1720
//words remaining to be found
18-
public static ArrayList<String> words = new ArrayList<String>();
21+
public static ArrayList<String> possibleWordsList = new ArrayList<String>();
1922
//how many times you want the program to try to find the word. Can be overridden by command line argument
2023
public static int maxTries = 6;
24+
//wordle letter block background colors
25+
public final static Color
26+
COLOR_UNSET = new Color(0x121213),
27+
COLOR_OFF_TARGETED = new Color(0x3a3a3c),
28+
COLOR_DISPLACED = new Color(0xb59f3b),
29+
COLOR_HIT = new Color(0x538d4e);
2130

2231
//read words from './words.txt' and store them in ArrayList<String> words
2332
public static void readDictionary() throws Exception
2433
{
2534
BufferedReader reader = new BufferedReader(new FileReader(file));
2635
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());
36+
if (validWord.matcher(line).find()) possibleWordsList.add(line.toLowerCase());
2837
reader.close();
29-
System.out.println("Dictionary size: " + words.size());
38+
System.out.println("Dictionary size: " + possibleWordsList.size());
39+
}
40+
41+
//calculate the possible words
42+
public static void calculatePossibleWords(String inputWord, int[] result)
43+
{
44+
for (int loc = 0; loc != 5; loc++)
45+
{
46+
switch (result[loc])
47+
{
48+
case 2://the char is in another location
49+
{
50+
//keep the words that have the char
51+
ArrayList<String> temp = new ArrayList<String>();
52+
for (String word : possibleWordsList) if (word.contains(inputWord.charAt(loc)+"")) temp.add(word);
53+
possibleWordsList = temp;
54+
break;
55+
}
56+
case 1://the char is in the right location
57+
{
58+
//keep the words that have the same char at the same location
59+
ArrayList<String> temp = new ArrayList<String>();
60+
for (String word : possibleWordsList) if (word.charAt(loc) == inputWord.charAt(loc)) temp.add(word);
61+
possibleWordsList = temp;
62+
break;
63+
}
64+
case 0://the char is not in the answer word
65+
{
66+
//remove the words that have the same char that is not matched
67+
ArrayList<String> temp = new ArrayList<String>();
68+
for (String word : possibleWordsList) if (!word.contains(inputWord.charAt(loc)+"")) temp.add(word);
69+
possibleWordsList = temp;
70+
break;
71+
}
72+
}
73+
}
3074
}
3175
}

0 commit comments

Comments
 (0)