public class EightPuzzle { /****************************************************** * Plays the "eight puzzle" at the keyboard. Uses the * EightPuzzle class to manage the moves. * The board is 3X3, indexed with the numbers 0, 1, and 2. * When a player makes a move, he/she enters m followed * by the row and column of the move. For example, m01 moves the * number in row 0, and column 1 to the adjacent empty square. * If no adjacent square is empty, the program * informs the user that the move is "illegal." * The user may re-initialize the game or quit the * program at any time. ******************************************************/ /****************************************************** * Precondition: ch is a character digit, like '6'. * Returns: The integer version of the character digit. ******************************************************/ public int toInt (char ch) { return (int) ch - (int) '0'; }//toInt /****************************************************** * Provides "on-line" help to the user. ******************************************************/ public void help () { System.out.println("\nAfter the prompt \"eightpuzzle>\":"); System.out.println(" Enter m directly followed by two digits (each 0, 1, or 2) to move."); System.out.println(" Enter q to quit."); System.out.println(" Enter n to start a new game."); System.out.println(" Enter h for help.\n"); } /****************************************************** * Constructor for EightPuzzle. Creates and initializes * the board. ******************************************************/ public EightPuzzle () { initBoard (); }//EightPuzzle constructor /****************************************************** * Initializes the board to numbers descending from 8 * down to 1 in this configuration: * 8 7 6 * 5 4 3 * 2 1 ******************************************************/ private void initBoard () { }//initBoard /****************************************************** * Precondition: legalMove has just been invoked. * Moves the number at the given row and column to the * current empty row and column. It does nothing if * the current empty row and column is not adjacent * to the given square. ******************************************************/ public void addAMove (int row, int column) { //MODIFY: Put the number in square (row, column) // into square (emptyRow, emptyColumn). Note this // assumes we have already checked that the squares // are adjacent (i.e., that (row, column) can be // legally moved). //Also, increment the number of moves. }//addAMove /****************************************************** * Displays the board. ******************************************************/ public void displayBoard() { } /****************************************************** * Precondition: The board has been initialized. * Returns: True if the puzzle is solved. ******************************************************/ //MODIFY: Stub that always returns false. public boolean gameWon () { return false; } /****************************************************** * This re-initializes the board, and resets any necessary * instance variables. ******************************************************/ public void resetGame () { initBoard(); //MODIFY: Re-initialize number of moves. } public int newRow () { return 0; } public int newColumn () { return 0; } /****************************************************** * Returns the number at the row and column, as a string. * Empty string if the square is empty. * Useful in the GUI part. ******************************************************/ public String numberAt (int row, int column) { return " "; } /****************************************************** * Method : legalMove. * Returns : true, if there is an adjacent square that * is empty. ******************************************************/ //SIDE EFFECT: This works via calls to empty(). See //empty() for description of the side effect. //MODIFY: Stub that returns true. //HINT: Invoke empty for the cases of (row-1, column), //(row+1, column), etc. There are 4 possibilities, so //4 calls to empty(). public boolean legalMove (int row, int column) { return true; }//legalMove /****************************************************** * Method : empty. * Returns : true, the square at row, column is empty. * is empty. ******************************************************/ // SIDE EFFECT: Sets private instance variables to // the row and column of the current empty square. //MODIFY: Stub that always returns true. public boolean empty (int row, int column) { return true; }//empty /****************************************************** * Returns the number of moves. ******************************************************/ //MODIFY: Stub that always returns 0. public int getMoves () { return 0; }//getMoves }