Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8802639
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T01:08:07+00:00 2026-06-14T01:08:07+00:00

I’m currently working on a maze generating program, using a depth first search. I

  • 0

I’m currently working on a maze generating program, using a depth first search. I have the following code, but I can’t figure out what I’m doing wrong. I’m trying to have X’s in between to say which ones are walls. But it’s overwriting them, and putting paths right next to each other. Any help would be great. Below is the output.

. . . # # # # # # # 
. . . . # . . . . . 
. . . . # . # # # . 
. . # . # . . . # . 
. . # # # # # . # # 
. . . . # . . . # # 
. # # . # . # # # # 
. # # . . . # # # # 
. . . . . # # # # # 
# # # # # # # # # # 

import java.util.Random;

public class DepthFirstSearch {

    private Stack stack;
    public DepthFirstSearch() {}
    public char[][] DFS (int size, char[][] maze) {

        Random myRand = new Random();
        stack = new Stack(size);
        int x = myRand.nextInt(size);
        while (x % 2 == 0)
            x = myRand.nextInt(size);
        int y = myRand.nextInt(size);
        while (y % 2 == 0)
            y = myRand.nextInt(size);

        maze[x][y] = ' ';
        int total = (size * size) / 4;
        int visited = 1;
        int random[] = new int[4];
        int totalrand;

        while (visited < total) {
            totalrand = 0;
            if (x > 1 && maze[x - 2][y] == 'X')
                random[totalrand++] = 1;
            if (x < size - 2 && maze[x + 2][y] == 'X')
                random[totalrand++] = 2;
            if (y > 1 && maze[x][y - 2] == 'X')
                random[totalrand++] = 3;
            if (y < size - 2 && maze[x][y + 2] == 'X')
                random[totalrand++] = 4;

            if (totalrand > 0) {
                switch(random[myRand.nextInt(totalrand)]) {
                    case 1: maze[x-2][y] = maze[x-1][y] = ' ';
                            x -= 2;
                            stack.push(x * size + y);
                            visited++;
                            break;
                    case 2: maze[x+2][y] = maze[x+1][y] = ' ';
                            x += 2;
                            stack.push(x * size + y);
                            visited++;
                            break;
                    case 3: maze[x][y-2] = maze[x][y-1] = ' ';
                            y -= 2;
                            stack.push(x * size + y);
                            visited++;
                            break;
                    case 4: maze[x][y+2] = maze[x][y+1] = ' ';
                            y += 2;
                            stack.push(x * size + y);
                            visited++;
                            break;
                }
            }
            else {
                int vert = stack.pop();
                x = vert / size;
                y = vert % size;
            }
        }
        return maze;
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T01:08:08+00:00Added an answer on June 14, 2026 at 1:08 am

    As it turns out, this works perfectly. I was trying to be more efficient with my stack, and messed it up. So with a working stack, it works perfectly.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This could be a duplicate question, but I have no idea what search terms
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
I have a French site that I want to parse, but am running into
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
We're building an app, our first using Rails 3, and we're having to build
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have thousands of HTML files to process using Groovy/Java and I need to
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
I'm making a simple page using Google Maps API 3. My first. One marker

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.