# Why when generating a maze the grid is not really 10x10 size?

For example in the Inspector i set the Width and Height values to 10. So the grid should be 10x10 But when i'm running the game it's changing the values of the Width and Height in the inspector to 11 both to 11. And then when i look at the scene view and counting the width and height i see they are both 9. Like 9x9 grid, not 10x10 and not 11x11.

Then later if i click the button i created to generate a new maze each time then if i set the width and height both to 5 the grid size will be 4x4 and if i set both to 6x6 the grid size will be 5x5

For some reason the value i enter for width and height he reduce it by one. And i want to make it in the size of the values. For example 10x10 so the grid will be 10x10.

This is a screenshot while the game is running. you can see on the right in the inspector i changed the values to 5 and 5 and clicked the button. But in the scene view you can count the size of the grid: it's 4x4

It's showing only the maze but you can imagine and count how the grid should be and see it's not 5x5 but 4x4

This is another screenshot of 5x5 this time the grid seems like size of 3x3 Maybe i don't understand how it's working and how it should work ?

This is a link for the tutorial i'm trying to make:

Maze tutorial

This is the scripts i'm using so far:

Maze class for all settings and generating:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Maze
{
//Grid size
public int width;
public int height;

//Store grid
private bool[,] grid;
//Generate random directions to move
private System.Random rg;

//Start position
int startX;
int startY;

//Public getter
public bool[,] Grid
{
get { return grid; }
}

//Constructor of the grid for setting values
public Maze(int width, int height, System.Random rg)
{
this.width = width;
this.height = height;

this.rg = rg;
}

//Generate the grid
public void Generate()
{
grid = new bool[width, height];

startX = 1;
startY = 1;

grid[startX, startY] = true;

MazeDigger(startX, startY);
}

void MazeDigger(int x, int y)
{
int[] directions = new int[] { 1, 2, 3, 4 };

//We create random array of directions
HelpingTools.Shuffle(directions, rg);

//We are looping over all the directions
for (int i = 0; i < directions.Length; i++)
{
if (directions[i] == 1)
{
if (y - 2 <= 0)
continue;

if (grid[x, y - 2] == false)
{
grid[x, y - 2] = true;
grid[x, y - 1] = true;

MazeDigger(x, y - 2);
}
}

if (directions[i] == 2)
{
if (x - 2 <= 0)
continue;

if (grid[x - 2, y] == false)
{
grid[x - 2, y] = true;
grid[x - 1, y] = true;

MazeDigger(x - 2, y);
}
}

if (directions[i] == 3)
{
if (x + 2 >= width - 1)
continue;

if (grid[x + 2, y] == false)
{
grid[x + 2, y] = true;
grid[x + 1, y] = true;

MazeDigger(x + 2, y);
}
}

if (directions[i] == 4)
{
if (y + 2 >= height - 1)
continue;

if (grid[x, y + 2] == false)
{
grid[x, y + 2] = true;
grid[x, y + 1] = true;

MazeDigger(x, y + 2);
}
}
}
}
}


This class create random array of the maze:

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class HelpingTools : MonoBehaviour
{
public static T[] Shuffle<T>(T[] array, System.Random rg)
{
for (int i = 0; i < array.Length - 1; i++)
{
int randomIndex = rg.Next(i, array.Length);

T tempItem = array[randomIndex];

array[randomIndex] = array[i];
array[i] = tempItem;
}

return array;
}
}


This is the maze generator class

using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class MazeGenerator : MonoBehaviour
{
public Maze maze;
public int mazeWidth;
public int mazeHeight;
public string mazeSeed;
public GameObject wallPrefab;

private GameObject wall;
private GameObject wallCorner;
private System.Random mazeRG;
private GameObject[] bricks;

// Use this for initialization
void Start ()
{
mazeRG = new System.Random();

if (mazeWidth % 2 == 0)
mazeWidth++;

if (mazeHeight % 2 == 0)
{
mazeHeight++;
}

maze = new Maze(mazeWidth, mazeHeight, mazeRG);
GenerateMaze();
}

public void GenerateMaze()
{
bricks = GameObject.FindGameObjectsWithTag("MazeBrick");
if (bricks.Length > 0)
DestroyMaze();

maze.Generate();
DrawMaze();
}

private void DestroyMaze()
{
for(int i = 0; i < bricks.Length; i++)
{
DestroyImmediate(bricks[i]);
}
}

void DrawMaze()
{
for (int x = 0; x < mazeWidth; x++)
{
for (int y = 0; y < mazeHeight; y++)
{
Vector3 position = new Vector3(x, 0.5f, y);

if (maze.Grid[x, y] == true)
{
CreateMaze(position, transform, 0, mazeRG.Next(0, 3) * 90);
}
}
}
}

void CreateMaze(Vector3 position, Transform parent, int sortingOrder, float rotation)
{
GameObject mazePrefab = Instantiate(wallPrefab, position, Quaternion.identity);
mazePrefab.transform.SetParent(parent);
mazePrefab.transform.Rotate(0, 0, rotation);
mazePrefab.tag = "MazeBrick";
}

// Update is called once per frame
void Update () {

}
}


This is the button script@

using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

[CustomEditor(typeof(MazeGenerator))]
public class GenerateButton : Editor
{
public override void OnInspectorGUI()
{
DrawDefaultInspector();

MazeGenerator myScript = (MazeGenerator)target;
if (GUILayout.Button("Generate Maze"))
{
myScript.GenerateMaze();
}
}
}


My problem is that the grid size not fit to the result when running the game. The Maze is fine but the grid not seems to be 10x10 or 5x5 or 6x6

If for example i wanted to draw the completed grid not only the random maze then how can i do it and what would be the size ?

Again maybe i didn't understand how it is working and should be working.

• Looking it over, I don't see anything different than the tutorial. My best guess this late at night is that it's a "flaw" in the algorithm. It looks to me like it enforces an odd dimension maze, and looks like it won't continue to generate if it can't move the full two spaces each iteration. On a 5x5 it moves up, then right 2 would take it out of bounds, so it stops instead of going right 1. This makes the final grid look one dimension short. Like I said, I'm very tired so I may be missing something. – Stephan Dec 17 '17 at 5:51
• Verify your prefabs are the correct size without any scaling. Also ensure their parent doesn't have any scaling. – MichaelHouse Dec 18 '17 at 17:10

At a glance, it looks like each cell is either open or a wall (as opposed to maze algorithms that treat walls as lines rather than volumes). Some algorithms that do so further assume that outer ring of cells must be a wall (though some later dig an entrance & exit). It looks as if this algorithm does something similar. for instance, in the code for Start() you have:

if (mazeWidth % 2 == 0)
mazeWidth++;

if (mazeHeight % 2 == 0)
{
mazeHeight++;
}


This code checks to see if either dimension is even & if so, will increment said dimension to the next largest integer. (Side note: you should be consistent with your use of { } braces & if statements. either brace all single line ifs or none - don't do some, but not others. Personally, I recommend them on all if statements, single line or otherwise.)

So that's why your size doesn't always match your parameters. But your maze digging logic might also depend on getting an odd size, so fixing this might not be as simple as just changing the code in Start. For instance, there are several conditionals in MazeDigger that are checking the indices relative to the dimensions.