// import statements public class PuzzleSkeleton { public static void main(String[] args) { // starting grid char[] grid={'1', '4', '3', '-', '6', '5', '2', '7', '8'}; // goal grid char[] goal={'1', '2', '3', '4', '5', '-', '6', '7', '8'}; char move; int turn=0; Scanner in = new Scanner(System.in); // play the game while the grid does not match the goal while(!isGoal(grid,goal)) { turn++; move=getMove(grid,in); // get the user's move (which tile will be moved) update(move,grid); // update the grid based on the move } JOptionPane.showMessageDialog(null,"It took you " + turn + " turns to get " + getGridForOutput(grid)); // output result System.exit(0); } // this method will compare each array value of grid with goal and if there are any mismatches, return false, otherwise return true. // That is, if g1[i]==g2[i] for all i, return true, else return false. public static boolean isGoal(char[] g1, char[] g2) { // fill in the code here } // this method will update the grid by swapping the tile, move, with the blank public static void update(char move, char[] grid) { char temp; int blank=getLocationOfBlank(grid); // you have to implement getLocationOfBlank int tile=getLocationOfTile(move, grid); // you have to implement getLocationOfTile temp=grid[blank]; grid[blank]=grid[tile]; grid[tile]=temp; } // this method will display the current grid and ask the user which tile he/she wishes to move. The tile can only be // moved to the location of the blank, so the choice will only be a tile adjacent to the blank. You SHOULD verify that the // choice is correct which means it cannot be < '1' or > '8' and that it is adjacent to the blank. This last part is // challenging and will be explained in the lecture. public static char getMove(char[] grid, Scanner in) { char move; do { // you will need to implement printGrid printGrid(grid); System.out.print("Enter the tile you wish to move (1-8): "); move=in.next().charAt(0); }while(/*place data verification conditions here*/); return move; } // this method returns the index of the blank (which is denoted as '-') public static int getLocationOfBlank(char[] g) { int location=-1; // place code here to search for the blank return location; } // this method will receive the tile we want to move, which is a character like '5', and will find its index in the grid public static int getLocationOfTile(char t, char[] g) { int location=-1; // place code here to search for the tile t return location; } /* this method can be used to make sure that the move input is a legal one in that we need to make sure that the tile is adjacent to * the blank -- this is optional, only implement this once you have the rest of the game working but without this method, the user * will be able to cheat! public static boolean moveLegal(char m, char[] g) { int location=getLocationOfTile(m, g); int blank=getLocationOfBlank(g); switch(location) { // the switch statement will have for each possible location of the tile to be moved an entry like: // Case 0: if(blank==1||blank==3) return true; break; // the above statement says that if the user wants to move the tile at array location 0, then the blank must // either be to its right or below it } return false; // if none of the cases match or if a case matches but the blank is not where expected, return false, not a legal move } */ public static void printGrid(char[] grid) { // output the grid using a format like this: // 1 2 3 // 4 b 5 // 6 7 8 // you can use ' ' to output the blank or a character like 'b' or '_' or '-' } }