public class

Maze2D

extends Object
java.lang.Object
   ↳ es.usc.citius.hipster.util.examples.maze.Maze2D

Class Overview

This class defines a 2D ASCII Maze used to easily validate the search algorithms. Example usage:

     public static String[] example = new String[]{
                  "XXSXXXXXXXXXXXXXXXXXXXXXXXXXXXXX",
                  "XX XXXXXXXXXXXXX     XXXXXXXXXXX",
                  "XX    XXXXXXXXXX XXX XX     XXXX",
                  "XXXXX  XXXXXX    XXX XX XXX XXXX",
                  "XXX XX XXXXXX XX XXX XX  XX XXXX",
                  "XXX     XXXXX XXXXXX XXXXXX XXXX",
                  "XXXXXXX       XXXXXX        XXXX",
                  "XXXXXXXXXX XXXXX XXXXXXXXXXXXXXX",
                  "XXXXXXXXXX XX    XXXXX      XXXX",
                  "XXXXXXXXXX    XXXXXXXX XXXX XXXX",
                  "XXXXXXXXXXX XXXXXXXXXX XXXX XXXX",
                  "XXXXXXXXXXX            XXXX XXXX",
                  "XXXXXXXXXXXXXXXXXXXXXXXX XX XXXX",
                  "XXXXXX              XXXX XX XXXX",
                  "XXXXXX XXXXXXXXXXXX XX      XXXX",
                  "XXXXXX XXG   XXXXXX XXXX XXXXXXX",
                  "XXXXXX XXXXX   XXX            XX",
                  "XXXXXX XXXXXXX XXXXXXXXXXX XXXXX",
                  "XXXXXX XXXXXXX XXXXXXXXXXXXXXXXX",
                  "XXXXXX            XXXXXXXXXXXXXX";

            Maze2D maze = new Maze2D(example);
     }
 
Symbol connected used by Maze2D:
  • "X": occupied tile
  • " ": empty tile
  • "S": initial state (starting point)
  • "G": goal state
  • ".": visited tile

Summary

Nested Classes
enum Maze2D.Symbol Symbols allowed to create a maze  
Public Constructors
Maze2D(char[][] maze)
Creates a new 2D ASCII Maze.
Maze2D(String[] maze2D)
Creates a new 2D ASCII Maze from a array of Strings.
Public Methods
Set<Point> diff(Maze2D to)
Returns a set of points that are different with respect this maze.
static Maze2D empty(int size)
Generate an empty squared maze of the indicated size.
Point getGoalLoc()
Get the goal point (the point with the symbol GOAL) in this maze.
Point getInitialLoc()
Get the initial point (the point with the symbol START) in this maze.
char[][] getMaze()
Get this maze as a byte array.
char[][] getMazeCharArray()
List<Point> getMazePoints()
Return all tiles (i,j) of the maze
String getReplacedMazeString(List<Map<Point, Character>> replacements)
Generates a string representation of this maze but replacing all the indicated points with the characters provided.
String getStringMazeFilled(Collection<Point> points, char symbol)
Generates a string representation of this maze, with the indicated points replaced with the symbol provided.
boolean isFree(Point p)
Check if the point p in the maze is empty or occupied.
boolean pointInBounds(Point loc)
Check if the provided point is in the maze bounds or outside.
void putObstacle(Point p)
Puts a OCCUPIED in the indicated point.
void putObstacleRectangle(Point a, Point b)
Fill a rectangle defined by points a and b with occupied tiles.
static Maze2D read(File file)
Read a maze from a file in plain text.
void removeObstacle(Point p)
Puts a EMPTY character in the indicated point.
void removeObstacleRectangle(Point a, Point b)
Fill a rectangle defined by points a and b with empty tiles.
String toString()
String[] toStringArray()
void updateLocation(Point p, Maze2D.Symbol symbol)
Replace a tile in the maze
void updateRectangle(Point a, Point b, Maze2D.Symbol symbol)
Replace all tiles inside the rectangle with the provided symbol.
boolean validLocation(Point loc)
Calculates whether a location is empty or not.
Collection<Point> validLocationsFrom(Point loc)
Return all neighbor empty points from a specific location point.
[Expand]
Inherited Methods
From class java.lang.Object

