How can I get a maze last rows/columns ?

sharon glipman 441 Reputation points
2021-08-03T21:47:35.46+00:00

This is the maze class logic :

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
    public int startX;
    public 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);
                }
            }
        }
    }
}

And this is the class that generate the maze :

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;

    // 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()
    {
        maze.Generate();
        DrawMaze();
    }

    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);
                }
            }
        }

        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] == false)
                {
                    var t = Instantiate(wallPrefab, position, Quaternion.identity);
                    t.GetComponent<Renderer>().material.color = Color.yellow;
                }
            }
        }
    }

    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 () {

    }
}

From line 55 to 67 in the MazeGenerator class I'm trying to fill only the outer gaps empty places in the maze only the outer.
For this I need to find the last rows/columns but the way I did it now it's making a loop all over the maze and fill the gaps in all the maze.

How can I find the last/outer rows/columns and fill the gaps on them ?

Not Monitored
Not Monitored
Tag not monitored by Microsoft.
36,565 questions
{count} votes