import java.util.Random; /* Java application having as its purpose to test the solution() method ** of the SliderPuzzleSolver class. ** ** Author: R. McCloskey ** Date: April 2022 */ public class SPS_Tester { /* Provided with command line arguments (or what jGrasp refers to as ** "run arguments") indicating the dimensions of a puzzle, a seed ** for a pseudo-random number generator, and a number of pseudo-random ** moves to make --starting with the standard goal configuration-- to ** obtain an initial configuration, this method uses the solution() ** method in the SliderPuzzleSolver class to find a minimum-length ** sequence of moves that transforms that initial configuration into ** the standard goal configuration. This method then displays the ** configurations along that path of moves. */ public static void main(String[] args) { int numRows = Integer.parseInt(args[0]); int numCols = Integer.parseInt(args[1]); int seed = Integer.parseInt(args[2]); Random rand = new Random(seed); int numMoves = Integer.parseInt(args[3]); SliderPuzzleConfig start = new SliderPuzzleConfig(numRows, numCols, rand, numMoves); System.out.println("Starting configuration is"); start.display(); System.out.println(); String solution = SliderPuzzleSolver.solution(start); if (solution == null) { System.out.println("No solution was found."); } else { System.out.printf("Path of length %d found:\n\n", solution.length()); displayPath(start, solution); } System.out.println("\nGoodbye."); } /* Displays the sequence of configurations starting with the one ** given and proceeding according to the given sequence of moves. */ public static void displayPath(SliderPuzzleConfig config, String moveSeq) { SliderPuzzleConfig spc = config.clone(); spc.display(); for (int i = 0; i != moveSeq.length(); i++) { char dir = moveSeq.charAt(i); spc.move(dir); System.out.printf("\nAfter moving %c:\n", dir); spc.display(); } } }