### Question:
Solver { public int moves() { return solution.size() - 1; } Solver(WorldState initial); int moves(); Iterable<WorldState> solution();  }### Answer:
@Test(timeout = 10000) public void testWordPuzzles() { In in = new In("input/word_puzzles.txt"); while (!in.isEmpty()) { WordPuzzleSolution wps = stringToWordPuzzle(in.readLine()); if (wps == null) { continue; } Word w = new Word(wps.start, wps.goal); Solver s = new Solver(w); String errorMessage = "Wrong number of moves solving " + wps.start + "->" + wps.goal; assertEquals(errorMessage, wps.numMoves, s.moves()); } }
@Test(timeout = 1000) public void test2x2BoardPuzzles() { for (int i = 0; i <= 6; i += 1) { String pnum = String.format("%02d", i); String puzzleName = "input/puzzle2x2-" + pnum + ".txt"; Board b = readBoard(puzzleName); int numMoves = i; BoardPuzzleSolution bps = new BoardPuzzleSolution(puzzleName, b, numMoves); Solver s = new Solver(b); assertEquals("Wrong number of moves on " + puzzleName, bps.numMoves, s.moves()); System.out.println("Solved 2x2 Board Puzzles: " + (i+1)); } }
@Test public void test3x3BoardPuzzles() { for (int i = 0; i <= 30; i += 1) { String pnum = String.format("%02d", i); String puzzleName = "input/puzzle3x3-" + pnum + ".txt"; Board b = readBoard(puzzleName); int numMoves = i; BoardPuzzleSolution bps = new BoardPuzzleSolution(puzzleName, b, numMoves); Solver s = new Solver(b); assertEquals("Wrong number of moves on " + puzzleName, bps.numMoves, s.moves()); System.out.println("Solved 3x3 Board Puzzles: " + (i+1)); } }
@Test public void test4x4BoardPuzzles() { for (int i = 0; i <= 30; i += 1) { String pnum = String.format("%02d", i); String puzzleName = "input/puzzle4x4-" + pnum + ".txt"; Board b = readBoard(puzzleName); int numMoves = i; BoardPuzzleSolution bps = new BoardPuzzleSolution(puzzleName, b, numMoves); Solver s = new Solver(b); assertEquals("Wrong number of moves on " + puzzleName, bps.numMoves, s.moves()); System.out.println("Solved 4x4 Board Puzzles: " + (i+1)); } }
@Test public void testVariousPuzzles() { for (int i = 0; i <= 31; i += 1) { String pnum = String.format("%02d", i); String puzzleName = "input/puzzle" + pnum + ".txt"; Board b = readBoard(puzzleName); int numMoves = i; BoardPuzzleSolution bps = new BoardPuzzleSolution(puzzleName, b, numMoves); Solver s = new Solver(b); assertEquals("Wrong number of moves on " + puzzleName, bps.numMoves, s.moves()); System.out.println("Solved Various Puzzles: " + (i+1)); } }