//Chin-Kai Chang(chinkaic@usc.edu) //CSCI460 HW2 8-puzzle problem import java.io.*; import java.util.*; public class Main{ private Solver puzzle_solver; private Puzzle puzzles; public static void main(String ARGS[]){ if(ARGS.length!=2){ System.err.println("Usage: java Main input_file output_file"); return; } Main main = new Main(); int[][] inputset = new int[3][3]; //Read input file try{ FileReader frs= new FileReader(ARGS[0]); StringTokenizer in; BufferedReader inline =new BufferedReader(frs); for(int i= 0;i<3;i++){ in= new StringTokenizer(inline.readLine()); for(int j = 0 ; j<3;j++){ inputset[i][j]= Integer.parseInt(in.nextToken()); } } frs.close(); }catch(FileNotFoundException fnfe){ System.err.println("Please enter correct input file name"); return; }catch(NullPointerException npe){ npe.printStackTrace(); }catch(Exception e){ e.printStackTrace(); } main.initSolver(); main.initPuzzle(inputset); main.puzzles.print(); try{ main.puzzle_solver.solve(); }catch(OutOfMemoryError oome){ System.err.println("Can't Find Solution"); return; } String output =new String("Sequence of tiles to be moved:"+main.getSoulution()+"\n"); output +=new String("Number of moves:"+main.getMoveTimes()); //Write output file try{ FileWriter fws= new FileWriter(ARGS[1]); fws.write(output); fws.close(); }catch(Exception e){ e.printStackTrace(); } System.out.println(output); } public Main() { //puzzles = new Puzzle(); } public Puzzle getPuzzle(){ return puzzles; } public String getSoulution(){ return puzzle_solver.getSoulution(); } public int getMoveTimes(){ return puzzle_solver.getMoveTimes(); } public void initSolver(){ puzzle_solver = new Solver(this); } public void initPuzzle(int[][] n){ puzzles = new Puzzle(n); } }