Maze Game
A desktop game, written in Java with LibGDX, that generates square or hexagonal mazes for you to solve, using recursive backtracking or Aldous-Broder. I was the sole programmer.
- Java
- LibGDX
- Scene2D
- Gradle
Overview
Maze Game generates a maze, puts a player in it, and lets you try to solve it. You can choose square mazes or hexagonal ones. It’s a desktop game written in Java, using the LibGDX framework for graphics and input and Scene2D for the interface. I was the only programmer.
What it does
- Generates square mazes, controlled with
W,A,SandD. - Generates hexagonal mazes, where each cell has six neighbors and movement uses six keys instead of four.
- Press
Rto generate a new maze, orHto open a settings panel where you can pick the tile shape, the generation algorithm and the size of the maze.
A square maze, with the player shown as the red marker:
A hexagonal maze:
How the mazes are generated
The game has two maze generators, and you can switch between them in the settings panel. The default is recursive backtracking, a form of depth-first search. It starts in a random cell and carves a path by stepping to a random neighbor it hasn’t visited yet. When it reaches a cell with no unvisited neighbors, it backs up along its path until it finds one that does, and carries on from there. It stops once every cell has been visited. I wrote it with an explicit stack instead of recursion, so large mazes can’t overflow the call stack. The second generator is Aldous-Broder, which wanders randomly and carves a passage whenever it steps into a cell for the first time. The two produce mazes that feel quite different, which is a good reason to have both.
The part I’m proudest of is how the code is arranged. An abstract Maze class knows how to carve a passage, ask for a cell’s neighbors and pick an unvisited one, but nothing about shapes. MazeSquare and MazeHexagon only fill in the geometry. Each algorithm is written once, as a generic method over any kind of maze, so square and hexagonal mazes share exactly the same generation code. The hexagon class is where most of the shape-specific work lives: six neighbors per cell, with different offsets on even and odd rows, and a mapping between opposite walls so that carving through one cell opens the matching wall in the next.
Every maze gets an entrance and an exit on opposite edges, so there is always a way through.
What it involved
This is a complete project rather than a single script: a Gradle build, the LibGDX framework and the Scene2D UI library working together. The code is public and MIT licensed, and the README lists what you need to run it: Java 8 or higher and an IDE that can import Gradle projects. The project is archived and no longer maintained.