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

  • Home
  • SEARCH
  • 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 493507
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T05:24:44+00:00 2026-05-13T05:24:44+00:00

I’ve written some code that generates mazes for me. The maze consists of (n

  • 0

I’ve written some code that generates mazes for me. The maze consists of (n x n) cells, each cell has a boolean value to represent a wall (north, south, east west).

It is working fine, and I wrote the function below to print out the maze:

public static void printMaze(Cell[][] maze)
    {
        for(int i = 0; i < maze.length; i++)
        {
            for(int j = 0; j < maze[i].length; j++)
            {
                System.out.print((maze[i][j].walls.get(Dir.NORTH)) ? "+--+" : "+  +"); 
            }
            System.out.println();
            for(int j = 0; j < maze[i].length; j++)
            {
                System.out.print((maze[i][j].walls.get(Dir.WEST)) ? "|" : " ");
                System.out.print("  ");
                System.out.print((maze[i][j].walls.get(Dir.EAST)) ? "|" : " ");
            }
            System.out.println();
            for(int j = 0; j < maze[i].length; j++)
            {
                System.out.print((maze[i][j].walls.get(Dir.SOUTH)) ? "+--+" : "+  +");
            }
            System.out.println();
        }
    }

However, because cells share walls I produce a sort of a double-wall corridor look in my print function:

+--++--++--++--++--++--++--++--++--++--+
|      ||                  ||          |
+--++  ++--++--++  ++--++--++  ++  ++--+
+--++  ++--++--++  ++--++--++  ++  ++--+
|  ||          ||  ||          ||      |
+  ++--++--++  ++  ++  ++--++--++--++  +
+  ++--++--++  ++  ++  ++--++--++--++  +
|      ||      ||  ||  ||      ||  ||  |
+  ++  ++  ++--++  ++  ++  ++  ++  ++  +
+  ++  ++  ++--++  ++  ++  ++  ++  ++  +
|  ||  ||  ||  ||          ||      ||  |
+  ++  ++  ++  ++  ++--++--++--++--++  +
+  ++  ++  ++  ++  ++--++--++--++--++  +
|  ||      ||          ||          ||  |
+  ++--++--++--++--++--++  ++--++  ++  +
+  ++--++--++--++--++--++  ++--++  ++  +
|  ||          ||          ||      ||  |
+  ++--++  ++  ++  ++--++--++  ++--++  +
+  ++--++  ++  ++  ++--++--++  ++--++  +
|          ||  ||  ||      ||  ||      |
+--++--++--++  ++  ++  ++  ++  ++  ++  +
+--++--++--++  ++  ++  ++  ++  ++  ++  +
|          ||  ||  ||  ||  ||      ||  |
+  ++  ++--++  ++  ++  ++  ++--++--++  +
+  ++  ++--++  ++  ++  ++  ++--++--++  +
|  ||  ||      ||      ||  ||  ||      |
+  ++  ++  ++--++--++--++  ++  ++  ++--+
+  ++  ++  ++--++--++--++  ++  ++  ++--+
|  ||                      ||          |
+--++--++--++--++--++--++--++--++--++--+

How should I modify my print function so it looks like:

+--+--+--+--+--+--+--+--+--+--+
|     |              |        |
+--+  +--+--+  +--+--+  +  +--+
|  |        |  |        |     |
+  +--+--+  +  +  +--+--+--+  +
|     |     |  |  |     |  |  |
+  +  +  +--+  +  +  +  +  +  +
|  |  |  |  |        |     |  |
+  +  +  +  +  +--+--+--+--+  +
|  |     |        |        |  |
+  +--+--+--+--+--+  +--+  +  +
|  |        |        |     |  |
+  +--+  +  +  +--+--+  +--+  +
|        |  |  |     |  |     |
+--+--+--+  +  +  +  +  +  +  +
|        |  |  |  |  |     |  |
+  +  +--+  +  +  +  +--+--+  +
|  |  |     |     |  |  |     |
+  +  +  +--+--+--+  +  +  +--+
|  |                 |        |
+--+--+--+--+--+--+--+--+--+--+

I fear I’m going to face a similar problem when I eventually get to the point I start drawing my maze using actual graphics rather than ascii as well.

How do I modify my printMaze method so it goes from the first example to the second one?

In case anyone is interested the source code to my class for generating these is here.

  • 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-05-13T05:24:44+00:00Added an answer on May 13, 2026 at 5:24 am

    Only print the NORTH and WEST walls. Code on its way…

    I changed the walls to an EnumSet

    public Set<Dir> walls = EnumSet.allOf(Dir.class);
    

    So you don’t need to add any walls in your constructor:

    public Cell(final int x, final int y) {
        this.x = x;
        this.y = y;
        this.Visited = false;
    }
    

    And to remove your walls, use:

    this.walls.remove(randDir);
    randomNeighbor.walls.remove(randDir.opposite());
    

    And then the print code looks like:

    public static void printMaze(final Cell[][] maze) {
        for (int r = 0; r < maze.length; r++) {
            final Cell[] row = maze[r];
            printTop(row);
            printMiddle(row);
            if (r == maze.length - 1) {
                printBottom(row);
            }
        }
    }
    
    private static void printBottom(final Cell[] row) {
        for (final Cell cell : row) {
            System.out.print(cell.walls.contains(Dir.SOUTH) ? "+--" : "+  ");
        }
        System.out.println("+");
    }
    
    private static void printMiddle(final Cell[] row) {
        for (int c = 0; c < row.length; c++) {
            final Cell cell = row[c];
            System.out.print(cell.walls.contains(Dir.WEST) ? "|  " : "   ");
            if (c == row.length - 1) {
                System.out.println(cell.walls.contains(Dir.EAST) ? "|" : " ");
            }
        }
    }
    
    private static void printTop(final Cell[] row) {
        for (final Cell cell : row) {
            System.out.print(cell.walls.contains(Dir.NORTH) ? "+--" : "+  ");
        }
        System.out.println("+");
    }
    

    (Note: Aesthetically, I prefer Direction, and randomDirection. But that’s just me 😉

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

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
That's pretty much it. I'm using Nokogiri to scrape a web page what has
Basically, what I'm trying to create is a page of div tags, each has
I've got a string that has curly quotes in it. I'd like to replace
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
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.

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.