Random Maze Generator

I am struggling quite a bit with this recent assignment.

How would I go about randomly choosing to remove the south or the west wall for each cell going from left to right top to bottom.


I have a working code but I brute forced myself into finishing it. I was wondering how I could optimize it to make my code cleaner.

#include <iostream>
#include <ctime>
#include <cmath>
#include <iomanip>
using namespace std;
int main() {

	int number = 5;
	char plus = '+';
	char dash = '-';
	char wall = '|';


	for (int i = 1; i <= number; ++i) {
		cout << plus << dash;
	}
	cout << plus << endl;

	//collumn 2
	int num1;
	char wall1 = '|';
	char dash1 = '-';
	srand(time(NULL));
	num1 = rand() % 2;
	if (num1 == 1) {
		wall1 = '|';
		dash1 = ' ';
	}
	else {
		dash1 = '-';
		wall1 = ' ';
	}

	//collumn 3
	int num2;
	char wall2 = '|';
	char dash2 = '-';
	srand(time(NULL));
	num2 = rand() % 4 + 3;
	if (num2 == 3) {
		wall2 = '|';
		dash2 = ' ';
	}
	else {
		dash2 = '-';
		wall2 = ' ';
	}

	//collumn 4
	int num3;
	char wall3 = '|';
	char dash3 = '-';
	srand(time(NULL));
	num3 = rand() % 6 + 5;
	if (num3 == 5) {
		wall3 = '|';
		dash3 = ' ';
	}
	else {
		dash3 = '-';
		wall3 = ' ';
	}

	//collumn 5
	int num4;
	char wall4 = '|';
	char dash4 = '-';
	srand(time(NULL));
	num4 = rand() % 8 + 7;
	if (num4 == 8) {
		wall4 = '|';
		dash4 = ' ';
	}
	else {
		dash4 = '-';
		wall4 = ' ';
	}

	//first row
	cout << wall << setw(2) << wall1 << setw(2) << wall2 << setw(2) << wall3 << setw(2) << wall4 << setw(2) << wall << endl;

	cout << plus << " " << plus << dash1 << plus << dash2 << plus << dash3 << plus << dash4 << plus << endl;

	//collumn 2
	int num5;
	char wall5 = '|';
	char dash5 = '-';
	srand(time(NULL));
	num5 = rand() % 10+ 9;	//CHANGE
	if (num5 == 9) {	//CHANGE
		wall5 = '|';
		dash5 = ' ';
	}
	else {
		dash5 = '-';
		wall5 = ' ';
	}

	//collumn 3
	int num6;
	char wall6 = '|';
	char dash6 = '-';
	srand(time(NULL));
	num6 = rand() % 12 + 11;	//CHANGE
	if (num6 == 11) {	//CHANGE
		wall6 = '|';
		dash6 = ' ';
	}
	else {
		dash6 = '-';
		wall6 = ' ';
	}

	//collumn 4
	int num7;
	char wall7 = '|';
	char dash7 = '-';
	srand(time(NULL));
	num7 = rand() % 14 + 13;	//CHANGE
	if (num7 == 13) {	//CHANGE
		wall7 = '|';
		dash7 = ' ';
	}
	else {
		dash7 = '-';
		wall7 = ' ';
	}

	//collumn 5
	int num8;
	char wall8 = '|';
	char dash8 = '-';
	srand(time(NULL));
	num8 = rand() % 16 + 15;	//CHANGE
	if (num8 == 15) {	//CHANGE
		wall8 = '|';
		dash8 = ' ';
	}
	else {
		dash8 = '-';
		wall8 = ' ';
	}

	//second row
	cout << wall << setw(2) << wall5 << setw(2) << wall6 << setw(2) << wall7 << setw(2) << wall8 << setw(2) << wall <<  endl;

	cout << plus << " " << plus << dash5 << plus << dash6 << plus << dash7 << plus << dash8 << plus << endl;

	//collumn 2
	int num9;
	char wall9 = '|';
	char dash9 = '-';
	srand(time(NULL));
	num9 = rand() % 18 + 17;	//CHANGE
	if (num9 == 17) {	//CHANGE
		wall9 = '|';
		dash9 = ' ';
	}
	else {
		dash9 = '-';
		wall9 = ' ';
	}

	//collumn 3
	int num10;
	char wall10 = '|';
	char dash10 = '-';
	srand(time(NULL));
	num10 = rand() % 20 + 19;	//CHANGE
	if (num10 == 19) {	//CHANGE
		wall10 = '|';
		dash10 = ' ';
	}
	else {
		dash10 = '-';
		wall10 = ' ';
	}

	//collumn 4
	int num11;
	char wall11 = '|';
	char dash11 = '-';
	srand(time(NULL));
	num11 = rand() % 24 + 23;	//CHANGE
	if (num11 == 23) {	//CHANGE
		wall11 = '|';
		dash11 = ' ';
	}
	else {
		dash11 = '-';
		wall11 = ' ';
	}

	//collumn 5
	int num12;
	char wall12 = '|';
	char dash12 = '-';
	srand(time(NULL));
	num12 = rand() % 26 + 25;	//CHANGE
	if (num12 == 25) {	//CHANGE
		wall12 = '|';
		dash12 = ' ';
	}
	else {
		dash12 = '-';
		wall12 = ' ';
	}

	//third row
	cout << wall << setw(2) << wall9 << setw(2) << wall10 << setw(2) << wall11 << setw(2) << wall12 << setw(2) << wall << endl;

	cout << plus << " " << plus << dash9 << plus << dash10 << plus << dash11 << plus << dash12 << plus << endl;

	//collumn 2
	int num13;
	char wall13 = '|';
	char dash13 = '-';
	srand(time(NULL));
	num13 = rand() % 28 + 27;	//CHANGE
	if (num13 == 28) {	//CHANGE
		wall13 = '|';
		dash13 = ' ';
	}
	else {
		dash13 = '-';
		wall13 = ' ';
	}

	//collumn 3
	int num14;
	char wall14 = '|';
	char dash14 = '-';
	srand(time(NULL));
	num14 = rand() % 30 + 29;	//CHANGE
	if (num14 == 29) {	//CHANGE
		wall14 = '|';
		dash14 = ' ';
	}
	else {
		dash14 = '-';
		wall14 = ' ';
	}

	//collumn 4
	int num15;
	char wall15 = '|';
	char dash15 = '-';
	srand(time(NULL));
	num15 = rand() % 32+ 31;	//CHANGE
	if (num15 == 31) {	//CHANGE
		wall15 = '|';
		dash15 = ' ';
	}
	else {
		dash15 = '-';
		wall15 = ' ';
	}

	//collumn 5
	int num16;
	char wall16 = '|';
	char dash16 = '-';
	srand(time(NULL));
	num16 = rand() % 34 + 33;	//CHANGE
	if (num16 == 33) {	//CHANGE
		wall16 = '|';
		dash16 = ' ';
	}
	else {
		dash16 = '-';
		wall16 = ' ';
	}

	//fourth row
	cout << wall << setw(2) << wall13 << setw(2) << wall14 << setw(2) << wall15 << setw(2) << wall16 << setw(2) << wall << endl;

	cout << plus << " " << plus << dash13 << plus << dash14 << plus << dash15 << plus << dash16 << plus << endl;

	//fifth row
	cout << wall << setw(2) << " " << setw(2) << " " <<setw(2) << " " << setw(2) << " " << " " << wall << endl;

	cout << plus << dash << plus << dash << plus << dash << plus << dash << plus << dash << plus << endl;




	return 0
;
}
Last edited on
@barcandy

