Creating mazes with Depth-First Search
We will start our journey with PCG by using a search algorithm as a starting point for creating mazes. We will use the foundations acquired in Chapter 2, Navigation, and twist them a little, to create mazes.
Getting ready
For the graph, we will use the grid representation, and Boolean values to define whether a cell is a wall or not, respectively.
How to do it...
We will comprise everything in just a class that will handle the abstract representation, called DSFDungeon
:
- Define the
DFSDungeon
component and its member values:
using UnityEngine; using System.Collections.Generic; public class DFSDungeon : MonoBehaviour { public int width; public int height; public bool[,] dungeon; public bool[,] visited; private Stack<Vector2> stack; private Vector2 current; private int size; // next steps }
- Define the initialization function:
private void Init() { // next steps }
- Initialize the required variables and random initial position:
stack =...