Public Constructors

public Maze2D (char[][] maze)

Creates a new 2D ASCII Maze.

Parameters
maze 2D Byte array of that represents the maze using Maze2D.Symbol

public Maze2D (String[] maze2D)

Creates a new 2D ASCII Maze from a array of Strings.

Parameters
maze2D Array of strings representing the maze. Use symbols from Symbol
Throws
IllegalFormatException

Public Methods

public Set<Point> diff (Maze2D to)

Returns a set of points that are different with respect this maze. Both mazes must have same size.

Parameters
to maze to be compared.
Returns
  • set of different points.

public static Maze2D empty (int size)

Generate an empty squared maze of the indicated size.

Parameters
size maze size.
Returns
  • empty maze.

public Point getGoalLoc ()

Get the goal point (the point with the symbol GOAL) in this maze.

public Point getInitialLoc ()

Get the initial point (the point with the symbol START) in this maze.

public char[][] getMaze ()

Get this maze as a byte array.

public char[][] getMazeCharArray ()

public List<Point> getMazePoints ()

Return all tiles (i,j) of the maze

Returns
  • List of points, from (0,0) to (m,n).

public String getReplacedMazeString (List<Map<Point, Character>> replacements)

Generates a string representation of this maze but replacing all the indicated points with the characters provided.

Parameters
replacements list with maps of point-character replacements.
Returns
  • String representation of the maze with the replacements.

public String getStringMazeFilled (Collection<Point> points, char symbol)

Generates a string representation of this maze, with the indicated points replaced with the symbol provided.

Parameters
points points of the maze.
symbol symbol to be inserted in each point.
Returns
  • the string representation of the maze with the points changed.

public boolean isFree (Point p)

Check if the point p in the maze is empty or occupied.

Parameters
p Point to check
Returns
  • True if is free, false if is not empty.

public boolean pointInBounds (Point loc)

Check if the provided point is in the maze bounds or outside.

Parameters
loc point to be tested.
Returns
  • true if the point is in the maze.

public void putObstacle (Point p)

Puts a OCCUPIED in the indicated point.

Parameters
p point to put an obstacle in.

public void putObstacleRectangle (Point a, Point b)

Fill a rectangle defined by points a and b with occupied tiles.

Parameters
a point a of the rectangle.
b point b of the rectangle.

public static Maze2D read (File file)

Read a maze from a file in plain text.

Parameters
file file with the plain ascii text.
Returns
  • a new Maze2D.
Throws
IOException

public void removeObstacle (Point p)

Puts a EMPTY character in the indicated point.

Parameters
p point to be free.

public void removeObstacleRectangle (Point a, Point b)

Fill a rectangle defined by points a and b with empty tiles.

Parameters
a point a of the rectangle.
b point b of the rectangle.

public String toString ()

public String[] toStringArray ()

public void updateLocation (Point p, Maze2D.Symbol symbol)

Replace a tile in the maze

Parameters
p Point of the tile to be replaced
symbol New symbol

public void updateRectangle (Point a, Point b, Maze2D.Symbol symbol)

Replace all tiles inside the rectangle with the provided symbol.

Parameters
a point a of the rectangle.
b point b of the rectangle.
symbol symbol to be inserted in each tile.

public boolean validLocation (Point loc)

Calculates whether a location is empty or not.

Parameters
loc point location to be tested.
Returns
  • true if point is filled with EMPTY. False otherwise.

public Collection<Point> validLocationsFrom (Point loc)

Return all neighbor empty points from a specific location point.

Parameters
loc source point
Returns
  • collection of empty neighbor points.