I'm trying to use the DFS algo to create a maze in ASCII ('#' represents a wall and ' ' a free space) that has as start the upper left corner and as exit the lower right corner. The problem is the maze starts its creation and then it's blocked because all its neighbors have been already visited.
I start at the upper left corner, mark the cell as visited and put a ' ' (it represents a free space), then I chose randomly a neighbor of the cell and I do the same. However I put this in a while loop and I'm sure this isn't the good idea.
Here my attempt of the DFS :
int generation(t_maze *maze, int pos_y, int pos_x)
{
int dest;
maze->maze[pos_y][pos_x] = ' ';
maze->visited[pos_y][pos_x] = '1';
while (maze->maze[maze->height - 1][maze->width - 1] == '#')
{
if ((dest = my_rand(1, 4)) == 1 && pos_y - 1 >= 0 && maze->visited[pos_y - 1][pos_x] == '0')
generation(maze, pos_y - 1, pos_x);
else if (dest == 2 && pos_x + 1 < maze->width && maze->visited[pos_y][pos_x + 1] == '0')
generation(maze, pos_y, pos_x + 1);
else if (dest == 3 && pos_y + 1 < maze->height && maze->visited[pos_y + 1][pos_x] == '0')
generation(maze, pos_y + 1, pos_x);
else if (dest == 4 && pos_x - 1 >= 0 && maze->visited[pos_y][pos_x - 1] == '0')
generation(maze, pos_y, pos_x - 1);
my_showtab(maze->maze); //it prints the 2d array
usleep(50000);
}
typedef struct s_maze
{
int width;
int height;
char **maze;
char **visited;
} t_maze;
In the structure, width is the width of the maze height is the height of the maze maze is a 2D array that is supposed to be filled with ' ' and '#' visited is a 2D array with 0 and 1, 0 : unvisited, 1 : visited
I want to have a maze like this (little example)
########
# #
## #
# #
#######