How to create a random maze

I currently have code that opens up all of the doors on my prefabs for rooms/tiles that are not on the edges of my randomly generated map. I would like to modify this code so that not all of the doors are open and it is more of a randomly generated maze. I want every room to be accessible and it is possible to have more than one path through the maze/game world. How can I change my code so it works in this matter? Below is my current code that I want to modify.

    using UnityEngine;
   
    public class Room : MonoBehaviour {
   
        public GameObject doorNorth;
        public GameObject doorSouth;
        public GameObject doorEast;
        public GameObject doorWest;
    }
   
    using UnityEngine;
    using System;
    public class mapGenerator : MonoBehaviour {
        public int rows;
        public int cols;
        public GameObject[] gridPrefabs;
        private float roomWidth = 50.0f;
        private float roomHeight = 50.0f;
        public Room[,] grid;//used to keep track of rooms created ,uses two numbers to refer to it in memory
        public bool isMapOfDay;
        public bool isRandomMap;
        public int chosenSeed;
   
   
        // Use this for initialization
        void Start () {
            chosenSeed = GameManager.instance.mapSeed;
            rows = GameManager.instance.mapRows;
            cols = GameManager.instance.mapColumns;
            isMapOfDay = GameManager.instance.useMapOfDay;
            isRandomMap = GameManager.instance.useRandomMap;
            gridPrefabs = GameManager.instance.mapTiles;
   
        }
   
   
        public GameObject RandomRoomPrefab()//Returns a random room
        { 
            return gridPrefabs [UnityEngine.Random.Range (0, gridPrefabs.Length)];     
        }
        public void GenerateGrid()//used to generate map grid
        {
   
            if (isRandomMap == true && isMapOfDay == false) {//sets map to random map based on time
                UnityEngine.Random.InitState(DateToInt(DateTime.Now));//sets "random" seed to current time
            } else if (isRandomMap == false && isMapOfDay == true) {//sets map to map of day based on numbers in day
                UnityEngine.Random.InitState(DateToInt (DateTime.Now.Date));
            } else {//if both are selected just use random map
                UnityEngine.Random.InitState(DateToInt (DateTime.Now));
            }
            if (chosenSeed != 0) {//if a specific seed is entered in game manager use this instead
                UnityEngine.Random.InitState(chosenSeed);
            }
            //Clear out the grid
            grid = new Room[cols, rows];
            GameManager.instance.mapGrid = grid;
            //For each grid row...
            for (int i=0; i<rows; i++)
            {
                //for each column in that row
                for (int j=0; j<cols; j++)
                {
                    //Figure out the location
                    float xPosition = roomWidth * j;
                    float zPosition = roomHeight * i;
                    Vector3 newPosition = new Vector3 (xPosition, 0.0f, zPosition);
                    //create a new grid at appropiate location
                    GameObject tempRoomObj = Instantiate (RandomRoomPrefab (), newPosition, Quaternion.identity)as GameObject;
                    //set its parent
                    tempRoomObj.transform.parent = this.transform;
                    //give the temp room a meaningful name
                    tempRoomObj.name = "Room_" + j + "," + i;
                    //Get the room object
                    Room tempRoom = tempRoomObj.GetComponent<Room> ();
                    //open doors as needed
                    if (i == 0) {
                        //open north doors if on bottom row
                        tempRoom.doorNorth.SetActive (false);
                    } else if (i == rows - 1) {
                        //Otherwise, if doors are on the top row open south doors
                        tempRoom.doorSouth.SetActive (false);
                    } else {
                        //otherwise, this row is in the middle so both north and south open
                        tempRoom.doorNorth.SetActive (false);
                        tempRoom.doorSouth.SetActive (false);
                    }
                    if (j == 0) {
                        //if first column then east doors are opened
                        tempRoom.doorEast.SetActive (false);
                    } else if (j == cols - 1) {
                        //Otheriwse, if one last column row open west doors
                        tempRoom.doorWest.SetActive (false);
                    } else {
                        //otherwise, we are in middle so both west and east are opened
                        tempRoom.doorEast.SetActive (false);
                        tempRoom.doorWest.SetActive (false);
                    }
   
                    //save it to the grid array
                    grid [j, i] = tempRoom;//
                    GameManager.instance.mapGrid=grid;
                }
            }
   
        }
        public int DateToInt(DateTime dateToUse)//adds date and time up and returns it as an int
        {
            int dateToReturn = dateToUse.Year + dateToUse.Month + dateToUse.Day + dateToUse.Hour +dateToUse.Minute + dateToUse.Second + dateToUse.Millisecond;
            return dateToReturn;
        }
        public void clear()//clears grid
        {
            for (int c=0; c<GameManager.instance.mapGrid.GetLength(0); c++) {
                for (int r=0; r<GameManager.instance.mapGrid.GetLength(1); r++) {
                    if(GameManager.instance.mapGrid[c,r]!=null)//if not null destroy
                    {
                       Destroy(GameManager.instance.mapGrid[c,r].gameObject);
                    }
   
   
                }
   
            }
        }
   
    }

I believe the change needs to happen in my else statements but I’m not exactly sure how to go about this as I have never made a maze before. Thanks for the help!

You can’t easily generate a maze in a single pass. Use the above code to generate a grid with rooms that have all doors open. then do the following

  • pick a start Location
  • pick a end Location. user must find start to end
  • write an algorithm that verifies you can get from start to end

now, in a Loop for a variable number (to be found by Trial and error)

  • randomly Close a door
  • verify that the maze is still solvable from start to end
  • if not, open the door

That way you can create labyriths of varying difficulties

As an alternative option, would this cover your requirements? :

  • Create a grid with all doors closed and add each room to a list of sealed rooms.
  • Now create a guaranteed solution. Do this by starting at the beginning and move to a random adjoining room (that is not previously visited) until the end point is reached unlocking doors as you go (removing these rooms from the sealed rooms list).
  • Pick a random sealed room and remove it from the sealed room list.
  • Move to a random adjoining room (that has not been picked since Step 3). If that room is sealed, unlock the door just taken, remove from the sealed room list and repeat this step.
  • While sealed rooms is not empty, return to step 3.

This will generate at least one workable path but maybe more and will end up with all rooms being visitable.