Skip to content

Commit

Permalink
small refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
MCUmbrella committed Jan 3, 2023
1 parent 6dd2e9b commit 43b2956
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 34 deletions.
48 changes: 44 additions & 4 deletions src/main/java/vip/floatationdevice/wordlehelper/WordleHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@ public class WordleHelper
private final static String allWordsFile = "/all.txt";

// list of possible answer words (from answer.txt)
static LinkedList<String> answerWordsList = new LinkedList<>();
private static LinkedList<String> answerWordsList = new LinkedList<>();

// list of all acceptable words (from all.txt)
static LinkedList<String> allWordsList = new LinkedList<>();
private final static LinkedList<String> allWordsList = new LinkedList<>();

// read words from 'answer.txt' and store them in answerWordsList
public static void readAnswerWords() throws Exception
private static void readAnswerWords() throws Exception
{
InputStream is = WordleHelper.class.getResourceAsStream(answerWordsFile);
if(is == null) throw new Exception("Could not read file '" + answerWordsFile + "'");
Expand All @@ -44,7 +44,7 @@ public static void readAnswerWords() throws Exception
}

// read words from 'all.txt' and store them in allWordsList
public static void readAllWords() throws Exception
private static void readAllWords() throws Exception
{
InputStream is = WordleHelper.class.getResourceAsStream(allWordsFile);
if(is == null) throw new Exception("Could not read file '" + allWordsFile + "'");
Expand All @@ -58,8 +58,48 @@ public static void readAllWords() throws Exception
System.out.println("All words dictionary size: " + allWordsList.size());
}

/**
* get the String array containing the remaining answer words
*/
public static String[] getRemainingWords()
{
String[] remaining = new String[answerWordsList.size()];
int i = 0;
for(String s : answerWordsList)
{
remaining[i] = s;
i++;
}
return remaining;
}

/**
* initialize or reset WordleHelper
* @return the initial answer words array
* @throws Exception when fail to read dictionary or something else happen
*/
public static String[] init() throws Exception
{
if(allWordsList.size() == 0) readAllWords();
answerWordsList.clear();
readAnswerWords();
return getRemainingWords();
}

/**
* check if the provided string is in the acceptable words' dictionary.
* @param s the string to be checked
* @return true if the string is a valid word, false otherwise
*/
public static boolean isValidWord(String s)
{
return allWordsList.contains(s);
}

