I just saw the thread from CppProgrammer88 (which is closed) and I can't post the code I wrote, so I created this thread to complete his implementation (it would be great if a moderator could move this post to his thread).
I don't know if this is the way the exercise should be solved, but looking at how he thought it could be solved, I extended it.
Code:
/*
* Write a program that takes a width and a height and
* dynamically generates a maze with the given width
* and height. The maze must always have a valid path
* through it (how can you ensure this?). Print the maze
* to the screen once it's been generated.
*/#include <ctime>
#include <cstdlib>
#include <iostream>
#include <string>
using namespace std;
const char WALL = 'X';
int promptPositiveInteger(string prompt);
char** createArray(int height, int width);
char randomMazeChar();
void createMaze(char** maze, int height, int width);
void printMaze(char** maze, int height, int width);
void createHorizontalPath(char** maze, int height, int width);
int main()
{
int seed = time(NULL);
srand(seed);
cout << "Dynamically generated Maze." << endl;
cout << "The seed was " << seed << endl;
int height = promptPositiveInteger("Enter the height of the maze(min 3): ");
int width = promptPositiveInteger("Enter the width of the maze(min 3): ");
char** maze = createArray(height, width);
createMaze(maze, height, width);
cout << endl;
printMaze(maze, height, width); // before creating the valid path
cout << endl << endl;
createHorizontalPath(maze, height, width);
printMaze(maze, height, width); // after creating the valid path
return 0;
}
// Prompts the user until he enters a positive integer
// the minimum size for a maze should be 3
int promptPositiveInteger(string prompt) {
int integer = 0;
while (integer < 3) {
cout << prompt;
cin >> integer;
}
return integer;
}
// Returns a pointer to a two dimensional dynamic array
char** createArray(int height, int width) {
char** maze = new char*[height];
for (int row = 0; row < width; row++) {
maze[row] = new char[width];
}
return maze;
}
// There are only two random chars, an empty space or a wall
char randomMazeChar() {
int random = rand() % 2;
switch(random) {
// wall
case 0:
return WALL;
break;
case 1:
return ' ';
break;
default:
return ' ';
}
}
// Populate the two dimensional dynamic array
void createMaze(char** maze, int height, int width) {
for (int row = 0; row < height; row++) {
for (int column = 0; column < width; column++) {
// Randomly assign a wall or an empty space
maze[row][column] = randomMazeChar();
}
}
// Place walls around the perimeter of the maze
// Top wall & Bottom walls
for (int column = 0; column < width; column++) {
maze[0][column] = WALL;
maze[height - 1][column] = WALL;
}
// East & West walls
for (int row = 0; row < height; row++) {
maze[row][0] = WALL;
maze[row][width - 1] = WALL;
}
}
// Prints the maze into the console
void printMaze(char** maze, int height, int width) {
for (int row = 0; row < height; row++) {
for (int column = 0; column < width; column++) {
cout << maze[row][column];
}
cout << endl;
}
}
// Create a valid horizontal path through the maze
void createHorizontalPath(char** maze, int height, int width) {
// start at random position without including the top and
// bottom walls
int row = (rand() % (height - 2)) + 1;
int column = 0;
// repeat until you reach the other side of the maze
while (column < width) {
// clear the current position and then advance
maze[row][column++] = ' ';
// randomly decide whether you want to continue,
// go north or go south
int nextPosition = rand() % 3;
switch(nextPosition) {
case 0: // keep going forward
// do nothing
break;
case 1: // go north unless adjacent to the wall
if (row > 2) {
// since it's a diagonal move, clear the
// updated path
maze[row][column] = ' ';
row--;
}
break;
case 2: // go south unless adjacent to the wall
if (row < (height - 2)) {
// since it's a diagonal move, clear the
// updated path
maze[row][column] = ' ';
row++;
}
break;
default:
// never gets here
break;
}
}
}