import java.io.*; public class PlayPuzzle { public static String type = "array"; public static boolean playAgain = true; public static int moves; public static final int SIZE = 3; public static BufferedReader in = new BufferedReader(new InputStreamReader(System.in)); public static IPuzzle puzzle; // Play 3x3 game: public static void main (String[] args) { setGameParameters(args); // user's game settings welcomeUser(); // say hello playGame(); // single game play } // Change game settings: private static void setGameParameters(String[] args) { // Get type commandline; default type is array if nothing entered: if (args.length > 0) type = args[0]; } // Welcome the user: private static void welcomeUser() { System.out.println("Welcome to Puzzle!"); System.out.println("Press Return to quit at any time."); System.out.println(); } // Play one game: private static void playGame() { // Create the puzzle: if (type.equals("array")) puzzle = new PuzzleAsArray(); else if (type.equals("int")) puzzle = new PuzzleAsInt(); else if (type.equals("string")) puzzle = new PuzzleAsString(); else { System.out.println("Type of puzzle ("+type+") not recognized!"); System.exit(0); } // Scramble the puzzle: puzzle.scramble(); // Show current state: System.out.println("Current state of puzzle:"); puzzle.display(); // Make tile moves until the puzzle is solved: while( !puzzle.isSolved() ) { System.out.println("The puzzle isn't solved yet."); // Move a tile into the blank space and test the puzzle again: char tryMove = getMove(); while( !puzzle.move(tryMove) ) { System.out.println("You cannot move a tile in that direction."); tryMove = getMove(); } // Keep track of valid moves made: moves++; // Show current state: System.out.println("Current state of puzzle:"); puzzle.display(); } // Done! System.out.println("Congratulations!"); System.out.println("Number of moves you took: "+moves+"."); } // Get a character move: private static char getMove() { // Get user input: char move=' '; String user1,user2; try { System.out.print("Enter a move [NSEW]: "); move=((in.readLine()).toUpperCase()).charAt(0); while ( !(move=='N' || move=='S' || move=='E' || move=='W') ) { System.out.print("I do not recognize that move. "); System.out.print("Enter a real move [NSEW]: "); move = ((in.readLine()).toUpperCase()).charAt(0); } } catch(Exception e) { System.out.println("Quitting!"); System.exit(0); } System.out.println(); return move; } } // Class PlayPuzzle