java.lang.Object
net.regsirius06.engine.core.graphics.labyrinth.default2D.Algorithm

public final class Algorithm extends Object
This class provides a set of algorithms for generating labyrinths. It contains methods for transforming grid dimensions, calculating indices, and generating labyrinths using different algorithms such as Sirius' and Eller's.

The algorithms create labyrinths as a list of Wall objects, representing walls, empty spaces, and boundaries. It also includes helper methods for transforming 2D grid coordinates into 1D indices and vice versa.

  • Method Summary

    Modifier and Type
    Method
    Description
    static @NotNull List<Wall>
    EllerGenLabyrinth(int n, long seed)
    Generates a labyrinth using Eller's algorithm.
    static int
    getIndex(int x, int y, int dimension)
    Converts 2D grid coordinates (x, y) to a 1D index based on the given dimension.
    static @NotNull Point
    getPoint(int index, int dimension)
    Converts a 1D index into 2D coordinates based on the given dimension.
    static @NotNull List<Wall>
    SiriusGenLabyrinth(int n, long seed)
    Generates a labyrinth using Sirius' algorithm.
    static int
    Transforms the input dimension to account for the walls of the labyrinth.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • transformDimension

      public static int transformDimension(int n)
      Transforms the input dimension to account for the walls of the labyrinth. The labyrinth grid is represented as a square, where each dimension is increased by 1 to include walls around the maze.
      Parameters:
      n - the size of the labyrinth without walls (number of cells in one row/column)
      Returns:
      the transformed dimension, which is equal to 2 * n + 1
    • getIndex

      public static int getIndex(int x, int y, int dimension)
      Converts 2D grid coordinates (x, y) to a 1D index based on the given dimension. This is useful for accessing elements in a list representation of the labyrinth.
      Parameters:
      x - the row coordinate
      y - the column coordinate
      dimension - the total dimension of the grid (including walls)
      Returns:
      the 1D index corresponding to the 2D coordinates
    • getPoint

      @Contract("_, _ -> new") @NotNull public static @NotNull Point getPoint(int index, int dimension)
      Converts a 1D index into 2D coordinates based on the given dimension. This is the reverse operation of getIndex(int, int, int).
      Parameters:
      index - the 1D index
      dimension - the total dimension of the grid (including walls)
      Returns:
      a Point object representing the 2D coordinates of the index
    • SiriusGenLabyrinth

      @NotNull public static @NotNull List<Wall> SiriusGenLabyrinth(int n, long seed)
      Generates a labyrinth using Sirius' algorithm. This algorithm randomly places walls and open spaces within the grid, creating a maze. The generated labyrinth includes boundary walls and random placements of empty spaces and walls.
      Parameters:
      n - the size of the labyrinth (number of cells in one row/column)
      seed - a seed for random number generation
      Returns:
      a list of Wall objects representing the generated labyrinth
    • EllerGenLabyrinth

      @NotNull public static @NotNull List<Wall> EllerGenLabyrinth(int n, long seed)
      Generates a labyrinth using Eller's algorithm. This algorithm is more complex and uses a union-find data structure to progressively connect sets of cells, eventually creating a maze. The algorithm generates a maze by connecting cells row by row, then linking them vertically while ensuring there are no loops.
      Parameters:
      n - the size of the labyrinth (number of cells in one row/column)
      seed - a seed for random number generation
      Returns:
      a list of Wall objects representing the generated labyrinth