de.caff.maze
Class AbstractBasicMaze

java.lang.Object
  extended by de.caff.maze.MazePropertyOwner
      extended by de.caff.maze.AbstractBasicMaze
All Implemented Interfaces:
Maze
Direct Known Subclasses:
CircularMaze, DiamondMaze, HexagonalMaze, OctogonalMaze, RectangularMaze, TriangularMaze

public abstract class AbstractBasicMaze
extends MazePropertyOwner
implements Maze

The abstract basis for a maze. This class defines a lot of useful stuff common to all mazes. The live cycle of a maze mainly falls in three parts:

  1. Geometry creation First a geometry is created which is in principle a description of maze cells and their neighbours. As long as the geometric properties are not changed this description is constant.
  2. Maze creation Second these cells have to be connected to create the real maze. The algorithm used creates exactly one way between any two cells of the geometry. This class allows the definition of two way points and calculates the resulting way between them (called the solution). This is usually repeated more often than part 1.
  3. Drawing The maze is drawn. This part is usually done more often than part 2.
The extending classes need not care for step 2. Creation of a random maze and calculation of the solution is done here. The drawing of the solution and the way points may be delegated from extending classes, too.

 

Nested Class Summary
protected static class AbstractBasicMaze.DoubleDelayedPropertyInformation
          A delayed setter for double values.
protected static class AbstractBasicMaze.IntegerDelayedPropertyInformation
          A delayed setter for integer values.
 
Nested classes/interfaces inherited from class de.caff.maze.MazePropertyOwner
MazePropertyOwner.BooleanPropertyInformation, MazePropertyOwner.EnumPropertyInformation, MazePropertyOwner.InfoPropertyDisplay, MazePropertyOwner.IntegerPropertyInformation, MazePropertyOwner.PaintPropertyInformation
 
Field Summary
static float BOX_SIZE
          The size of the box to which the maze is drawn.
static java.lang.String PROPERTY_CREATION_TIME
          The property used when the time needed for the creation is newly calculated.
static java.lang.String PROPERTY_MAZE
          The property used when the maze changes.
static java.lang.String PROPERTY_NUMBER_CELLS
          The property used when the number of cells is changed.
static java.lang.String PROPERTY_SEED
          The property used when the seed changes.
static java.lang.String PROPERTY_SOLUTION_LENGTH
          The property used when the length of the solution chnages.
static java.lang.String PROPERTY_WAY
          The property used when the way changes.
static java.lang.String PROPERTY_WAY_POINTS
          The property used when the way points (start and/or end) changes.
 
Constructor Summary
protected AbstractBasicMaze()
          Default constructor.
 
Method Summary
 void addMazeFinishedListener(MazeFinishedListener listener)
          Add a maze finished listener which is called when the maze (re)creation is finished.
static int arg2int(java.lang.String[] args, int arg, int defaultValue)
          Helper method creating an int value from a string from an array, used in test code in main() methods of extending classes.
 void createMaze()
          Create the maze with a random seed.
 void createMaze(long randomSeed)
          Create a maze with the given randomSeed.
 void createMaze(long randomSeed, int version)
          Create a maze with the given randomSeed using a special version of the creation algorithm.
protected abstract  void doDraw(MazePainter painter, MazePaintPropertiesProvider properties)
          Do the actual drawing.
 void draw(MazePainter painter, MazePaintPropertiesProvider properties)
          Draw this maze.
protected  void drawBackgroundAndWay(MazePainter painter, MazePaintPropertiesProvider properties)
          Draws the background.
 MazeCell getCellAt(java.awt.geom.Point2D position)
          Get the cell at a given point.
 AbstractBasicMaze getClone()
          Get a clone of this maze.
 long getCreationTimeMillis()
          Get the creation time of this maze.
protected abstract  AbstractBasicMaze getGeometryClone()
          Get a deep copy of this maze geometry.
 java.lang.String getInfo(boolean onlyVariable)
           
abstract  java.lang.String getMazeType()
          Get a internally used string describing the maze type.
protected abstract  java.awt.Shape getOuterBorder()
          Get the borders of the maze as a shape.
 java.util.Collection<PropertyInformation> getPropertyInformations()
          Get the property setters and displays for this maze.
 long getSeed()
          Get the seed which with the current maze is created.
 java.util.Collection<MazeCell> getWay()
          Get the solution way.
 MazeCell getWayEnd()
          Get the end cell of the way.
 MazeCell getWayStart()
          Get the start cell of the way.
 boolean isDuringRecreation()
          Is a recreation running?
protected  void loadSeedWayAndVersion(DataStorage dataStorage, java.lang.String prefix)
          Load the seed and way points from the given data storage.
