public static void main(String[] args) { WordLadder test = new WordLadder(); String start = "hit"; String end = "cog"; Set<String> dict = new HashSet<>(); dict.addAll(Arrays.asList("hot", "dot", "dog", "lot", "log")); System.out.println(dict); System.out.println(test.ladderLength(start, end, dict)); }
@Override public void actionPerformed(ActionEvent actionEvent) { Object obj = actionEvent.getSource(); if (obj == btFindPat) { testpanel.removeAll(); validate(); repaint(); if (taDictionary.getText().isEmpty()) { JOptionPane.showMessageDialog( frame, "Please load words so they appear in Text Area before Finding Path. "); } else if ((tfSourc.getText().isEmpty()) || (tfSourc_6.getText().isEmpty())) { JOptionPane.showMessageDialog( frame, "Please enter Source and Destination words before Finding Path. "); } else if (testpanel.getComponents().length != 0) { JOptionPane.showMessageDialog(frame, "Please clear results before continuing."); } else { wordLadder.findPath( tfSourc.getText(), tfSourc_6.getText(), Integer.parseInt(tfWordSize.getText())); lblFindPat.setText( "Time to find Path: " + String.valueOf(wordLadder.getTimeForPath()) + " milliseconds"); lblCos.setText("Cost of Path: " + String.valueOf(wordLadder.g.getCost())); results = wordLadder.getResults(); Collections.reverse(results); int x = 10; int y = 20; for (String s : results) { x += 20; y += 20; JLabel _lbl = new JLabel(s); _lbl.setLocation(x, y); _lbl.setSize(100, 26); if (results.indexOf(s) == 0) { _lbl.setForeground(new Color(-14646771)); } else if (results.indexOf(s) == (results.size() - 1)) { _lbl.setForeground(new Color(-8254711)); } else { _lbl.setForeground(new Color(-16777216)); } testpanel.add(_lbl); testpanel.repaint(); } if (wordLadder.g.getCost() == 0) { // if (wordLadder.g.getGraphError() != null){ JLabel _lbl = new JLabel(wordLadder.g.getGraphError()); _lbl.setLocation(x, y); _lbl.setSize(100, 26); testpanel.add(_lbl); testpanel.repaint(); // } } } /*if (taDictionary.getText().isEmpty()){ wordLadder = new WordLadder(tfFilePat.getText(), tfSourc.getText(), tfSourc_6.getText()); }else if (!taDictionary.getText().isEmpty()){ ArrayList<String> taList = new ArrayList<String>(); StringTokenizer stringTokenizer = new StringTokenizer(taDump, "\t\n\r\f,\""); while (stringTokenizer.hasMoreTokens()) { String token = stringTokenizer.nextToken(); taList.add(token); } */ // } } if (obj == btLoadFil) { clearData(); // wordLadder.setWords_size(Integer.parseInt(tfWordSize.getText())); // String size = tfWordSize.getText(); // int intSize = Integer.parseInt(size); // WordLadderGUI.showMessage("Loading words of" + tfWordSize.getText() + " characters from // file: " + tfFilePat.getText(), Color.GREEN, Color.GREEN); lblIndexing1.setText("Indexing..."); System.out.println( "Loading words of " + tfWordSize.getText() + " characters from file: " + tfFilePat.getText()); wordLadder = new WordLadder(tfFilePat.getText(), Integer.parseInt(tfWordSize.getText())); // wordLadder. guiDictionary = new WordCollection(wordLadder.getWordList()); WordLadderGUI.showMessage( "Displaying " + wordLadder.getWordList().size() + " words from file with length of " + tfWordSize.getText(), Color.GREEN, Color.GREEN); System.out.println( "Displaying " + wordLadder.getWordList().size() + " words from file with length of " + tfWordSize.getText()); taDictionary.setText(guiDictionary.toString()); lblIndexing1.setText("Indexing... done."); lblDictCoun.setText("Words in Dictionary = " + wordLadder.getWordList().size() + " words"); wordLadder.buildGraph(); System.out.println("Graph Built"); lblProgres.setText("Time to Build Graph: " + wordLadder.getTimeForGraph() + " milliseconds"); } if (obj == btLoadTextFiel) { wordLadder.setWords_size(Integer.parseInt(tfWordSize.getText())); String taDump = taDictionary.getText(); System.out.println("Loading Words from Text Area"); lblIndexing1.setText("Indexing..."); ArrayList<String> taList = new ArrayList<String>(); StringTokenizer stringTokenizer = new StringTokenizer(taDump, "\t\n\r\f,\""); while (stringTokenizer.hasMoreTokens()) { String token = stringTokenizer.nextToken(); taList.add(token); } guiDictionary = new WordCollection(); guiDictionary.setWords(taList); wordLadder = new WordLadder(taList); WordLadderGUI.showMessage( "Loading " + wordLadder.getWordList().size() + " words from Text Field", Color.GREEN, Color.GREEN); lblIndexing1.setText("Indexing... done."); lblDictCoun.setText("Words in Dictionary = " + wordLadder.getWordList().size() + " words"); wordLadder.buildGraph(); System.out.println("Graph Built"); lblProgres.setText("Time to Build Graph: " + wordLadder.getTimeForGraph() + " milliseconds"); } if (obj == btClear) { testpanel.removeAll(); testpanel.validate(); testpanel.repaint(); wordLadder = null; lblCos.setText("Cost of Path: 0.0"); lblCos.repaint(); lblDictCoun.setText("Words in Dictionary = 0 words"); lblFindPat.setText("Time to find Path: 0 milliseconds"); tfSourc.setText(""); tfSourc_6.setText(""); taDictionary.setText(""); results = null; lblProgres.setText("Time to Build Graph: 0 milliseconds"); } }