Rmaze
is a package that provides options to generate and plot mazes in R.
Currently, the package provides options to create a maze using the recursive backtracker (modifed depth-first search), Kruskal's, and Prim's algorithms. These algorithms create "perfect" mazes, i.e., mazes without loops and without inaccessible areas. Such mazes have exactly one solution (there is exactly one path from each maze cell to another maze cell).
All mazes are represented in a form of connected graphs, where the edges represent possible wall sites and the nodes represent maze cells. All graph manipulations are based on the functions provided in the igraph package.
To install the Rmaze
package, run the following command:
library("devtools") install_github("Vessy/Rmaze")
To load the Rmaze
package, run:
library("Rmaze")
To run a Shiny app showcasing the basic Rmaze
features, run the following command:
Rmaze::runExample()
To report a bug, a problem, or have a suggestion, please raise an issue in the Rmaze
GitHub project.
All mazes are represented in a form of connected graphs, where the edges represent possible wall sites and the nodes represent maze cells. Initial maze will have all walls up.
maze1 <- makeGraph(10, 10)
To see this initial maze, we can plot it:
plotMaze(maze1, 10, 10)
It is just a mash grid right now.
Once we have created an initial maze graph, we can use it to create a real maze.
For example, we can create a maze using the recursive backtracker algorithm:
maze2 <- makeMaze_dfs(maze1) plotMaze(maze2, 10, 10)
Or Kruskal's algorithm
maze3 <- makeMaze_kruskal(maze1) plotMaze(maze3, 10, 10)
Or Prim's algorithm:
maze4 <- makeMaze_prim(maze1) plotMaze(maze4, 10, 10)
Alternatively, we can plot step-by-step maze creation process. This process is slower and is not recommended for larger mazes.
# Recursive backtracker algorithm maze2 <- makeMaze_dfs(maze1, stepBystep = TRUE, nrows=10, ncols=10) #Kruskal's algorithm maze3 <- makeMaze_kruskal(maze1, stepBystep = TRUE, nrows=10, ncols=10) # Prim's algorithm: maze4 <- makeMaze_prim(maze1, stepBystep = TRUE, nrows=10, ncols=10)
White cells are unvisited maze cells, red cells are the maze cells on stack, and the blue cells are the maze cells that have been visited and removed from the stack.
We can also plot a maze solution. The maze solution is defined as the shortest path between the start and end maze cells:
plotMazeSolution(maze2, 10, 10)
Rmaze
provides an option to create imperfect mazes. These mazes are created by randomly adding or removing specified percentages of maze walls from the perfect mazes. In the process of randomization, the algorithm ensures that the added and removed walls do not interfere with the existence of the maze solution. The imperfect mazes contain loops, inaccessible areas, and may have more than one solution.
# Create imperfect maze from the maze2 (the perfect maze created using the recursive backtracker algorithm) maze5 <- makeImperfect(maze2) # Plot the imperfect maze and its solution plotMaze(maze5, 10, 10) plotMazeSolution(maze5, 10, 10)
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.