protected abstract  void recreateFromDelayedSetters()
          This is called during the call of AbstractBasicMaze.setFromSetters() and should be used to recreate the geometry of the maze from the setters of the geometric properties.
protected  void recreateMaze()
          Recreate the maze with the current seed.
 void removeMazeFinishedListener(MazeFinishedListener listener)
          Remove a maze finished listener which was called when the maze (re)creation is finished.
 void reset()
          Resets the internal data.
abstract  void setDefaultWayPoints()
          Set some useful default way points.
 void setFromSetters()
          Set all values from the setters of the maze, than recreate it.
 void setProgressShower(ProgressShower progressShower)
          Set the progress shower which is called during maze creation.
 void setWayEnd(MazeCell end)
          Set the end cell of the way.
 void setWayPoints(MazeCell start, MazeCell end)
          Set the way start and end points.
 void setWayStart(MazeCell start)
          Set the start cell of the way.
protected  void solve()
          Solve this maze.
protected  void storeSeedWayAndVersion(DataStorage dataStorage, java.lang.String prefix)
          Store the seed and way points to the given data storage.
 
Methods inherited from class de.caff.maze.MazePropertyOwner
addPropertyChangeListener, firePropertyChange, removePropertyChangeListener
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface de.caff.maze.Maze
getCellByID, getCells, getInsets, getPreferredAspectRatio, loadPersistentData, storePersistentData
 

Field Detail

BOX_SIZE

public static final float BOX_SIZE
The size of the box to which the maze is drawn. A size of 1.0 would be more clearly but there are some bugs in java2d which make this a better choice.

See Also:
Constant Field Values

PROPERTY_MAZE

public static final java.lang.String PROPERTY_MAZE
The property used when the maze changes.

See Also:
Constant Field Values

PROPERTY_WAY_POINTS

public static final java.lang.String PROPERTY_WAY_POINTS
The property used when the way points (start and/or end) changes.

See Also:
Constant Field Values

PROPERTY_WAY

public static final java.lang.String PROPERTY_WAY
The property used when the way changes.

See Also:
Constant Field Values

PROPERTY_SEED

public static final java.lang.String PROPERTY_SEED
The property used when the seed changes.

See Also:
Constant Field Values

PROPERTY_NUMBER_CELLS

public static final java.lang.String PROPERTY_NUMBER_CELLS
The property used when the number of cells is changed.

See Also:
Constant Field Values

PROPERTY_SOLUTION_LENGTH

public static final java.lang.String PROPERTY_SOLUTION_LENGTH
The property used when the length of the solution chnages.

See Also:
Constant Field Values

PROPERTY_CREATION_TIME

public static final java.lang.String PROPERTY_CREATION_TIME
The property used when the time needed for the creation is newly calculated.

See Also:
Constant Field Values
Constructor Detail

AbstractBasicMaze

protected AbstractBasicMaze()
Default constructor.

Method Detail

getGeometryClone

protected abstract AbstractBasicMaze getGeometryClone()
Get a deep copy of this maze geometry.

Returns:
deep copy

getClone

public AbstractBasicMaze getClone()
Get a clone of this maze. This may be a costly operation.

Returns:
a clone of this maze

isDuringRecreation

public boolean isDuringRecreation()
Is a recreation running?

Returns:
true during a recreation phase

solve

protected void solve()
Solve this maze. This calculates a way between the way points.

See Also:
AbstractBasicMaze.setWayPoints(MazeCell, MazeCell), AbstractBasicMaze.setWayStart(MazeCell), AbstractBasicMaze.getWayStart(), AbstractBasicMaze.setWayEnd(MazeCell), AbstractBasicMaze.getWayEnd(), AbstractBasicMaze.getWay()

setWayPoints

public void setWayPoints(MazeCell start,
                         MazeCell end)
Set the way start and end points. Implicitely creates a solution.

Parameters:
start - way start
end - way end

setWayStart

public void setWayStart(MazeCell start)
Set the start cell of the way.

Specified by:
setWayStart in interface Maze
Parameters:
start - start cell

setWayEnd

public void setWayEnd(MazeCell end)
Set the end cell of the way.

Specified by:
setWayEnd in interface Maze
Parameters:
end - end cell

getWayStart

public MazeCell getWayStart()
Get the start cell of the way.

Returns:
start cell

getWayEnd

public MazeCell getWayEnd()
Get the end cell of the way.

Returns:
end cell

getWay

public java.util.Collection<MazeCell> getWay()
Get the solution way.

Returns:
the solution or null if there is no solution

reset

public void reset()
Resets the internal data. Should be overwritten, and overwritung methods should call this.

Specified by:
reset in interface Maze

createMaze

public void createMaze()
Create the maze with a random seed.

Specified by:
createMaze in interface Maze

