import java.io.File; import java.io.IOException; import java.util.Scanner; /** * Driver for the Doublets game of HW7 of Boston University CAS CS 112 A1 Spring 2007 * See problem set for the description * @author Leo Reyzin * */ public class Doublets { public static void main(String[] args) { final String fileName = "TWL06.txt"; // Change numWordsInFile if fileName changes final int numWordsInFile = 178691; // Create a new scanner for reading the dictionary text file from the current // directory System.out.println("Current directory: " + System.getProperty("user.dir")); Scanner fileScanner; try { fileScanner = new Scanner (new File (fileName));//("dictionary.txt")); } catch (IOException e) { System.out.println("Unable to open file. "+e.getMessage()); return; } // read the file into the graph WordGraph g = new WordGraph (numWordsInFile); int wordsRead = 0; System.out.println("Words read so far:"); while (fileScanner.hasNext()) { g.addVertex(fileScanner.next()); final int HOW_OFTEN_TO_PRINT = 10000, HOW_OFTEN_NEW_LINE = 100000; // print the number of words processed every once in a while, to show progress if (++wordsRead % HOW_OFTEN_TO_PRINT == 0) { System.out.print(wordsRead+" "); if (wordsRead % HOW_OFTEN_NEW_LINE == 0) System.out.println(); } } // Process user input Scanner inputScanner = new Scanner(System.in); userInputLoop: for(;;) { WordGraph.Vertex begin; do { // loop until user finds legitimate start word System.out.print("\nPlease enter the start word, or \"***\" to quit: "); String s = inputScanner.next(); if (s.equals("***")) break userInputLoop; begin = g.find(s); if (begin == null) System.out.print("Word not in the dictionary. "); } while (begin == null); WordGraph.Vertex end; do { // loop until user finds legitimate end word System.out.print("Please enter the end word: "); end = g.find(inputScanner.next()); if (end == null) System.out.print("Word not in the dictionary. "); } while (end == null); WordGraph.VertexStack s = g.bfs(begin, end); // search for path from begin to end if (s == null) { System.out.println("No path exists"); } else while (!s.isEmpty()) { // print out the path System.out.println(s.pop()); } } } }