I need help populating multidimensional vectors using recursion

Hello I started my programming course at the start of this year, and am new to C++.

I've been working on making a maze for the last 2 weeks as a project game to work on while I am studying.

I wanted to know if there was a way to populate my "playing" area for my maze using a recursive function? I've been stuck here for the last week and have been trying to work it out without asking for help or looking for solutions on the internet. But now I am here, and I want to know if it's a possible option for what I am trying to create.

I first started with just drawing a diamond with 0 and 1. Once I was able to output that, I moved to filling an array.

This is the code I wrote to draw a global map limit.
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
#include <iostream>


void drawDiamond(int levelDifficulty)
{
	int globalMapSize = (levelDifficulty * 2) + 1;
	for (int drawY = 0; drawY < globalMapSize; drawY++)
	{
		for (int drawX = 0; drawX < globalMapSize; drawX++)
		{
			if (drawX == levelDifficulty && drawY == 0)
			{
				std::cout << "E" << " ";
			}
			else if (drawY <= levelDifficulty && drawX < levelDifficulty - drawY) //NW OUT OF BOUNDS
			{
				std::cout << "0" << " ";
			}
			else if (drawY <= levelDifficulty && drawX == levelDifficulty - drawY)
			{
				std::cout << "E" << " ";
			}
			else if (drawY <= levelDifficulty && drawX > levelDifficulty + drawY) //NE OUT OF BOUNDS
			{
				setColor(0);
				std::cout << "0" << " ";
			}
			else if (drawY <= levelDifficulty && drawX == levelDifficulty + drawY)
			{
				setColor(12);
				std::cout << "E" << " ";
			}
			else if (drawY > levelDifficulty && drawX < drawY - levelDifficulty) //SW OUT OF BOUNDS
			{
				std::cout << "0" << " ";
			}
			else if (drawY > levelDifficulty && drawX == drawY - levelDifficulty)
			{
				std::cout << "E" << " ";
			}
			else if (drawY > levelDifficulty && drawX > levelDifficulty + globalMapSize - drawY - 1) //SE OUT OF BOUNDS
			{
				std::cout << "0" << " ";
			}
			else if (drawY > levelDifficulty && drawX == levelDifficulty + globalMapSize - drawY - 1)
			{
				std::cout << "E" << " ";
			}
			else if (drawY == levelDifficulty && drawX == levelDifficulty)
			{
				std::cout << "S" << " ";
			}
			else
			{
				std::cout << "1" << " ";
			}
		}
		std::cout << std::endl;
	}
}


int main()
{
	//INTRODUCTION
	//introduction();

	bool exitMain = false;
	do
	{
		//MAIN BLOCK
		
		unsigned int userInputLevel;

		do
		{
			system("CLS");
			std::cout << "Enter difficulty level [1 to 9]: ";
			std::cin >> userInputLevel;
		} while (userInputLevel < 1 || userInputLevel > 9);

		std::cout << std::endl << "Generating Map Limit according to difficulty rating!" << std::endl << std::endl;

		drawDiamond(userInputLevel);

		std::cout << std::endl;
		system("PAUSE");

		if (GetAsyncKeyState(VK_ESCAPE))  //If "Esc" is pressed, Exit from do:while.
		{
			exitMain = true;
		}
	} while (exitMain == false);
	return 0;
}



I wanted to use a vector so that I could assign a map size scaling with difficulty for my game. There are a few variables in my code below that are identical but I just gave specific identifiers as when I started working with the multidimensional array, I started to get confused with index values, so I labelled them X and Y.

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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <iostream>
#include <Windows.h>
#include <vector>
#include <string>
#include <sstream>
#include <ctime>
#include <random>
#include <cstdlib>

using namespace std;


class Room
{
public:
	Room()	
	//Room Function: When a room is created, set values to:
	{
		isStart = 0;
		isEnd = 0;
		isWalkable = 0;
		isExitChar = 'E';
		isStartChar = 'S';
	}
	int isStart;
	char isStartChar;
	int isEnd;
	char isExitChar;
	int isWalkable;
};


