Rekursive Labyrinthe/ MazeField
Erscheinungsbild
/*
Source file: MazeField.java
Program: Maze Field
Author: Markus Bautsch
Licence: public domain
Date: 23 October 2020
Version: 1.0
Date: 24 April 2026
Version: 1.1 with Javadoc comments
Programming language: Java
*/
/**
This class implements a single maze field.
A maze field is rectangular and can have walls and borders at its edges.
The maze has to be initialised with all walls blocked
and some of these walls can be removed for building a maze.
The outer rim of the maze has to be defined by borders.
Borders are also allowed within the maze to exclude certain fields from the maze.
Borders must have walls.
*/
public class MazeField
{
/** Directions (constants) */
/**
* For number of possible directions
*/
public final static byte NumberOfDirections = 4;
/**
* For undefinded direction
*/
public final static byte NULL = 0; // for undefined direction
/**
* For direction north (upwards)
*/
public final static byte NORTH = 1;
/**
* For direction east (to the right)
*/
public final static byte EAST = 2;
/**
* For direction south (downwards)
*/
public final static byte SOUTH = 3;
/**
* For direction west (to the left)
*/
public final static byte WEST = 4;
/* Borders (cannot be removed) */
private boolean borderN;
private boolean borderE;
private boolean borderS;
private boolean borderW;
/* Walls (can be removed for designing mazes) */
private boolean wallN;
private boolean wallE;
private boolean wallS;
private boolean wallW;
/* Location of maze field and previous field for internal reference (recursion and backtracking) */
private int locationX;
private int locationY;
private MazeField previousField;
private MazeField nextField;
/**
* Constructor for the initialisation of instances of the class MazeFields
* All initialised MazeFields have four walls, but no borders
* @param locationX position of maze field in x-direction
* @param locationY position of maze field in y-direction
*/
public MazeField (int locationX, int locationY)
{
this.removeAllBorders ();
this.setAllWalls ();
this.locationX = locationX;
this.locationY = locationY;
this.previousField = null;
this.nextField = null;
}
/** Checks all four borders. If there is a border there has to be a wall
* @return true if all four borders are correct, false if there is at least one wall without border
* */
public boolean checkBorders ()
{
boolean valid = true;
if (this.borderN && !this.wallN)
{
valid = false;
}
else if (this.borderE && !this.wallE)
{
valid = false;
}
else if (this.borderS && !this.wallS)
{
valid = false;
}
else if (this.borderW && !this.wallW)
{
valid = false;
}
return valid;
}
/** Get attribute loacationX
* @return position of maze field in x-direction
* */
public int getLocationX ()
{
return this.locationX;
}
/** Get attribute loacationY
* @return position of maze field in y-direction
* */
public int getLocationY ()
{
return this.locationY;
}
/** Set attribute previousField
* @param field for maze field that shall be the predecessor of a maze field
* */
public void setPreviousField (MazeField field)
{
this.previousField = field;
}
/** Get attribute previousField
* @return maze field that is the predecessor of a maze field
* */
public MazeField getPreviousField ()
{
return this.previousField;
}
/** Set attribute nextField
* @param field for maze field that shall be the successor of a maze field
* */
public void setNextField (MazeField field)
{
this.nextField = field;
}
/** Get attribute nextField
* @return maze field that is the successor of a maze field
* */
public MazeField getNextField ()
{
return this.nextField;
}
/** Treat border attributes N, E, S and W */
/** Set north border */
public void setBorderN ()
{
this.borderN = true;
}
/** Remove north border */
public void removeBorderN ()
{
this.borderN = false;
}
/** Check north border
* @return true, if there is a north border
* */
public boolean hasBorderN ()
{
return this.borderN;
}
/** Set east border */
public void setBorderE ()
{
this.borderE = true;
}
/** Remove east border */
public void removeBorderE ()
{
this.borderE = false;
}
/** Check east border
* @return true, if there is a east border
* */
public boolean hasBorderE ()
{
return this.borderE;
}
/** Set south border */
public void setBorderS ()
{
this.borderS = true;
}
/** Remove south border */
public void removeBorderS ()
{
this.borderS = false;
}
/** Check south border
* @return true, if there is a south border
* */
public boolean hasBorderS ()
{
return this.borderS;
}
/** Set west border */
public void setBorderW ()
{
this.borderW = true;
}
/** Remove west border */
public void removeBorderW ()
{
this.borderW = false;
}
/** Check west border
* @return true, if there is a west border
* */
public boolean hasBorderW ()
{
return this.borderW;
}
/** Set all four borders */
public void setAllBorders ()
{
this.setBorderN ();
this.setBorderE ();
this.setBorderS ();
this.setBorderW ();
}
/** Remove all four borders */
public void removeAllBorders ()
{
this.removeBorderN ();
this.removeBorderE ();
this.removeBorderS ();
this.removeBorderW ();
}
/** This method checks, whether all four borders are set
* @return true only if all borders are set
* */
public boolean hasAllBorders ()
{
return this.hasBorderN () && this.hasBorderE () && this.hasBorderS () && this.hasBorderW ();
}
/** Treat wall attributes N, E, S and W */
/** Set north wall */
public void setWallN ()
{
this.wallN = true;
}
/** Remove north wall */
public void removeWallN ()
{
this.wallN = false;
}
/** Check north wall
* @return true, if there is a north wall
* */
public boolean hasWallN ()
{
return this.wallN;
}
/** Set east wall */
public void setWallE ()
{
this.wallE = true;
}
/** Remove east wall */
public void removeWallE ()
{
this.wallE = false;
}
/** Check east wall
* @return true, if there is a east wall
* */
public boolean hasWallE ()
{
return this.wallE;
}
/** Set south wall */
public void setWallS ()
{
this.wallS = true;
}
/** Remove south wall */
public void removeWallS ()
{
this.wallS = false;
}
/** Check south wall
* @return true, if there is a south wall
* */
public boolean hasWallS ()
{
return this.wallS;
}
/** Set west wall */
public void setWallW ()
{
this.wallW = true;
}
/** Remove west wall */
public void removeWallW ()
{
this.wallW = false;
}
/** Check west wall
* @return true, if there is a west wall
* */
public boolean hasWallW ()
{
return this.wallW;
}
/** Set all four walls */
public void setAllWalls ()
{
this.setWallN ();
this.setWallE ();
this.setWallS ();
this.setWallW ();
}
/** This method checks, whether all four walls are set
* @return true only if all walls are set
* */
public boolean hasAllWalls ()
{
return this.hasWallN () && this.hasWallE () && this.hasWallS () && this.hasWallW ();
}
}