How to make a Maze Generator
Tags: programming, learning, cpp, beginners
Published: 2018-06-04 21:28:00

---
title: How to make a Maze Generator
published: true
description: How to make a Maze Generator in C++
tags: #programming #learning #cpp #beginners 
---

![Screenshot](https://raw.githubusercontent.com/alinp25/alinp25.github.io/master/assets/images/mazegeneratortutorial/screenshot.png)

What are we going to make? A maze generator. How? Let's see.

---

### What do we need?

We're going to make the project in C++, using [SDL Library](https://www.libsdl.org/) for graphics. They have a really nice tutorial on setting up SDL, which is really easy and fast, [right here](https://wiki.libsdl.org/Installation).

**Let's code!**

First of all, what do we understand by maze? Hmmm... Let's take a look at the dictionary: *a confusing network of intercommunicating paths or passages*. I don't like how it sounds, let's make it simpler: a collection of rooms that have or have not a connection. So, how are we going to structure this? In a normal way, it can be structured as a graph, but for the simplicity of this problem, we're gonna make it as an NxN grid.

Now comes the fun part, where the thinking is happening... What should contain every cell from the grid? Let's call it a room. What does a normal room? Walls. So, theoretically, our connections between the rooms, are represented by walls. ![Room from maze](/assets/images/mazegeneratortutorial/roommaze.png).  

Now, how do we connect 2 rooms? By removing the wall between them. And how we can do that? By removing two walls. Now here comes the question "Wait, between two rooms we have two walls". Theoretically no, but in this model, yes, because when you remove the left wall of a room, you have to remove the right wall of the room situated at it's left. Kind of confusing, but this image will most probably make it clearer.

![Wall Diagram](https://github.com/alinp25/alinp25.github.io/raw/master/assets/images/mazegeneratortutorial/roommaze.png)

### How does the algorithm look like?

The algorithm is based on a backtrack solution (Also, you can find Kruskal and Prim's version on randomizing a maze) and it works like this:
1. Pick one room, make it the starting point and mark it as visited
2. While there are unvisited rooms  
   1. If the current cell has any neighbors which have not been visited  
      1. Choose one random neighbor that hasn't been visited yet
      2. Push the current room to the stack
      3. Remove the wall between the current room and the next stack (bye bye walls, welcome roads)
      4. Make the current cell *the chosen one* and mark it as visited
   2. Else If there are no unvisited neighbors and stack is not empty  
      1. Pop a cell from the stack
      2. Make it the current cell

![Algorithm](https://github.com/alinp25/alinp25.github.io/raw/master/assets/images/mazegeneratortutorial/walldiagram.png)

It's a very simple algorithm, that can make some very interesting visual effects and be helpful in some situations... And with this, my childhood thinking that mazes from books were manually designed is ruined. You can check it out [here](https://github.com/alinp25/MazeGenerator) and [see it in action here](https://www.youtube.com/watch?v=qKDWKDohnBw).

###### You can find the original article on [MY BLOG](https://alinp25.github.io/).