Your code is (a) hugely repetitive; (b) absolutely tied to one size of maze. You can remedy both by completely rewriting your code using loops and arrays.

I suggest (take it as you will) that you:
- declare global constants ACROSS and DOWN to dictate the number of cells in each direction;
- declare global arrays edgeW[DOWN][ACROSS] and edgeS[DOWN][ACROSS] to hold the current status of west and south walls for each cell.


Suggested outline:
- initialise your arrays holding the state of the walls;

- draw the initial maze;

- nested loops running across and down - within this nested loop, knock down a wall according to the rules:
- left-most: knock down a south wall (unless at bottom of maze);
- bottom: knock down a west wall (unless at left of maze);
- otherwise: randomly choose either 0 or 1 using rand() % 2 and knock down a south or west wall accordingly;

- draw the final maze.


I suggest that you have a separate function to draw the maze.


You only need to call the seeding function srand() once in your entire program. The random number generator rand() can be called when you need it.

+-+-+-+-+-+-+-+-+
| | |       |   |
+ + + +-+-+-+ +-+
| |   |     |   |
+ + +-+ +-+-+ +-+
|   | |   |     |
+ +-+ + +-+ +-+-+
| |   | | | | | |
+ + +-+ + + + + +
|               |
+-+-+-+-+-+-+-+-+





Last edited on
@lastchance

How do I nest loops on something like this?
Once I draw out the maze how can I knock down walls inside?

I'm new to programming sorry
How do I nest loops on something like this?

Nested loops? Think about your 24-hour digital watch. The minutes digits run from 0 to 59 for each of the hours running from 0 to 23. That's a minutes loop nesting inside an hours loop. (You can even nest a third loop for seconds inside if you want).

Here, to consider all the "cells" in the maze you are looping across all your columns inside a rows loop which goes down the page. Something like:
1
2
3
4
5
6
7
   for ( int d = 0; d < DOWN; d++ )
   {
      for ( int a = 0; a < ACROSS; a++ )
      {
         // Your code for changing walls goes here
      }
   }



Once I draw out the maze how can I knock down walls inside?

You don't knock down the walls from your drawing, you simply change the character variable representing that wall from '|' (for vertical walls) to ' ' (blank). It's a lot less messy than bricks and mortar. When you draw your final maze using these character variables then that position will be blank.

You don't have to draw the undepleted maze at the start if you don't want to - I only suggested it so that you can check variables before starting dismantling walls.


Look at your code. If you see variables named wall1, wall2, wall3, wall4, ... wall16 then you really ought to be using arrays. See: http://www.cplusplus.com/doc/tutorial/arrays/

If you keep repeating the same block of code with maybe a simple change of parameters, then use loops; see: http://www.cplusplus.com/doc/tutorial/control/

Topic archived. No new replies allowed.