From 43b29569cfefae693c523e9a75de26871abc4a65 Mon Sep 17 00:00:00 2001 From: MCUmbrella Date: Tue, 3 Jan 2023 16:54:58 +0800 Subject: [PATCH] small refactor --- .../wordlehelper/WordleHelper.java | 48 +++++++++++++++++-- .../wordlehelper/WordleHelperCLI.java | 21 ++++---- .../wordlehelper/WordleHelperGUI.java | 33 ++++++------- 3 files changed, 68 insertions(+), 34 deletions(-) diff --git a/src/main/java/vip/floatationdevice/wordlehelper/WordleHelper.java b/src/main/java/vip/floatationdevice/wordlehelper/WordleHelper.java index 5c8f5fd..ca5c990 100644 --- a/src/main/java/vip/floatationdevice/wordlehelper/WordleHelper.java +++ b/src/main/java/vip/floatationdevice/wordlehelper/WordleHelper.java @@ -23,13 +23,13 @@ public class WordleHelper private final static String allWordsFile = "/all.txt"; // list of possible answer words (from answer.txt) - static LinkedList answerWordsList = new LinkedList<>(); + private static LinkedList answerWordsList = new LinkedList<>(); // list of all acceptable words (from all.txt) - static LinkedList allWordsList = new LinkedList<>(); + private final static LinkedList 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 + "'"); @@ -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 + "'"); @@ -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) { diff --git a/src/main/java/vip/floatationdevice/wordlehelper/WordleHelperCLI.java b/src/main/java/vip/floatationdevice/wordlehelper/WordleHelperCLI.java index 9b8dc0c..5e630dd 100644 --- a/src/main/java/vip/floatationdevice/wordlehelper/WordleHelperCLI.java +++ b/src/main/java/vip/floatationdevice/wordlehelper/WordleHelperCLI.java @@ -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 @@ -25,8 +23,7 @@ public static void main(String[] args) } try { - WordleHelper.readAnswerWords(); - WordleHelper.readAllWords(); + WordleHelper.init(); } catch(Exception e) { @@ -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++; } diff --git a/src/main/java/vip/floatationdevice/wordlehelper/WordleHelperGUI.java b/src/main/java/vip/floatationdevice/wordlehelper/WordleHelperGUI.java index 46964c4..7a4988a 100644 --- a/src/main/java/vip/floatationdevice/wordlehelper/WordleHelperGUI.java +++ b/src/main/java/vip/floatationdevice/wordlehelper/WordleHelperGUI.java @@ -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) { @@ -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" + @@ -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"); @@ -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 ); @@ -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; @@ -407,8 +406,6 @@ void resetGUI() { System.out.println("resetting"); KeyboardFocusManager.getCurrentKeyboardFocusManager().removeKeyEventDispatcher(keyEventDispatcher); - answerWordsList.clear(); - allWordsList.clear(); windowX = getX(); windowY = getY(); dispose();