Class State

java.lang.Object
  extended byState

public class State
extends java.lang.Object

This is the class for representing a single state of the rush hour puzzle. Methods are provided for constructing a state, for accessing information about a state, for printing a state, and for expanding a state (i.e., obtaining a list of all states immediately reachable from it).

Every car is constrained to only move horizontally or vertically. Therefore, each car has one dimension along which it is fixed, and another dimension along which it can be moved. This variable dimension is stored here as part of the state. A link to the puzzle with which this state is associated is also stored. Note that the goal car is always assigned index 0.

To make it easier to use State objects with some of the data structures provided as part of the Standard Java Platform, we also have provided hashCode and equals methods. You probably will not need to access these methods directly, but they are likely to be used implicitly if you take advantage of the Java Platform. These methods define two State objects to be equal if they refer to the same Puzzle object, and if they indicate that the cars have the identical variable positions in both states. The hashcode is designed to satisfy the general contract of the Object.hashCode method that it overrides, with regard to the redefinition of equals.


Constructor Summary
State(Puzzle puzzle, int[] varPos)
          The main constructor for constructing a state.
 
Method Summary
 boolean equals(java.lang.Object o)
          Returns true if and only if this state is considered equal to the given object.
 State[] expand()
          Computes all of the states immediately reachable from this state and returns them as an array of states.
 int[][] getGrid()
          Computes a grid representation of the state.
 Puzzle getPuzzle()
          Returns the puzzle associated with this state.
 int getVariablePosition(int v)
          Returns the variable position of car v.
 int hashCode()
          Returns a hash code value for this State object.
 boolean isGoal()
          Returns true if and only if this state is a goal state.
 void print()
          Prints to standard output a primitive text representation of the state.
 
Methods inherited from class java.lang.Object
getClass, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

State

public State(Puzzle puzzle,
             int[] varPos)
The main constructor for constructing a state. You probably will never need to use this constructor.

Parameters:
puzzle - the puzzle that this state is associated with
varPos - the variable position of each of the cars in this state
Method Detail

isGoal

public boolean isGoal()
Returns true if and only if this state is a goal state.


getVariablePosition

public int getVariablePosition(int v)
Returns the variable position of car v.


getPuzzle

public Puzzle getPuzzle()
Returns the puzzle associated with this state.


print

public void print()
Prints to standard output a primitive text representation of the state.


getGrid

public int[][] getGrid()
Computes a grid representation of the state. In particular, an nxn two-dimensional integer array is computed and returned, where n is the size of the puzzle grid. The (i,j) element of this grid is equal to -1 if square (i,j) is unoccupied, and otherwise contains the index of the car occupying this square. Note that the grid is recomputed each time this method is called.


expand

public State[] expand()
Computes all of the states immediately reachable from this state and returns them as an array of states. You probably will not need to use this method directly, since ordinarily you will be expanding Nodes, not States.


hashCode

public int hashCode()
Returns a hash code value for this State object. Although you probably will not need to use it directly, this method is provided for the benefit of hashtables given in the Java Platform. See documentation on Object.hashCode, which this method overrides, for the general contract that hashCode methods must satisfy.


equals

public boolean equals(java.lang.Object o)
Returns true if and only if this state is considered equal to the given object. In particular, equality is defined to hold if the given object is also a State object, if it is associated with the same Puzzle object, and if the cars in both states are in the identical positions. This method overrides Object.equals.