Maze Generator
Build a command-line program in C that generates a random maze and prints it with ASCII characters. You choose the size, for example ./maze 15 10 for a 15×10 maze, and the program carves a maze in which every cell can be reached, with an entrance at the top left and an exit at the bottom right. Passing the same --seed value always produces the same maze. It uses the depth-first search algorithm (recursive backtracking), one of the classic ways to generate mazes. You'll practice 2D arrays, structs, random numbers, a stack, command-line arguments, and building a C project with Make and CMake.
- Estimate
- ~9h
- Steps
- 6
- Completed by
- 0
- Proposed by
- codeseed.app
Make · POSIX · CMake
Project roadmap
- 01
Set up the project and build files
~1hCreate src/main.c with a main that prints Hello. Add a Makefile that compiles with -Wall -Wextra -std=c11 and has build and clean targets, plus a simple CMakeLists.txt that builds the same program, so both make and cmake work. Done when: make builds and runs the program, and cmake -B build followed by cmake --build build also works.
- 02
Read the arguments
~1hRead the width and height from the command line with strtol, both between 2 and 50, and an optional --seed number. If no seed is given, use the current time. Print a usage message and exit with code 1 for missing or invalid values. Done when: ./maze 15 10 is accepted and ./maze abc 10 prints the usage.
- 03
Model the grid
~1.5hDefine a Cell struct with four walls (top, right, bottom, left) and a visited flag, and allocate a width × height grid with malloc. Start with every wall standing. Write a function that removes the wall between two neighboring cells on both sides. Done when: the grid can be created, freed, and have walls removed between neighbors.
- 04
Carve the maze with depth-first search
~2.5hImplement recursive backtracking with your own stack array instead of recursion: start at the top-left cell and mark it visited. While the stack isn't empty, look at the current cell's unvisited neighbors; if there are any, pick one at random with rand(), remove the wall between them, mark it and push it. Otherwise, pop. Call srand(seed) once at the start. Done when: every cell is visited, so the maze is fully connected.
- 05
Print the maze in ASCII
~1.5hPrint the maze using + for corners, --- for horizontal walls, | for vertical walls and spaces for open paths. Open the outer wall at the top-left cell for the entrance and at the bottom-right cell for the exit. Done when: ./maze 5 5 --seed 42 prints a readable maze, and running it twice with the same seed prints the same maze.
- 06
Test it
~1.5hAdd a make test target that runs the program with fixed seeds and compares the output with saved expected files in tests/. Also add a check that every cell is reachable from the entrance, using a simple flood fill over the grid. Done when: make test passes.
Resources
Ready to build this?
Get a GitHub repo and start building. Your AI reviewer checks each step as you go.
Tech stack