unsigned int selectDifficulty()
{
	unsigned int inputValue;
	char userInput[128];
	do
	{
		system("CLS");
		cout << "Enter difficulty level [1 to 9]: ";
		cin.getline(userInput, 128);					//CRASH PREVENTION STRING CHECK - MUST BE INT - USE GETLINE, ELSE INFINITE LOOP
		istringstream checkInput(userInput);			//checkInput Function passes in userInput
		checkInput >> inputValue;						//checkInput converted to inputValue Integer

	} while (inputValue < 1 || inputValue > 9);

	cout << endl;
	
	return inputValue;
}


int main()
{
	//SetWindow(100, 50); 
	//INTRODUCTION
	//introduction();

	unsigned int mainDifficultySelection = 0; //Initialize a variable for difficulty selection to pass to function to create map size
	unsigned int globalMapSizeX = 0; //Intitialize a variable to store map size X after function returns value selected by user
	unsigned int globalMapSizeY = 0;
	unsigned int globalMapSize;
	unsigned int inputValue = 0;

	bool exitMain = false;
	do
	{
		//MAINBLOCK


		//consoleDrawArea();
		//cin.get();


		mainDifficultySelection = selectDifficulty();
		inputValue = mainDifficultySelection;
		globalMapSize = (inputValue * 2) + 1;
		globalMapSizeX = globalMapSize;
		globalMapSizeY = globalMapSize;
		
		cout << endl;
		//cout << mainDifficultySelection << " " << globalMapSize << " " << globalMapSizeY << " " << globalMapSizeX << " ";

		vector< vector< Room> > GlobalMap(globalMapSizeX, vector <Room>(globalMapSizeY));


		unsigned int indexX;
		unsigned int indexY;

		for (indexX = 0; indexX < globalMapSizeX; indexX++)
		{
			for (indexY = 0; indexY < globalMapSizeY; indexY++)
			{
				GlobalMap[indexX][indexY].isWalkable = 0;							//Populate Global Map as Non-Walkable
				//cout << GlobalMap[indexX][indexY].isWalkable << " ";				//Print Global Map Layout
			}
			//cout << endl;
		}

		unsigned int levelDifficulty;
		levelDifficulty = inputValue;

		for (unsigned int drawY = 0; drawY < globalMapSize; drawY++)
		{
			for (unsigned int drawX = 0; drawX < globalMapSize; drawX++)
			{
				if (drawX == levelDifficulty && drawY == 0)		//N EXIT
				{
					GlobalMap[drawX][drawY].isEnd = 1;
					GlobalMap[drawX][drawY].isWalkable = 1;
				}
				else if (drawY <= levelDifficulty && drawX < levelDifficulty - drawY) //NW OUT OF BOUNDS
				{
					GlobalMap[drawX][drawY].isWalkable = 0;
				}
				else if (drawY <= levelDifficulty && drawX == levelDifficulty - drawY) //NW EXIT
				{
					GlobalMap[drawX][drawY].isEnd = 1;
					GlobalMap[drawX][drawY].isWalkable = 1;
				}
				else if (drawY <= levelDifficulty && drawX > levelDifficulty + drawY) //NE OUT OF BOUNDS
				{
					GlobalMap[drawX][drawY].isWalkable = 0;
				}
				else if (drawY <= levelDifficulty && drawX == levelDifficulty + drawY) //NE EXIT
				{
					GlobalMap[drawX][drawY].isEnd = 1;
					GlobalMap[drawX][drawY].isWalkable = 1;
				}
				else if (drawY > levelDifficulty && drawX < drawY - levelDifficulty) //SW OUT OF BOUNDS
				{
					GlobalMap[drawX][drawY].isWalkable = 0;
				}
				else if (drawY > levelDifficulty && drawX == drawY - levelDifficulty) //SW EXIT
				{
					GlobalMap[drawX][drawY].isEnd = 1;
					GlobalMap[drawX][drawY].isWalkable = 1;
				}
				else if (drawY > levelDifficulty && drawX > levelDifficulty + globalMapSize - drawY - 1) //SE OUT OF BOUNDS
				{
					GlobalMap[drawX][drawY].isWalkable = 0;
				}
				else if (drawY > levelDifficulty && drawX == levelDifficulty + globalMapSize - drawY - 1) //SE EXIT
				{
					GlobalMap[drawX][drawY].isEnd = 1;
					GlobalMap[drawX][drawY].isWalkable = 1;
				}
				else if (drawY == levelDifficulty && drawX == levelDifficulty) //START POINT
				{
					GlobalMap[drawX][drawY].isStart = 1;
					GlobalMap[drawX][drawY].isWalkable = 1;
				}
				else
				{
					GlobalMap[drawX][drawY].isWalkable = 1;
				}
			}
		}

		//	cout << endl << endl << endl;

		unsigned int indexX2;
		unsigned int indexY2;

		for (indexX2 = 0; indexX2 < globalMapSizeX; indexX2++)
		{
			for (indexY2 = 0; indexY2 < globalMapSizeY; indexY2++)
			{
				if (GlobalMap[indexX2][indexY2].isEnd == 1)
				{
					setColor(12);
					cout << GlobalMap[indexX2][indexY2].isExitChar << " ";
					setColor(7);

				}
				else if (GlobalMap[indexX2][indexY2].isStart == 1)
				{
					setColor(10);
					cout << GlobalMap[indexX2][indexY2].isStartChar << " ";
					setColor(7);
				}
				else if (GlobalMap[indexX2][indexY2].isWalkable == 0)
				{
					setColor(8);
					cout << GlobalMap[indexX2][indexY2].isWalkable << " ";
					setColor(7);
				}
				else
				{
					setColor(15);
					cout << GlobalMap[indexX2][indexY2].isWalkable << " ";
					setColor(7);
				}
			}
			cout << endl;
		}
		

		cout << endl;
		system("PAUSE");
	
		if (GetAsyncKeyState(VK_ESCAPE))
		//IF "ESC" EXIT FROM DO-WHILE
		{
			exitMain = true;
		}
	} while (exitMain == false);

	//ON EXIT
	system("CLS");
	//onExit();

	return 0;
}



