Maze generation problem

closed account (E3h7X9L8)
stack overflow over and over again, I cant find the mistake...
the algorithm is like this :


Generate an int array with 4 random numbers to represent directions.
Start a for loop to go for 4 times.
Set up a switch statement to take care of 4 directions.
For that direction, check if the new cell will be out of maze or if it’s a path already open. If so, do nothing.
If the cell in that direction is a wall, set that cell to path and call recursive method passing the new current row and column.
Done.




1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <iostream>
using std::cout;
using std::endl;
using std::cin;

#include <cstdlib>
using std::srand;
using std::rand;

#include <algorithm>
using std::random_shuffle;

#include <ctime>
using std::time;

void generateMaze(int **, int, int, int, int);
void printMaze(int **, int, int);

int main()
{
	srand(time(0));

	int **maze;
	maze = new int*[50];
	for (int i = 0; i < 50; i++)
		maze[i] = new int[50];

	int width, height;

	cout << "Enter height:";
	cin >> height;
	cout << "Enter width: ";
	cin >> width;

	//Initialise all array to 1 (WALLS)
	for (int i = 0; i < height; i++)
	for (int j = 0; j < width; j++)
		maze[i][j] = 1;

	
	int randX, randY;
	//Generate odd starting position for algorithm
	do
	{
		randX = 1 + rand() % width - 1;
		randY = 1 + rand() % height - 1;
	} while ((randX % 2 == 0) && (randY % 2 == 0));

	maze[randX][randY] = 0;

	generateMaze(maze, randX, randY, width, height);
	printMaze(maze, width, height);

	cin.ignore(255, '\n');
	cin.get();

	return 0;
}

//Print maze
void printMaze(int **maze, int width, int height)
{
	for (int i = 0; i < height; i++)
	{
		for (int j = 0; j < width; j++)
			cout << maze[i][j];
		cout << endl;
	}
	cout << endl;
}

void generateMaze(int **maze, int x, int y, int wSize, int hSize)
{
	//Generate random directions array
	int dirs[4] = { 1, 2, 3, 4 };
	random_shuffle(&dirs[0], &dirs[3]);

	//Check and follow directions
	for (int i = 0; i < 4; i++)
	{
		switch (dirs[i])
		{
		case 1://Up
			if (x - 2 <= 0)
				break;
			if (x - 2 != 0)
			{
				maze[x - 2][y] = 0;
				maze[x - 1][y] = 0;
				generateMaze(maze, x - 2, y, wSize, hSize);
			}
			break;
		case 2://Right
			if (y + 2 >= wSize - 1)
				break;
			if (y + 2 != 0)
			{
				maze[x][y + 2] = 0;
				maze[x][y + 1] = 0;
				generateMaze(maze, x, y + 2, wSize, hSize);
			}
			break;
		case 3://Down
			if (x + 2 >= hSize - 1)
				break;
			if (x + 2 != 0)
			{
				maze[x + 2][y] = 0;
				maze[x + 1][y] = 0;
				generateMaze(maze, x + 2, y, wSize, hSize);
			}
			break;
		case 4://Left
			if (y - 2 <= 0)
				break;
			if (y - 2 != 0)
			{
				maze[x][y - 2] = 0;
				maze[x][y - 1] = 0;
				generateMaze(maze, x, y - 2, wSize, hSize);
			}
			break;
		
		}
	}
}

Last edited on
Topic archived. No new replies allowed.