cmsc433.p3
Class Maze

java.lang.Object
  extended by cmsc433.p3.Maze
All Implemented Interfaces:
java.io.Serializable

public class Maze
extends java.lang.Object
implements java.io.Serializable

Stores the maze and provides functions for querying the maze.

See Also:
Serialized Form

Field Summary
 MazeDisplay display
          A reference to the graphical display of this maze.
 int height
           
 java.util.concurrent.atomic.AtomicIntegerArray maze
          The maze is a 2 dimensional array of atomic integers encoded as an AtomicIntegerArray.
 int width
           
 
Constructor Summary
Maze()
           
 
Method Summary
 boolean canMove(Position pos, Direction dir)
          Returns true if it is possible to move in direction @dir when at position @pos.
 boolean checkSolution(java.util.List<Direction> soln)
          Checks that a solution is correct.
 int getCell(Position pos)
          Returns the integer at the given position.
 int getColor(Position pos)
          Gets the color of the cell at position 'pos'.
 int getColor(Position pos, Direction dir)
          Gets the color of the edge in direction 'dir' at position 'pos'.
 Position getEnd()
          Returns the position corresponding to the exit of the maze.
 int getHeight()
          Returns the height of the maze.
 java.util.LinkedList<Direction> getMoves(Position pos)
          Returns the list of open directions at this position.
 Position getStart()
          Returns the position corresponding to the entrance of the maze.
 int getWidth()
          Returns the width of the maze.
 void setColor(Position pos, Direction dir, int color)
          Sets the color of the edge in direction 'dir' at position 'pos' to the specified value, which must be between 0 and 3 inclusive.
 void setColor(Position pos, int color)
          Sets the color of the cell at position 'pos' to the specified value, which must be between 0 and 3 inclusive.
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

maze

public java.util.concurrent.atomic.AtomicIntegerArray maze

The maze is a 2 dimensional array of atomic integers encoded as an AtomicIntegerArray. The upper-left, or north-western corner of the maze is given by maze.get(0). The lower-right, or south-eastern corner of the maze is maze.get(maze.length()-1). The maze is stored in row-major order, so the element at row n, column m is given by maze.get(n * width + m).

The entrance to the maze is at row: 0, column: width/2. The exit is at row: height-1, column: width/2.

Individual bits of each byte in the "maze" array are used to encode information about that cell of the maze. Only the low-order 8 bits are used. The designated bits are:

  CCSSEEse

and have the following meaning

CC
marks two bits that are used to give a color to this cell in the maze. Each of the four bit combinations corresponds to a different color that will be used by the MazeDisplay object when drawing this cell. This can be used by your code to provide visual feedback on solving progress or for debugging.
SS
marks two bits that are used to give a color to the southern side of this cell.
EE
marks two bits that are used to give a color to the eastern side of this cell.
s
Set to 1 if there is a wall to the SOUTH.
e
Set to 1 if there is a wall to the EAST.

There is always a wall to the south of cells on the southern border of the maze. There is always a wall to the east of cells on the eastern border of the maze. There is an implicit wall to the north of cells on the northern border and to the west of cells on the western border.


width

public int width

height

public int height

display

public transient MazeDisplay display
A reference to the graphical display of this maze. This will be null if graphical maze display is not currently enabled.

Constructor Detail

Maze

public Maze()
Method Detail

getCell

public int getCell(Position pos)
Returns the integer at the given position. The returned value uses the encoding described in the documentation for the maze field.


canMove

public boolean canMove(Position pos,
                       Direction dir)
Returns true if it is possible to move in direction @dir when at position @pos.


getMoves

public java.util.LinkedList<Direction> getMoves(Position pos)
Returns the list of open directions at this position. A direction is open if it is not blocked by a wall.


getWidth

public int getWidth()
Returns the width of the maze.


getHeight

public int getHeight()
Returns the height of the maze.


setColor

public void setColor(Position pos,
                     int color)
Sets the color of the cell at position 'pos' to the specified value, which must be between 0 and 3 inclusive.


getColor

public int getColor(Position pos)
Gets the color of the cell at position 'pos'. The returned value will be between 0 and 3 inclusive.


setColor

public void setColor(Position pos,
                     Direction dir,
                     int color)
Sets the color of the edge in direction 'dir' at position 'pos' to the specified value, which must be between 0 and 3 inclusive. The northern wall of top-row cells and the western wall of left-column cells cannot be colored.


getColor

public int getColor(Position pos,
                    Direction dir)
Gets the color of the edge in direction 'dir' at position 'pos'. The northern wall of top-row cells and the western wall of left-column cells cannot be colored. The color 0 will be returned for these cases.


getStart

public Position getStart()
Returns the position corresponding to the entrance of the maze.


getEnd

public Position getEnd()
Returns the position corresponding to the exit of the maze.


checkSolution

public final boolean checkSolution(java.util.List<Direction> soln)
Checks that a solution is correct.

Returns:
true if the solution is correct, false otherwise.