This is nothing special, just for my own fun.
The bulk of the logic is in the github.com/yalue/maze
go package. It can be
used as a library:
import (
"github.com/yalue/maze"
"image/png"
"os"
)
func main() {
// Generates a new maze, taking a width and height in cells.
m, _ := maze.NewGridMaze(60, 40)
// The returned maze satisfies the image.Image interface; save it to a
// file.
f, _ := os.Create("maze.png")
png.Encode(f, m)
f.Close()
}
Alternatively, there is a command-line tool in the create_maze_image
subdirectory:
cd create_maze_image
go build .
# View the command-line options required by the tool:
./create_maze_image -help
If you want to make a maze with a more interesting shape than a basic
rectangle, you can use a "template" image. In template images, each pixel
corresponds to a single cell in the generated maze. Black pixels will be
"excluded" from the final maze, the starting location will be chosen
randomly from among green (RGB 0, 255, 0) pixels, the ending location
will be randomly chosen from among red (RGB 255, 0, 0) pixels, and all other
pixels will be "normal" cells for maze pathways. The
create_maze_image/sample_template.png
image serves as an example. You can
create a random maze based on this template using
./create_mage_image -template_image ./sample_template.png <other options>
.
Note that you can still use random seeds or generate solutions for mazes
generated by templates (so long as the template supports generating a solvable
maze!).