Skip to content

Commit 5a65b9c

Browse files
committed
ok
1 parent 5740d37 commit 5a65b9c

File tree

4 files changed

+198
-0
lines changed

4 files changed

+198
-0
lines changed

.gitignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
words.txt
2+
# idea
3+
.idea/
4+
*.iml
5+
target/
6+
17
# Compiled class file
28
*.class
39

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,10 @@
11
# WordleHelper
22
A program that helps you beat the Wordle game
3+
## Usage:
4+
```shell
5+
java -jar WordleHelper.jar [maxTries]
6+
```
7+
**CAUTION:** The external resource is needed - a TXT dictionary file.
8+
This can be found at [MCUmbrella/AWordle/dictionary](https://github.com/MCUmbrella/AWordle/tree/main/dictionary).
9+
You may need to put it under your workdir and rename it to `words.txt`.
10+
It is recommended to use 'common.txt' as the dictionary.

pom.xml

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>vip.floatationdevice</groupId>
8+
<artifactId>wordlehelper</artifactId>
9+
<version>1.0</version>
10+
11+
<properties>
12+
<maven.compiler.source>8</maven.compiler.source>
13+
<maven.compiler.target>8</maven.compiler.target>
14+
</properties>
15+
16+
<build>
17+
<plugins>
18+
<plugin>
19+
<artifactId>maven-assembly-plugin</artifactId>
20+
<version>3.1.1</version>
21+
<executions>
22+
<execution>
23+
<id>make-assembly</id>
24+
<phase>package</phase>
25+
<goals>
26+
<goal>single</goal>
27+
</goals>
28+
</execution>
29+
</executions>
30+
<configuration>
31+
<descriptorRefs>
32+
<descriptorRef>jar-with-dependencies</descriptorRef>
33+
</descriptorRefs>
34+
<archive>
35+
<manifest>
36+
<mainClass>vip.floatationdevice.wordlehelper.Main</mainClass>
37+
</manifest>
38+
</archive>
39+
</configuration>
40+
</plugin>
41+
</plugins>
42+
</build>
43+
44+
</project>
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
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.NoSuchElementException;
8+
import java.util.Scanner;
9+
import java.util.regex.Pattern;
10+
11+
public class Main
12+
{
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;
23+
//scanner for input
24+
final static Scanner s = new Scanner(System.in);
25+
26+
public static void main(String[] args)
27+
{
28+
System.out.println("WordleHelper Version 1.0");
29+
try
30+
{
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();
35+
}
36+
catch (Exception e)
37+
{
38+
System.out.println("Failed to read 'words.txt'. Have you put it under your workdir?\nYou can get the file from 'https://github.com/MCUmbrella/AWordle/tree/main/dictionary'");
39+
e.printStackTrace();
40+
System.exit(-1);
41+
}
42+
if (args.length != 0)
43+
try
44+
{
45+
maxTries = Integer.parseInt(args[0]);
46+
if(maxTries < 1) throw new NumberFormatException();
47+
}
48+
catch (Exception e)
49+
{
50+
System.out.println("Argument must be a positive integer!");
51+
e.printStackTrace();
52+
System.exit(-1);
53+
}
54+
System.out.println("Dictionary size: " + words.size());
55+
System.out.println(
56+
"Enter a Wordle check result with 5 letters, a space and 5 numbers from 0 to 2.\n" +
57+
"Example: apple 01002\n" +
58+
"Explaination:\n" +
59+
" 0 means the letter is not in the word,\n" +
60+
" 1 means the letter is at the right position,\n" +
61+
" 2 means the letter is in the wrong position."
62+
);
63+
try
64+
{
65+
for(int i = 0; i != maxTries;)
66+
{
67+
System.out.print("Enter check result (try " + (i + 1) + "/" + maxTries + "): ");
68+
String input = s.nextLine();
69+
if(checkResult.matcher(input).find() && words.contains(input.substring(0, 5))) //if the input is valid
70+
{
71+
String inputWord = input.substring(0, 5);
72+
int[] result = new int[5];
73+
for(int j = 0; j != 5; j++) result[j] = Integer.parseInt(input.substring(j + 6, j + 7));
74+
for (int loc = 0; loc != 5; loc++)
75+
{
76+
switch (result[loc])
77+
{
78+
case 2://the char is in another location
79+
{
80+
//keep the words that have the char
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+
case 1://the char is in the right location
87+
{
88+
//keep the words that have the same char at the same location
89+
ArrayList<String> temp = new ArrayList<String>();
90+
for (String word : words) if (word.charAt(loc) == inputWord.charAt(loc)) temp.add(word);
91+
words = temp;
92+
break;
93+
}
94+
case 0://the char is not in the answer word
95+
{
96+
//remove the words that have the same char that is not matched
97+
ArrayList<String> temp = new ArrayList<String>();
98+
for (String word : words) if (!word.contains(inputWord.charAt(loc)+"")) temp.add(word);
99+
words = temp;
100+
break;
101+
}
102+
}
103+
}
104+
if(words.size() == 0)
105+
{
106+
System.out.println("No words left! Is there something wrong with the program or your input?");
107+
System.exit(0);
108+
}
109+
else if (words.size() == 1)
110+
{
111+
System.out.println("The word is: " + words.get(0));
112+
System.exit(0);
113+
}
114+
else
115+
{
116+
System.out.println("Possible words: " + words);
117+
System.out.println("Words left: " + words.size());
118+
}
119+
i++;
120+
}
121+
else
122+
{
123+
System.out.print("Invalid input. Try again? [y/N]\n? ");
124+
if(s.nextLine().equalsIgnoreCase("y")) continue;
125+
else
126+
{
127+
System.out.println("Exiting");
128+
System.exit(0);
129+
}
130+
}
131+
}
132+
}
133+
catch (NoSuchElementException e)//someone press ctrl+d !?/!1?!?1?!?1!?!?
134+
{
135+
System.out.println("\nExiting");
136+
System.exit(0);
137+
}
138+
System.out.println("Max tries exceeded. The program will exit.\n");
139+
}
140+
}

0 commit comments

Comments
 (0)