/**
* calculate the possible words
* @return the String array that contains the remaining words
* @throws IllegalStateException when the remaining answer words is less than 2
*/
public static String[] calculatePossibleWords(String inputWord, int[] result)
{
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package vip.floatationdevice.wordlehelper;

import java.util.Arrays;
import java.util.NoSuchElementException;
import java.util.Scanner;
import java.util.regex.Pattern;

import static vip.floatationdevice.wordlehelper.WordleHelper.answerWordsList;
import static vip.floatationdevice.wordlehelper.WordleHelper.calculatePossibleWords;

public class WordleHelperCLI
{
//scanner for input
Expand All @@ -25,8 +23,7 @@ public static void main(String[] args)
}
try
{
WordleHelper.readAnswerWords();
WordleHelper.readAllWords();
WordleHelper.init();
}
catch(Exception e)
{
Expand Down Expand Up @@ -61,26 +58,26 @@ public static void main(String[] args)
{
System.out.print("Enter check result (try " + (i + 1) + "/" + maxTries + "): ");
String input = s.nextLine().toLowerCase();
if(checkResult.matcher(input).find() && answerWordsList.contains(input.substring(0, 5))) //if the input is valid
if(checkResult.matcher(input).find() && WordleHelper.isValidWord(input.substring(0, 5))) //if the input is valid
{
String inputWord = input.substring(0, 5);
int[] result = new int[5];
for(int j = 0; j != 5; j++) result[j] = Integer.parseInt(input.substring(j + 6, j + 7));
calculatePossibleWords(inputWord, result);
if(answerWordsList.size() == 0)
String[] remaining = WordleHelper.calculatePossibleWords(inputWord, result);
if(remaining.length == 0)
{
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?");
System.exit(0);
}
else if(answerWordsList.size() == 1)
else if(remaining.length == 1)
{
System.out.println("The word is: " + answerWordsList.get(0));
System.out.println("The word is: " + remaining[0]);
System.exit(0);
}
else
{
System.out.println("Possible words: " + answerWordsList);
System.out.println("Words left: " + answerWordsList.size());
System.out.println("Possible words: " + Arrays.toString(remaining));
System.out.println("Words left: " + remaining.length);
}
i++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,10 @@ public WordleHelperGUI()
//load answer dictionary and all words dictionary
try
{
readAnswerWords();
readAllWords();
wordsLeftLabel.setText("Answer words left: " + answerWordsList.size());
String[] remaining = init();
wordsLeftLabel.setText("Answer words left: " + remaining.length);
//show all words at first
possibleWordsField.append(answerWordsList.toString());
possibleWordsField.append(Arrays.toString(remaining));
}
catch(Exception e)
{
Expand Down Expand Up @@ -257,7 +256,7 @@ public boolean dispatchKeyEvent(KeyEvent e)
tt.setBorder(BorderFactory.createLineBorder(Color.red));
helpButton.setBorder(BorderFactory.createLineBorder(Color.red));
resetButton.setBorder(BorderFactory.createLineBorder(Color.red));
System.out.println(answerWordsList);
System.out.println(Arrays.toString(getRemainingWords()));
JOptionPane.showMessageDialog(WordleHelperGUI.this,
"Created by MCUmbrella (https://github.com/MCUmbrella)\n" +
"This software is licensed under the MIT license and provided with absolutely no warranty.\n" +
Expand Down Expand Up @@ -294,11 +293,11 @@ DBG_TITLE[new Random().nextInt(DBG_TITLE.length)],
{
//this line has reached the end, update possible words
System.out.println("update possible words: " + getWord(letterIndexLine) + " " + Arrays.toString(getResultNumbers(numberIndexLine)));
calculatePossibleWords(getWord(letterIndexLine), getResultNumbers(numberIndexLine));
possibleWordsField.setText("Possible words:\n" + answerWordsList);
wordsLeftLabel.setText("Answer words left: " + answerWordsList.size());
//if ArrayList is empty, the game is over
if(answerWordsList.size() == 0)
String[] remaining = calculatePossibleWords(getWord(letterIndexLine), getResultNumbers(numberIndexLine));
possibleWordsField.setText("Possible words:\n" + Arrays.toString(remaining));
wordsLeftLabel.setText("Answer words left: " + remaining.length);
//if remaining words list is empty, the game is over
if(remaining.length == 0)
{
KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(keyEventDispatcher);
triesLabel.setText("Try " + ++tries + " / 6");
Expand All @@ -314,16 +313,16 @@ DBG_TITLE[new Random().nextInt(DBG_TITLE.length)],
);
resetGUI();
}
//if ArrayList has only one word, that word is the result
else if(answerWordsList.size() == 1)
//if remaining words has only one word left, that word is the result
else if(remaining.length == 1)
{
KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(keyEventDispatcher);
triesLabel.setText("Try " + ++tries + " / 6");
wordsLeftLabel.setText("Answer word: " + answerWordsList.get(0));
wordsLeftLabel.setText("Answer word: " + remaining[0]);
wordsLeftLabel.setForeground(Color.GREEN);
System.out.println("only one word left: " + answerWordsList.get(0));
System.out.println("only one word left: " + remaining[0]);
JOptionPane.showMessageDialog(null,
"The word we are finding is:\n\n · " + answerWordsList.get(0) + "\n\nThe program will reset",
"The word we are finding is:\n\n · " + remaining[0] + "\n\nThe program will reset",
"Congratulations!",
JOptionPane.INFORMATION_MESSAGE
);
Expand Down Expand Up @@ -375,7 +374,7 @@ else if(answerWordsList.size() == 1)
letterIndexColumn++;
if(letterIndexColumn == 5)//check if the word is in the dictionary
{
if(allWordsList.contains(getWord(letterIndexLine)))
if(isValidWord(getWord(letterIndexLine)))
{
//move to the first letter and start setting the numbers
letterIndexColumn = 0;
Expand Down Expand Up @@ -407,8 +406,6 @@ void resetGUI()
{
System.out.println("resetting");
KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(keyEventDispatcher);
answerWordsList.clear();
allWordsList.clear();
windowX = getX();
windowY = getY();
dispose();
Expand Down

0 comments on commit 43b2956

Please sign in to comment.