import java.util.*; import java.io.*; import javax.swing.*; import java.awt.BorderLayout; public class NumberGame { //! -> . // 2 -> abc // 3 -> def // 4 -> ghi // 5 -> jkl // 6 -> mno // 7 -> pqrs // 8 -> tuv // 9 -> wxyz static final char[][] intRelations = new char[][]{ {}, {}, "abc".toCharArray(), "def".toCharArray(), "ghi".toCharArray(), "jkl".toCharArray(), "mno".toCharArray(), "pqrs".toCharArray(), "tuv".toCharArray(), "wxyz".toCharArray(), }; static final int[] charRelations = new int[256]; private static Wrapper dictionary = new Wrapper(0); private static int maxWordLength = 0; public static void main(String[] args) throws IOException { JFrame frame = new JFrame(); frame.setSize(800,600); frame.setLayout(new BorderLayout()); JTextArea textArea = new JTextArea(); frame.add(textArea, BorderLayout.CENTER); long t1 = System.currentTimeMillis(); System.out.println("Starting... "); initCharRelations(); readDictionary(); long t2 = System.currentTimeMillis(); System.out.printf("%10dms: done reading the dictionary.\n", t2-t1); frame.setVisible(true); String oldResult = "!!XXX!!"; int caretPosition; while (true) { t2 = System.currentTimeMillis(); try { Thread.sleep(50); } catch (InterruptedException ie) { } Scanner scanner = new Scanner(new File("numbers.dat")); String data = scanner.nextLine(); scanner.close(); scanner = new Scanner(new File("result.txt")); String result = scanner.nextLine(); scanner.close(); if (oldResult.equals(result)) { continue; } oldResult = result; caretPosition = textArea.getCaretPosition(); textArea.setText(""); result = result.replaceAll(" ", ""); char[] resultArray = result.toCharArray(); char[][] lines = new char[4][resultArray.length]; int printLines = 0; for (int i=0;i= 1; j--) { String subSubString = subString.substring(0, Math.min(length, j)); List matches = dictionary.getMatches(subSubString); for (String match: matches) { output.println(indention+match); textArea.append(indention+match+'\n'); } } } output.close(); textArea.setCaretPosition(caretPosition); long t3 = System.currentTimeMillis(); System.out.printf("%10dms: done evaluating.\n", t3-t2); } } private static String buildIndention(int length) { StringBuilder sb = new StringBuilder(); for (int i=0;i maxWordLength) { maxWordLength = data.length(); } } scanner.close(); } static int getInt(char theChar) { return charRelations[theChar]; } static class Wrapper { int length; Wrapper[] longer = new Wrapper[10]; List matches = new ArrayList(); public Wrapper(int length) { this.length = length; } public void add(String word) { if (word.length() == length) { matches.add(word); } else { int key = getInt(word.charAt(length)); if (longer[key] == null) { longer[key] = new Wrapper(length+1); } longer[key].add(word); } } public List getMatches(String word) { /*List match = getMatches(word, 0); if (match.size() != 0) { debug = true; match = getMatches(word, 0); System.out.println(word); System.out.println(match); System.exit(0); }*/ return getMatches(word, 0); } static boolean debug = false; public List getMatches(String word, int index) { //if (debug) System.out.println(length+": "+word+" - "+index); if (index == word.length()) { //if (debug) System.out.println(length+": found"); return matches; } else { int number = word.charAt(index)-'0'; //if (debug) System.out.println(length+": not found - calling "+number); if (longer[number] == null) { return Collections.emptyList(); } return longer[number].getMatches(word, index+1); } } } }