I am now trying to find a way to do what I am doing using a recursive function.
Would it be better for me to use a recursive function for this? or am I in way over my head?

I've managed to compact my code even further by using this

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
int main()

{
	int mapX = 0, mapY = 0, mazeMaxDistance = 9;
	for (mapX = -mazeMaxDistance; mapX <= mazeMaxDistance; mapX++)
	{
		for (mapY = -mazeMaxDistance; mapY <= mazeMaxDistance; mapY++)
		{
			if (abs(mapX) + abs(mapY) <= mazeMaxDistance)
			{
				cout << "1 ";
			}
			else { cout << "0 "; }
		}
		cout << endl;
	}
	return 0;
}




This is the new code for my maze currently without the drawn map limit.
I am just using it to create my path finding/maze creation for the text dungeon crawler I want to build.
It's Move North South East West, and then I am just tracing the path. I still need to add more conditions so that the maze doesn't turn in on itself. But I will be able to work that out later.

The biggest problem I have is is trying to populate the maze in my diamond shape using recursion. I'm not sure if it's possible (I'm sure it is possible), or more efficient) - I'm still unsure as to what I'm supposed to do as a C++ Programming Student. If it's not possible to do what I'm trying to do, I just want someone to tell me it's not worth my time and effort trying to use a recursive function to populate my maze.

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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
#include <iostream>
#include <ctime>
#include <vector>
#include <Windows.h>

using namespace std;



void setColor(unsigned short color)
{
	HANDLE hConCol = GetStdHandle(STD_OUTPUT_HANDLE);
	SetConsoleTextAttribute(hConCol, color);
	/*
	HEX COLOUR CODES
	0 = Black
	1 = Blue
	2 = Green
	3 = Aqua
	4 = Red
	5 = Purple
	6 = Yellow
	7 = White
	8 = Gray
	9 = Light Blue
	A = Light Green		10
	B = Light Aqua		11
	C = Light Red		12
	D = Light Purple	13
	E = Light Yellow	14
	F = Bright White	15
	*/
}

void SetWindow(int Width, int Height)	// (X CHARACTERS, Y LINES)
{
	_COORD coord;
	coord.X = Width;
	coord.Y = Height;

	_SMALL_RECT Rect;
	Rect.Top = 0;
	Rect.Left = 0;
	Rect.Bottom = Height - 1;
	Rect.Right = Width - 1;

	HANDLE Handle = GetStdHandle(STD_OUTPUT_HANDLE);      // Get Handle 
	SetConsoleScreenBufferSize(Handle, coord);            // Set Buffer Size 
	SetConsoleWindowInfo(Handle, TRUE, &Rect);            // Set Window Size 
}


struct Coordinate
{
	unsigned int x;
	unsigned int y;
};

struct Room
{
	Coordinate RoomPosition;
	int MapExit;
	int MapStart;
	int PlaceHolder;
	int Visited;
};



int main()

{
	srand((unsigned int)time(NULL));
	SetWindow(100, 50);
	//int numbera; //roomcounter
	int roomcount = 0;
	unsigned int tempX = 9;
	unsigned int tempY = 9;
	unsigned int inputLevel;
	unsigned int difficulty;
	inputLevel = 9;
	difficulty = (inputLevel * 2) + 1;
	vector <vector <Room> > GlobalMap(difficulty, vector<Room>(difficulty));
	GlobalMap[inputLevel][inputLevel].MapStart = 1;

	for (unsigned int indexY = 0; indexY < difficulty; indexY++) //ALLOCATE VECTOR COORINATES FOR MAP
	{
		for (unsigned int indexX = 0; indexX < difficulty; indexX++)
		{
			GlobalMap[indexY][indexX].RoomPosition.x = indexX;
			GlobalMap[indexY][indexX].RoomPosition.y = indexY;
		}
	}





	unsigned int doorExit;
	unsigned int prevDoorExitCheck;
	char door1 = 'N';
	char door2 = 'E';
	char door3 = 'S';
	char door4 = 'W';
	int roomEntryDoorLoc;

		doorExit = rand() % 4 + 1;
		cout << "Map Start: Door Exit Location: ";
		setColor(10);
		switch (doorExit)
		{
		case 1:
			cout << door1 << endl;
			tempY--;
			GlobalMap[tempY][tempX].Visited = 1;
			break;
		case 2:
			cout << door2 << endl;
			tempX++;
			GlobalMap[tempY][tempX].Visited = 1;
			break;
		case 3:
			cout << door3 << endl;
			tempY++;
			GlobalMap[tempY][tempX].Visited = 1;
			break;
		case 4:
			cout << door4 << endl;
			tempX--;
			GlobalMap[tempY][tempX].Visited = 1;
			break;
		}
		setColor(7);
		
		prevDoorExitCheck = doorExit;
		if (doorExit == 1) // IF PREVIOUS ROOM EXIT DOOR WAS NORTH, CURRENT ROOM ENTRY DOOR IS SOUTH
		{
			roomEntryDoorLoc = 3;
		}
		else if (prevDoorExitCheck == 2) // IF PREVIOUS ROOM EXIT DOOR WAS EAST, CURRENT ROOM ENTRY DOOR IS WEST
		{
			roomEntryDoorLoc = 4;
		}
		else if (prevDoorExitCheck == 3) // IF PREVIOUS ROOM EXIT DOOR WAS SOUTH, CURRENT ROOM ENTRY DOOR IS NORTH
		{
			roomEntryDoorLoc = 1;
		}
		else if (prevDoorExitCheck == 4) // IF PREVIOUS ROOM EXIT DOOR WAS WEST, CURRENT ROOM ENTRY DOOR IS EAST
		{
			roomEntryDoorLoc = 2;
		}

		for (unsigned int rolla = 0; rolla < inputLevel; rolla++) //GENERATE ROOMS UP TO LEVEL DIFFICULTY
		{
			
			switch (roomEntryDoorLoc) //IF ROOM ENTRY IS
			{
			case 1: //NORTH
				cout << "Room #: " << rolla + 1 << ", Entry Door: ";
				cout << door1 << ", Door Exit Location: ";
				break;
			case 2: //EAST
				cout << "Room #: " << rolla + 1 << ", Entry Door: ";
				cout << door2 << ", Door Exit Location: ";
				break;
			case 3: //SOUTH
				cout << "Room #: " << rolla + 1 << ", Entry Door: ";
				cout << door3 << ", Door Exit Location: ";
				break;
			case 4: //WEST
				cout << "Room #: " << rolla + 1 << ", Entry Door: ";
				cout << door4 << ", Door Exit Location: ";
				break;
			}
			if (rolla == inputLevel-1)
			{
				setColor(12);
				cout << "LEVEL MAP EXIT ROOM"; setColor(7);
				GlobalMap[tempY][tempX].MapExit = 1;
				
			}
		else
			{
				do
				{
					doorExit = rand() % 4 + 1;
				} while (doorExit == roomEntryDoorLoc); //EXIT DOOR GENERATED IS THE SAME AS THE ENTRY DOOR, REGENERATE    //******ADD ALGORITHM SAYING: CHECK IF ROOM IS PLACEHOLDER TO NOT LOOP BACK IN TO ROOM (WILL NEED TO ADD NEW VARIABLE IN VECTOR TO ASSIGN FLAGS)
				setColor(9);
				switch (doorExit) //IF THE DOOR EXIT IS
				{
				case 1: //NORTH
					cout << door1; //PRINT NORTH
					tempY--; //INCREMENT MOVEMENT COUNTER
					GlobalMap[tempY][tempX].Visited = 1;
					roomEntryDoorLoc = 3; //SET NEW ROOM ENTRY TO SOUTH
					break;
				case 2:
					cout << door2; //EAST
					tempX++;
					GlobalMap[tempY][tempX].Visited = 1;
					roomEntryDoorLoc = 4; //SET NEW ROOM ENTRY TO WEST
					break;
				case 3:
					cout << door3; //SOUTH
					tempY++;
					GlobalMap[tempY][tempX].Visited = 1;
					roomEntryDoorLoc = 1; //SET NEW ROOM ENTRY TO NORTH
					break;
				case 4:
					cout << door4; //WEST
					tempX--;
					GlobalMap[tempY][tempX].Visited = 1;
					roomEntryDoorLoc = 2; //SET NEW ROOM ENTRY TO EAST
					break;
				}
				setColor(7);

			}
			cout << endl;
		}


	
	system("Pause");
	cout << endl;


	for (unsigned int indexY = 0; indexY < difficulty; indexY++)  //PRINT COORDINATES

		//cout << "0" << GlobalMap[indexY][indexX].RoomPosition.y << "0" << GlobalMap[indexY][indexX].RoomPosition.x << " ";

	{
		for (unsigned int indexX = 0; indexX < difficulty; indexX++)
		{
			
			if (indexY == 9 && indexX == 9)
			{
				setColor(10);
			}
			if (GlobalMap[indexY][indexX].Visited == 1)
			{
				setColor(9);
			}
			else
			{
				setColor(7);
			}
			if (GlobalMap[indexY][indexX].MapExit == 1)
			{
				setColor(12);
			}
			if (GlobalMap[indexY][indexX].MapStart == 1)
			{
				setColor(10);
			}




			if (GlobalMap[indexY][indexX].RoomPosition.y <= inputLevel && GlobalMap[indexY][indexX].RoomPosition.x <= inputLevel) //QUAD2
			{
				cout << "0" << GlobalMap[indexY][indexX].RoomPosition.y << "0" << GlobalMap[indexY][indexX].RoomPosition.x << " ";
			}
			if (GlobalMap[indexY][indexX].RoomPosition.y > inputLevel && GlobalMap[indexY][indexX].RoomPosition.x > inputLevel) //QUAD4
			{
				cout << "" << GlobalMap[indexY][indexX].RoomPosition.y << "" << GlobalMap[indexY][indexX].RoomPosition.x << " ";
			}
			if (GlobalMap[indexY][indexX].RoomPosition.y <= inputLevel && GlobalMap[indexY][indexX].RoomPosition.x > inputLevel) //QUAD1
			{
				cout << "0" << GlobalMap[indexY][indexX].RoomPosition.y << "" << GlobalMap[indexY][indexX].RoomPosition.x << " ";
			}
			if (GlobalMap[indexY][indexX].RoomPosition.y > inputLevel && GlobalMap[indexY][indexX].RoomPosition.x <= inputLevel) //QUAD3
			{
				cout << "" << GlobalMap[indexY][indexX].RoomPosition.y << "0" << GlobalMap[indexY][indexX].RoomPosition.x << " ";
			}

		}
		cout << endl;
	}


	return 0;
}



Thanks in advance
If you took the time to read my problem
Last edited on
Topic archived. No new replies allowed.