Class Maze

java.lang.Object
  |
  +--Maze

public class Maze
extends Object
implements MazeModel

A maze. Holds the maze layout, the finish location and the current player location. The maze is three dimensions, but acts like a two dimensional maze if the Z dimension has a size of 1. Each cell contains a bitmap representing which exits are available from that cell. The exits are known as XPL,XMI,YPL,YMI,ZPL,ZMI representing a plus and minus direction exit in each of the 3 dimensions. The maze is created such that adjoining cells share openings so that the "doorway" is two directional. There are no exits around the outside of the maze. The finish is just a designated cell.


Fields inherited from interface MazeModel
MARK, WALLS, XMI, XPL, YMI, YPL, ZMI, ZPL
 
Constructor Summary
Maze(Coordinate3D size)
          Contruct a new maze with the given dimensions.
Maze(int xdim, int ydim, int zdim)
          Contruct a new maze with the given dimensions.
 
Method Summary
 void addMazeListener(MazeListener l)
          Add a MazeListener.
static int count(int val)
          Count how many open walls there are in the cell value.
 void create()
          Create a random maze.
static String directionString(byte direction)
          Get a String representation of the given direction.
protected  void fireChange()
          Fire a change in the maze.
static Coordinate3D forward(Coordinate3D c, byte direction)
          Move between cells in a maze.
 Coordinate3D getCurrent()
          Get the current player co-ordinate.
 Coordinate3D getFinish()
          Get the maze finish cell location.
 Coordinate3D getSize()
          Get the size of this maze.
 byte grid(Coordinate3D c)
          Get the given cell walls and status.
 byte grid(int x, int y, int z)
          Get the given cell walls and status.
static byte invert(byte value)
          Return the opposite direction.
 boolean isMarked(Coordinate3D position)
          Check if the given cell has an "I've been here" marker.
 boolean isOpen(Coordinate3D current, byte direction)
          Check and see if movement is possible.
 void removeMazeListener(MazeListener l)
          Remove a MazeListener.
 void setCurrent(Coordinate3D current)
          Set the current player co-ordinate.
 void setFinish(Coordinate3D finish)
          Set where the finish cell is.
 void setMark(Coordinate3D position, boolean mark)
          Put or remove a "I've been here" marker in a cell.
 void setRandomFinish()
          Make a random finish location.
 Stack solve(Coordinate3D start)
          Solve the maze from a given start position.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Maze

public Maze(int xdim,
            int ydim,
            int zdim)
Contruct a new maze with the given dimensions.
Parameters:
xdim - The x dimension of the maze.
ydim - The y dimension of the maze.
zdim - The z dimension of the maze. Use 1 for a 2D maze.

Maze

public Maze(Coordinate3D size)
Contruct a new maze with the given dimensions.
Parameters:
size - The 3D size of the maze.
Method Detail

getSize

public Coordinate3D getSize()
Get the size of this maze.
Specified by:
getSize in interface MazeModel
Returns:
The 3D size of the maze.

setMark

public void setMark(Coordinate3D position,
                    boolean mark)
Put or remove a "I've been here" marker in a cell.
Specified by:
setMark in interface MazeModel
Parameters:
position - The cell position.
mark - True to place a mark, False to remove a mark.

grid

public byte grid(int x,
                 int y,
                 int z)
Get the given cell walls and status.
Specified by:
grid in interface MazeModel
Parameters:
x - The x co-ordinate.
y - The y co-ordinate.
z - The z co-ordinate.
Returns:
The cell status, MARK if an invalid cell co-ordinate.

grid

public byte grid(Coordinate3D c)
Get the given cell walls and status.
Specified by:
grid in interface MazeModel
Parameters:
c - The 3D co-ordinate of the desired cell.
Returns:
The cell status, MARK if an invalid cell co-ordinate.

setFinish

public void setFinish(Coordinate3D finish)
Set where the finish cell is.
Specified by:
setFinish in interface MazeModel
Parameters:
finish - The 3D co-ordinate of the end of the maze.

setRandomFinish

public void setRandomFinish()
Make a random finish location.
Specified by:
setRandomFinish in interface MazeModel

getFinish

public Coordinate3D getFinish()
Get the maze finish cell location. May be null if one hasn't been set.
Specified by:
getFinish in interface MazeModel
Tags copied from interface: MazeModel
Returns:
The 3D co-ordinate of the cell.

getCurrent

public Coordinate3D getCurrent()
Get the current player co-ordinate.
Specified by:
getCurrent in interface MazeModel
Returns:
The 3D co-ordinate of the player in the maze.

setCurrent

public void setCurrent(Coordinate3D current)
Set the current player co-ordinate.
Specified by:
setCurrent in interface MazeModel
Parameters:
current - The 3D co-ordinate of the player in the maze.

isMarked

public boolean isMarked(Coordinate3D position)
Check if the given cell has an "I've been here" marker.
Specified by:
isMarked in interface MazeModel
Parameters:
position - The 3D co-ordinate of the cell to check.
Returns:
True if a marker has been placed.

directionString

public static String directionString(byte direction)
Get a String representation of the given direction.
Parameters:
direction - One of MazeModel.XMI,XPL,YMI,YPL,ZMI,ZPL.
Returns:
A String representation of one of the above.

count

public static final int count(int val)
Count how many open walls there are in the cell value.
Parameters:
val - The internal value representing the cell.
Returns:
The number of open walls (0 thru 6).

create

public void create()
Create a random maze.

invert

public static byte invert(byte value)
Return the opposite direction.
Parameters:
value - One of XPL,XMI,YPL,YMI,ZPL,ZMI.
Returns:
One of XMI,XPL,YMI,YPL,ZMI,ZPL.

forward

public static Coordinate3D forward(Coordinate3D c,
                                   byte direction)
Move between cells in a maze. Does not check for walls or maze edges.
Parameters:
c - The 3D co-ordinate to start from.
direction - The direction to go, one of XPL,XMI etc.
Returns:
The ending 3D co-ordinate.

isOpen

public boolean isOpen(Coordinate3D current,
                      byte direction)
Check and see if movement is possible.
Specified by:
isOpen in interface MazeModel
Parameters:
current - The 3D co-ordinate to start from.
direction - The direction to go, one of XPL,XMI etc.
Returns:
True if movement possible, false if blocked by a wall.

solve

public Stack solve(Coordinate3D start)
Solve the maze from a given start position.
Specified by:
solve in interface MazeModel
Parameters:
start - The 3D co-ordinate to start from.
Returns:
A Stack of 3D co-ordinates giving the path from the given start to the known finish.

addMazeListener

public void addMazeListener(MazeListener l)
Add a MazeListener. An event is fired when the maze is created or when the finish or current positions change.
Specified by:
addMazeListener in interface MazeModel
Parameters:
l - A MazeListener to listen for maze changes.

removeMazeListener

public void removeMazeListener(MazeListener l)
Remove a MazeListener.
Specified by:
removeMazeListener in interface MazeModel
Parameters:
l - The MazeListener to remove.

fireChange

protected void fireChange()
Fire a change in the maze.