Rekursive Labyrinthe/ MazeField

Aus Wikibooks
/*
  Source file: MazeField.java
  Program: Maze Field
  Author: Markus Bautsch
  Licence: public domain
  Date: 23 October 2020
  Version: 1.0
  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) */
	public final static byte NumberOfDirections = 4;
	public final static byte NULL = 0; // for undefined direction
	public final static byte NORTH = 1;
	public final static byte EAST = 2;
	public final static byte SOUTH = 3;
	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 all initialised MazeFields have four walls, but no borders */
	public MazeField (int locationX, int locationY)
	{
		this.removeAllBorders ();
		this.setAllWalls ();

		this.locationX = locationX;
		this.locationY = locationY;

		this.previousField = null;
		this.nextField = null;
	}

	/* If there is a border there has to be a wall */
	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 */
	public int getLocationX ()
	{
		return this.locationX;
	}

	/* Get attribute locationY */
	public int getLocationY ()
	{
		return this.locationY;
	}

	/* Set attribute previousField */
	public void setPreviousField (MazeField field)
	{
		this.previousField = field;
	}

	/* Get attribute previousField */
	public MazeField getPreviousField ()
	{
		return this.previousField;
	}

	/* Set attribute nextField */
	public void setNextField (MazeField field)
	{
		this.nextField = field;
	}

	/* Get attribute nextField */
	public MazeField getNextField ()
	{
		return this.nextField;
	}

	/* Treat border attributes N, E, S and W */
	public void setBorderN ()
	{
		this.borderN = true;
	}

	public void removeBorderN ()
	{
		this.borderN = false;
	}

	public boolean hasBorderN ()
	{
		return this.borderN;
	}

	public void setBorderE ()
	{
		this.borderE = true;
	}

	public void removeBorderE ()
	{
		this.borderE = false;
	}

	public boolean hasBorderE ()
	{
		return this.borderE;
	}

	public void setBorderS ()
	{
		this.borderS = true;
	}

	public void removeBorderS ()
	{
		this.borderS = false;
	}

	public boolean hasBorderS ()
	{
		return this.borderS;
	}

	public void setBorderW ()
	{
		this.borderW = true;
	}

	public void removeBorderW ()
	{
		this.borderW = false;
	}

	public boolean hasBorderW ()
	{
		return this.borderW;
	}

	public void removeAllBorders ()
	{
		this.removeBorderN ();
		this.removeBorderE ();
		this.removeBorderS ();
		this.removeBorderW ();
	}

	public void setAllBorders ()
	{
		this.setBorderN ();
		this.setBorderE ();
		this.setBorderS ();
		this.setBorderW ();
	}

	/* Treat wall attributes N, E, S and W */
	public void setWallN ()
	{
		this.wallN = true;
	}

	public void removeWallN ()
	{
		this.wallN = false;
	}

	public boolean hasWallN ()
	{
		return this.wallN;
	}

	public void setWallE ()
	{
		this.wallE = true;
	}

	public void removeWallE ()
	{
		this.wallE = false;
	}

	public boolean hasWallE ()
	{
		return this.wallE;
	}

	public void setWallS ()
	{
		this.wallS = true;
	}

	public void removeWallS ()
	{
		this.wallS = false;
	}

	public boolean hasWallS ()
	{
		return this.wallS;
	}

	public void setWallW ()
	{
		this.wallW = true;
	}

	public void removeWallW ()
	{
		this.wallW = false;
	}

	public boolean hasWallW ()
	{
		return this.wallW;
	}

	public void setAllWalls ()
	{
		this.setWallN ();
		this.setWallE ();
		this.setWallS ();
		this.setWallW ();
	}

	/* This method returns true only if all walls are set */
	public boolean hasAllWalls ()
	{
		return this.hasWallN () && this.hasWallE () && this.hasWallS () && this.hasWallW ();
	}

	/* This method returns true only if all borders are set */
	public boolean hasAllBorders ()
	{
		return this.hasBorderN () && this.hasBorderE () && this.hasBorderS () && this.hasBorderW ();
	}
}