recreateMaze

protected void recreateMaze()
Recreate the maze with the current seed.


setFromSetters

public void setFromSetters()
Set all values from the setters of the maze, than recreate it.

See Also:
AbstractBasicMaze.recreateFromDelayedSetters()

setProgressShower

public void setProgressShower(ProgressShower progressShower)
Set the progress shower which is called during maze creation.

Parameters:
progressShower - progress shower

recreateFromDelayedSetters

protected abstract void recreateFromDelayedSetters()
This is called during the call of AbstractBasicMaze.setFromSetters() and should be used to recreate the geometry of the maze from the setters of the geometric properties.


createMaze

public void createMaze(long randomSeed)
Create a maze with the given randomSeed.

Specified by:
createMaze in interface Maze
Parameters:
randomSeed - random randomSeed

createMaze

public void createMaze(long randomSeed,
                       int version)
Create a maze with the given randomSeed using a special version of the creation algorithm.

Parameters:
randomSeed - random randomSeed
version - maze creation algorithm version

getSeed

public long getSeed()
Get the seed which with the current maze is created.

Returns:
the seed

getCreationTimeMillis

public long getCreationTimeMillis()
Get the creation time of this maze.

Returns:
creation time in milliseconds

arg2int

public static int arg2int(java.lang.String[] args,
                          int arg,
                          int defaultValue)
Helper method creating an int value from a string from an array, used in test code in main() methods of extending classes.

Parameters:
args - array of strings
arg - index into the array
defaultValue - value to return if args[arg] is not defining an int
Returns:
integer value, either from args[arg] or the default value

setDefaultWayPoints

public abstract void setDefaultWayPoints()
Set some useful default way points.


getMazeType

public abstract java.lang.String getMazeType()
Get a internally used string describing the maze type.

Returns:
maze type

drawBackgroundAndWay

protected void drawBackgroundAndWay(MazePainter painter,
                                    MazePaintPropertiesProvider properties)
Draws the background. Draw the solution if it is wished by the user. Draws the way points, too. This should be called early in their draw routine, before any borders are drawn, but after the shapes are (re)created.

Parameters:
painter - painter to draw to
properties - graphical maze properties

doDraw

protected abstract void doDraw(MazePainter painter,
                               MazePaintPropertiesProvider properties)
Do the actual drawing. The call to this method is embedded in the the calls to MazePainter.startPaintingMaze(Maze) and MazePainter.endPaintingMaze().

Parameters:
painter - painter to draw to
properties - access to properties for drawing (colors etc)

draw

public void draw(MazePainter painter,
                 MazePaintPropertiesProvider properties)
Draw this maze.

Specified by:
draw in interface Maze
Parameters:
painter - painter to draw to
properties - access to properties for drawing (colors etc)

getCellAt

public MazeCell getCellAt(java.awt.geom.Point2D position)
Get the cell at a given point. This method uses the second worst algorithm (first checking the outer border of the maze, and if necessary checking each cell shape whether it contains the point), so extending classes are encouraged to use their better understanding of the underlying geometry to calculate the target cell faster.

Specified by:
getCellAt in interface Maze
Parameters:
position - cell position
Returns:
cell at position or null if there is no cell at the given position

getInfo

public java.lang.String getInfo(boolean onlyVariable)

getOuterBorder

protected abstract java.awt.Shape getOuterBorder()
Get the borders of the maze as a shape.

Returns:
outer border

getPropertyInformations

public java.util.Collection<PropertyInformation> getPropertyInformations()
Get the property setters and displays for this maze. Overriding classes should include this collection first.

Specified by:
getPropertyInformations in class MazePropertyOwner
Returns:
colletion of property setters

loadSeedWayAndVersion

protected void loadSeedWayAndVersion(DataStorage dataStorage,
                                     java.lang.String prefix)
Load the seed and way points from the given data storage.

Parameters:
dataStorage - data storage to load from
prefix - key prefix unique for each maze

storeSeedWayAndVersion

protected void storeSeedWayAndVersion(DataStorage dataStorage,
                                      java.lang.String prefix)
Store the seed and way points to the given data storage.

Parameters:
dataStorage - data storage to load from
prefix - key prefix unique for each maze

addMazeFinishedListener

public void addMazeFinishedListener(MazeFinishedListener listener)
Add a maze finished listener which is called when the maze (re)creation is finished.

Specified by:
addMazeFinishedListener in interface Maze
Parameters:
listener - listener to add

removeMazeFinishedListener

public void removeMazeFinishedListener(MazeFinishedListener listener)
Remove a maze finished listener which was called when the maze (re)creation is finished.

Specified by:
removeMazeFinishedListener in interface Maze
Parameters:
listener - listener to remove