|
| 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