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 7037819
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T01:35:28+00:00 2026-05-28T01:35:28+00:00

I am having a problem with an algorithm I wrote for creating a ascii

  • 0

I am having a problem with an algorithm I wrote for creating a ascii maze. The code is using a recursive back tracker, and the pseudo code essentially is:

1. Make the initial cell the current cell and mark it as visited
2. While there are unvisited cells
    1. If the current cell has any neighbours which have not been visited
        1. Choose randomly one of the unvisited neighbours
        2. Push the chosen cell to the stack
        3. Remove the wall between the current cell and the chosen cell
        4. Make the chosen cell the current cell and mark it as visited
    2. Else
        1. Pop a cell from the stack
        2.Make it the current cell

The problem that I am having is that the code is getting stuck in a loop of adding to the stack and then popping the stack. The code currently uses one system command for linux on line 19 if anyone needs to change it for a windows machine

#include<iostream>
#include<cstdlib>
#include<stack>
#include<ctime>

#define NORTH   0
#define SOUTH   1
#define EAST    2
#define WEST    3
#define SIZEX   20
#define SIZEY   20

using namespace std;

int nGood = 0;
int locX = 1, locY = 1;

void printGrid(char grid[SIZEY][SIZEX]){
system("clear");
for (int i = 0; i < SIZEY; i++){
    for(int j = 0; j < SIZEX; j++){
        cout << grid[i][j];
    }
    cout << endl;
}
}

int moveEW(int direction, int x){
    if (direction == EAST)
            return x + 1;
    else if (direction == WEST)
            return x - 1;
    else
            return x;
}

int moveNS(int direction, int y){
    if (direction == NORTH)
            return y - 1;
    else if (direction == SOUTH)
            return y + 1;
    else
            return y;
}

 bool isGood(int x, int y, int direction, char grid[SIZEY][SIZEX]){
x = moveEW(direction,x);
y = moveNS(direction,y);

if (grid[x][y] == '.' || x >= (SIZEX - 1) || x <= 0 || y <= 0 || y >= (SIZEY - 1)){
    return false;
}

// check cardinal directions
if (direction == NORTH){
    if (grid[y][x-1] != '.' &&  grid[y-1][x] != '.' && grid[y][x+1] != '.' &&  grid[y-1][x-1] != '.' && grid[y-1][x+1] != '.'){
        return true;
    }
}
if (direction == SOUTH){
            if (grid[y][x-1] != '.' &&  grid[y+1][x] != '.' && grid[y][x+1] != '.' && grid[y+1][x-1] != '.' && grid[y+1][x+1] != '.'){
                    return true;
            }
    }
if (direction == EAST){
            if (grid[y][x+1] != '.' &&  grid[y-1][x] != '.' && grid[y+1][x] != '.' && grid[y-1][x+1] != '.' && grid[y+1][x+1] != '.'){
                    return true;
            }
    }
if (direction == WEST){
            if (grid[y][x-1] != '.' &&  grid[y-1][x] != '.' && grid[y+1][x] != '.' && grid[y-1][x-1] != '.' && grid[y+1][x-1] != '.'){
                    return true;
            }
    }
return false;
}

main(){
char grid[SIZEY][SIZEX];

// init grid
for (int i = 0; i < SIZEY; i++){
            for(int j = 0; j < SIZEX; j++){
                    grid[i][j] = '#';
            }
    }

//init rand
srand(time(0));

//init stacks for xy coords
stack<int> xValues;
stack<int> yValues;

nGood = 0;
int direction = 0;

do{
    //find n good moves
    for (int i = 0; i < 4; i++){
        if (isGood(locX,locY,i,grid))
            nGood++;
    }

    // if only 1 good move, move there
    if (nGood == 1){
        if (isGood(locX,locY,NORTH,grid))
            locY = moveNS(NORTH,locY);
        else if (isGood(locX,locY,SOUTH,grid))
            locY = moveNS(SOUTH,locY);
        else if (isGood(locX,locY,EAST,grid))
                            locX = moveEW(EAST,locX);
                    else if (isGood(locX,locY,WEST,grid))
                            locX = moveEW(WEST,locX);
    }

    // if no good moves, move back in stack
    else if (nGood == 0){
        locX = xValues.top();
        locY = yValues.top();
        xValues.pop();
        yValues.pop();
    }

    //if more than 1 good move, push stack
    else if (nGood > 1){
        xValues.push(locX);
        yValues.push(locY);

        //direction to move randomly chosen
        do{
            direction = rand() % 4;
        }while (!isGood(locX,locY,direction,grid));

        locX = moveEW(direction,locX);
        locY = moveNS(direction,locY);
    }

    // set grid
    grid[locY][locX] = '.';
    //output grid to show creation
    printGrid(grid);
    //reset nGood value
            nGood = 0;

}while(!xValues.empty());

//final maze output
printGrid(grid);
return 0;
}
  • 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-28T01:35:29+00:00Added an answer on May 28, 2026 at 1:35 am

    You swapped x and y in first if in isGood function (grid[x][y] instead of grid[y][x]). This causes the problem at least sometimes.

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

Sidebar

Related Questions

I am having problem in getting reference of Ajax control extender using $find() or
I am having a problem with the following code. It seems to be breaking
I'm creating an auto complete and I'm having a little problem the results highlighting.
I'm not looking for an implementation, just pseudo-code, or at least an algorithm to
I am having a general problem finding a good algorithm for generating each possible
I'm having a problem coming up with an algorithm for a big integer class
I know the basic algorithm for this problem but I am having trouble changing
I'm working on a homework problem and I'm having some difficulties creating a O(n*logn)
I'm implementing a customized Graph algorithm in vb.net, and I'm having the following problem:
I'm having a headache implementing this (awful) pseudo-java code (I wonder: why the hell

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.