First time user and i need some help fixing my code..

So i’m trying to make a simple maze generator using recursion and backtracking but for some reason it keeps deleting every other row and column. The first thing i do is fill the board with blocks and add them to a 2d array. The next step is to start the recursion. I make a list of random directions named dir and then i make a case statement for each possible direction. if the direction was up then id check 2 blocks up, delete them and go on to the next directions and so on. i really don’t know whats going wrong so any help would be appreciated. Also this is the first time i’ve developed with unity so i haven’t really read the API so i don’t know all of the classes and stuff :c

void Phase_one()//Fill board with blocks and make start position
    {
        GameObject ToInstantiate = box;
        boardHold = new GameObject("board").transform;
        gridPositions = new GameObject[columns, rows];
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
                    GameObject instance =
                    Instantiate(ToInstantiate, new Vector3(i, 0f, j), Quaternion.identity) as GameObject;
                    instance.transform.SetParent(boardHold);
                    gridPositions[i,j] = instance;//store blocks on map in list of gridpositions
            }
        }
    }

    void Phase_three()
    {
       
        //set a random coordinate for start point
        int r = Random.Range(1, rows);
        while (r % 2 == 0)
        {
            r = Random.Range(1, rows);
        }
        int c = Random.Range(1, columns);
        while (c % 2 == 0)
        {
            c = Random.Range(1, columns);
        }
        //destroy object at that position to make starting point for maze creation
        Destroy(gridPositions[r,c]);

        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < columns; j++)
            {
                Debug.Log(gridPositions[i, j]);
            }
        }

        Amazing(r, c);//start maze creation

    }

    void Amazing(int r, int c)
    {
        Dir = DirMaker();//randomize the list
        float z = .5f;
        for (int i = 0; i < Dir.Length;i++)//get first direction of list
        {
            switch (Dir[i])
            {
                case 1: // direction is left
                    /* (r - 2 <= 0)//if theres no object to the left
                        continue;
                    if (Physics.CheckSphere(new Vector3(r - 2, 0f, c), z))//is there an object 2 blocks away?
                    {
                        Destroy(gridPositions[ r - 2, c ]);//destroy object two objects away
                        Destroy(gridPositions[ r - 1, c ]);//destroy object one object away
                        gridPositions[r - 2, c] = null;
                        gridPositions[r - 1, c] = null;
                        Amazing( r - 2, c );//start over again two positions in current direction
                    }*/
                    break;
                case 2: // direction is up
                    if (c + 2 >= columns - 1)//if we can continue to the right
                        continue;
                    if (Physics.CheckSphere(new Vector3(r, 0f, c + 2), z))
                    {
                        Destroy(gridPositions[ r , c + 2]);
                        Destroy(gridPositions[ r , c + 1]);
                        gridPositions[r, c + 2] = null;
                        gridPositions[r, c + 1] = null;
                        Amazing( r , c + 2 );
                    }
                    break;
                case 3: // direction is right
                    if (r + 2 >= rows - 1)//if we can continue downwards
                        continue;
                    if (Physics.CheckSphere(new Vector3(r + 2, 0f, c), z))
                    {
                        Destroy(gridPositions[r + 2, c ]);
                        Destroy(gridPositions[r + 1, c ]);
                        gridPositions[r + 2, c] = null;
                        gridPositions[r + 1, c] = null;
                        Amazing( r + 2, c );
                    }
                    break;
                case 4: // direction is down
                    if (c - 2 <= 0)//if we can continue to the left
                        continue;
                    if (Physics.CheckSphere(new Vector3(r, 0f, c - 2), z))
                    {
                        Destroy(gridPositions[ r , c - 2]);
                        Destroy(gridPositions[ r , c - 1]);
                        gridPositions[r, c - 2] = null;
                        gridPositions[r, c - 1] = null;
                        Amazing( r , c - 2 );
                    }
                    break;
            }
        }
    }

    public int[] DirMaker()
    {
        int[] randoms = { 1, 2, 3, 4};
        for (int i = 0; i < Random.Range(3, 7); i++)
        {
            Reshuffle(randoms);
        }
        return randoms;
    }

    void Reshuffle(int[] texts)// shuffle thang
    {
        for (int t = 0; t < texts.Length; t++)
        {
            int tmp = texts[t];
            int r = Random.Range(t, texts.Length);
            texts[t] = texts[r];
            texts[r] = tmp;
        }
    }

ok nvm i fixed it hehe c:

1 Like