import java.io.FileWriter; import java.io.IOException; import java.util.Iterator; import java.util.List; import java.util.ListIterator; /** * MP4 is a sliding block puzzle solver. It coordinates solving the puzzle by * instantiating PuzzleLoader and PuzzleSolution and handling the writing of * output to the console and HTML file. * * TODO Learn how to use Java unit tests * * @author Michael Leonhard (mleonhar) * @version 2005-10-30 * @version JDK 1.5.0.5, Eclipse 3.1.0, Windows XP * @version CS 340, Fall 2005, Instructor: Pat Troy, TA: Nitin Jindal */ public class MP4 { // command line usage information private static final String USAGE_TEXT = "Usage: MP4 [-h] fileName\r\n" + " fileName is the name of a file that contains the initial puzzle state\r\n" + " -h write an HTML report to fileName.html\r\n"; /** * Prints the string to stdout (commented out in production version) * * @param string the string to be printed */ public static void dprint(String string) { // print out the message // System.out.println(string); // flush the output buffer so the text appears right away // System.out.flush(); } /** * Prints the message to stdout * * @param string the string to be printed */ public static void print(String string) { // print out the message System.out.println(string); // flush the output buffer so the text appears right away System.out.flush(); } /** * This method is called when the class is loaded from the command line. It * processes command line parameters, instantiates PuzzleLoader and * PuzzleSolver, and handles reporting on the console. * * @param argv command line parameters */ public static void main(String[] argv) { // uncomment this line to enable tests // Puzzle.testPieceName(); // print out program info print("MP4: Blocks Puzzle Solver by Michael Leonhard (mleonhar)"); print("CS340, Fall 2005, Instructor: Pat Troy, TA: Nitin Jindal"); print("2005-10-30 JDK 1.5.0.5, Eclipse 3.1.0, Windows XP\r\n"); boolean writeHTML = false; // an HTML report will be written String fileName = null; // the name of the file to read try { int argc = argv.length; // number of parameters dprint("Found " + argc + " parameters"); // loop through command line parameters for (int i = 0; i < argc; i++) { dprint("Checking parameter \"" + argv[i] + "\""); if (argv[i].equals("-h")) { // -h was specified before if (writeHTML) throw new PuzzleException( "Malformed Commandline: -h appears more than once"); // the user wants an HTML report writeHTML = true; } // -g is not supported else if (argv[i].equals("-g")) throw new PuzzleException( "Malformed Commandline: -g is not supported"); else { // file name was encountered already if (fileName != null) throw new PuzzleException( "Malformed Commandline: \"" + argv[i] + "\" is not expected"); // save the filename fileName = argv[i]; } } // file name was not found if (fileName == null) throw new PuzzleException(USAGE_TEXT); } // an error occurred while processing the command line catch (PuzzleException e) { // print out the error and exit print(e.toString()); return; } // load the initial puzzle PuzzleLoader loader = new PuzzleLoader(fileName); // show progress printInitialReport(loader); // the puzzle to solve Puzzle puzzle = loader.getPuzzle(); PuzzleSolution solution; // the puzzle was loaded, so search for a solution to it if (puzzle != null) solution = new PuzzleSolution(puzzle); // no puzzle was loaded, so there is no solution else solution = null; // generate reports printFinalReport(solution); if (writeHTML) writeHtmlReport(loader, solution); } /** * Writes a formatted HTML report with the results of the puzzle load and * solution search operations. The report is written to a file named * FNAME.html where FNAME is the String that was passed to the loader. * * @param loader the loaded file information * @param solution the solution to the puzzle, or null */ private static void writeHtmlReport(PuzzleLoader loader, PuzzleSolution solution) { FileWriter file = null; try { // check parameter if (loader == null) throw new IllegalArgumentException( "loader may not be null"); // the initial puzzle Puzzle puzzle = loader.getPuzzle(); // report file name String puzzleFileName = loader.getFileName(); String reportFileName = puzzleFileName + ".html"; MP4.print("Writing HTML report to file \"" + reportFileName + "\""); // open the report file file = new FileWriter(reportFileName); // html bits String htmlHeader = "" + "" + "
" + "End of report.
"; // HEADER file.write(htmlHeader); // TITLE file.write("Initial puzzle was loaded from "" + puzzleFileName + "" with no errors or warnings.
"); // some errors occurred else { // ERRORS TITLE file .write("Problems were encountered when reading the file "" + puzzleFileName + "":
" + "The puzzle has no solution.
"); file.write(htmlFooter); return; } // SOLUTION INFO file.write("The puzzle's solution consists of the following " + moves.size() + " moves: