Class MazeCell

java.lang.Object
  extended by MazeCell

public class MazeCell
extends java.lang.Object

A MazeCell represents a single cell in a Maze. The class includes a number of methods to get information about the cell's location, neighbors, visitation state and so on. MazeRunners may also store arbitrary "extra info" with each MazeCell using the get/setExtraInfo methods, which may be useful for keeping track of paths to the donut.

Author:
Albert J. Wong (awong@cs)

Nested Class Summary
static class MazeCell.CellState
          CellState is an enumeration that represents the state of a given cell in the current search.
 
Field Summary
private  int col
          The column this cell is on.
private  java.lang.Object info
          A piece of state that may be used to hold information about the current search, if useful.
private  boolean isDonutCell
          Whether or not this cell is the donut cell.
private  boolean isStartCell
          Whether or not this cell is the start cell.
private  java.util.ArrayList<MazeChangeListener> mazeChangeListeners
          A list of the MazeChangeListener that will be notified on a visitation state change.
private  int numWalls
          The number of walls this cell has.
private  int row
          The row this cell is on.
private  MazeCell.CellState state
          The current visitation state of this cell.
private  boolean[] walls
          An array to keep track of which walls exist on this cell.
 
Constructor Summary
MazeCell(int cellRow, int cellCol, boolean isStart, boolean isDonut, boolean northWall, boolean eastWall, boolean southWall, boolean westWall)
          Creates a new MazeCell with the given parameters.
 
Method Summary
 void addMazeChangeListener(MazeChangeListener listener)
          Adds a new MazeChangeListener to listen to changes in this cell.
protected  void broadcastChange()
          Notifies all MazeChangeListener that have been previously registered via addMazeChangeListener of a change in this cell's visitation state.
 int getCol()
          Returns the column this cell is in.
 java.lang.Object getExtraInfo()
          Gets the object that was previously stored in this cell with setExtraInfo(Object newInfo).
 int getMaxNumWalls()
          Returns the total possible number of walls this cell has.
 int getNumWalls()
          Returns the number of walls that this cell has
 int getRow()
          Returns the row this cell is in.
 MazeCell.CellState getState()
          Returns the current visitation state of the Cell.
 boolean isDonut()
          Returns true if this cell is a donut cell.
 boolean isStart()
          Returns true if this cell is a start cell.
 boolean isWall(Direction d)
          Returns true if there is a wall in the direction d.
 void setExtraInfo(java.lang.Object newInfo)
          Stores an object in this cell that can be later retrieved with a call to getExtraInfo().
 void setState(MazeCell.CellState s)
          Sets the current visitation state of the Cell.
 java.lang.String toString()
          A defined toString function that will output a nicely formated string that specifies the location (row,col) of this cell.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
 

Field Detail

info

private java.lang.Object info
A piece of state that may be used to hold information about the current search, if useful.


state

private MazeCell.CellState state
The current visitation state of this cell.


mazeChangeListeners

private java.util.ArrayList<MazeChangeListener> mazeChangeListeners
A list of the MazeChangeListener that will be notified on a visitation state change.


walls

private boolean[] walls
An array to keep track of which walls exist on this cell.


numWalls

private int numWalls
The number of walls this cell has.


isStartCell

private boolean isStartCell
Whether or not this cell is the start cell.


isDonutCell

private boolean isDonutCell
Whether or not this cell is the donut cell.


row

private int row
The row this cell is on.


col

private int col
The column this cell is on.

Constructor Detail

MazeCell

public MazeCell(int cellRow,
                int cellCol,
                boolean isStart,
                boolean isDonut,
                boolean northWall,
                boolean eastWall,
                boolean southWall,
                boolean westWall)
Creates a new MazeCell with the given parameters.

Parameters:
cellRow - The row this cell is on.
cellCol - The column this cell is on.
isStart - true if this cell is the start cell.
isDonut - true if this cell is the donut cell.
northWall - true if this cell has a wall to the North.
eastWall - true if this cell has a wall to the East.
southWall - true if this cell has a wall to the South.
westWall - true if this cell has a wall to the West.
Method Detail

getState

public MazeCell.CellState getState()
Returns the current visitation state of the Cell. A cell can be marked as any value in CellState.

Returns:
Returns the current visitation state of the cell.

setState

public void setState(MazeCell.CellState s)
Sets the current visitation state of the Cell. A cell can be marked as any value in CellState.

Parameters:
s - Sets the visitation state to the state in s

getExtraInfo

public java.lang.Object getExtraInfo()
Gets the object that was previously stored in this cell with setExtraInfo(Object newInfo). This can be used to implement a form of closures on operations with this cell.

Returns:
The object that was last stored in this node with setExtraInfo(Object newInfo). If there was no previous call to setExtraInfo, null is returned.

setExtraInfo

public void setExtraInfo(java.lang.Object newInfo)
Stores an object in this cell that can be later retrieved with a call to getExtraInfo(). This can be used to implement a form of closures on operations with this cell.

Parameters:
newInfo - The object to store in this cell. This object may later be retrieved with a call to getExtraInfo(). newInfo may be null. This will remove any previously stored object from this cell.

addMazeChangeListener

public void addMazeChangeListener(MazeChangeListener listener)
Adds a new MazeChangeListener to listen to changes in this cell.

Parameters:
listener - Adds listener to the list of MazeChangeListener that will be notified when the visitation state of this cell is changed via setState(CellState s)

broadcastChange

protected void broadcastChange()
Notifies all MazeChangeListener that have been previously registered via addMazeChangeListener of a change in this cell's visitation state.


isStart

public boolean isStart()
Returns true if this cell is a start cell.

Returns:
true if this cell is a start cell. false otherwise.

isDonut

public boolean isDonut()
Returns true if this cell is a donut cell.

Returns:
true if this cell is a donut cell. false otherwise.

toString

public java.lang.String toString()
A defined toString function that will output a nicely formated string that specifies the location (row,col) of this cell.

Overrides:
toString in class java.lang.Object
Returns:
a string of the form "(row,col)".

isWall

public boolean isWall(Direction d)
Returns true if there is a wall in the direction d.

Parameters:
d - The direction in which to check for a wall.
Returns:
true if there is a wall in the direction d.

getNumWalls

public int getNumWalls()
Returns the number of walls that this cell has

Returns:
the number of walls this cell has

getMaxNumWalls

public int getMaxNumWalls()
Returns the total possible number of walls this cell has. This can be though of as the number of sides the cell has.

Returns:
the maximum number of wall this cell can have.

getRow

public int getRow()
Returns the row this cell is in.

Returns:
the row this cell is in.

getCol

public int getCol()
Returns the column this cell is in.

Returns:
the column this cell is in.