package puzzle; /** * The {@code State} class holds a representation of one of the (many) * states a {@link Puzzle} can be in. */ public class State implements Comparable { private final int[][] state; // Array holding tile numbers (blank has value dimension*dimension) private final int cost; // Estimate of cost to solve private final Puzzle.Direction move; // Move that led to this state private final State previous; // Previous state private final int blankRow; // Row of blank tile private final int blankColumn; // Column of blank tile /** * Constructor sets member variables and calculates cost. * @param state state of tiles * @param move move that led to this state * @param previous previous state * @param blankRow row of blank tile * @param blankColumn column of blank tile */ public State(int[][] state, Puzzle.Direction move, State previous, int blankRow, int blankColumn) { //TODO: Complete constructor } /** * Accessor for previous state * @return previous state */ public State getPrevious() { //TODO: Complete accessor } /** * Accessor for move that led to this state * @return move */ public Puzzle.Direction getMove() { //TODO: Complete accessor } /** * Method that checks to see if moving in a direction is allowed * (within bounds of puzzle). * @param direction of move * @return true if the move is allowed, false otherwise */ public boolean canMove(Puzzle.Direction direction) { //TODO: Complete method } /** * Creates a new state from current state based on moving in the given * direction. * @param direction of move * @return new state */ public State move(Puzzle.Direction direction) { //TODO: Complete method } /** * Estimates the cost in moves of solving a state. * For each tile (except the blank tile), it finds the row where it * should be (by dividing one less than its value by the width of the * puzzle) and the column where it should be (by modding one less than * its value by the width of the puzzle). Then, it finds the absolute * value of the difference between the row where it is and the row * where it should be and the absolute value of the difference of the * column where it is and the column where it should be and add these * values to the total cost. * @param state state to estimate the cost of * @return estimated cost in moves */ public static int cost(int[][] state) { //TODO: Complete method } /** * Checks to see whether the state is in a solved position. * For any tile, if the row times the size of the puzzle plus the * column plus one is ever *not* equal to its tile value, it's not * solved. Once all of the tiles have been checked and none of them * violate this rule, the puzzle must be solved. * @return true if the puzzle is solved, false otherwise */ public boolean isSolved() { //TODO: Complete method } /* (non-Javadoc) * @see java.lang.Comparable#compareTo(java.lang.Object) * Needed to make PriorityQueue work. */ @Override public int compareTo(State other) { return cost - other.cost; } /* (non-Javadoc) * @see java.lang.Object#equals(java.lang.Object) * Also needed to make PriorityQueue work. */ @Override public boolean equals(Object object) { if( !(object instanceof State) ) return false; State other = (State) object; if( other.state.length != state.length ) return false; int size = state.length; for( int i = 0; i < size; ++i ) for( int j = 0; j < size; ++j ) if( state[i][j] != other.state[i][j] ) return false; return true; } /* (non-Javadoc) * @see java.lang.Object#toString() * Needed to make hashCode() work. */ @Override public String toString() { StringBuilder output = new StringBuilder(); for( int i = 0; i < state.length; ++i ) for( int j = 0; j < state[i].length; ++j ) output.append(state[i][j] + ","); return output.toString(); } /* (non-Javadoc) * @see java.lang.Object#hashCode() * Needed to make HashSet work. */ @Override public int hashCode() { return toString().hashCode(); } }