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
|
#include<iostream> //"cout"
#include<conio.h> //"getch()" and "kbhit()"
#include <windows.h> //Lots of useful things
#include<time.h> // "time"
#include "color.h" // color text header file
#define MAX 61
#define CELL 900
#define WALL 1
#define PATH 0
// Wall Integers
void init_maze(int maze[MAX][MAX]);
void maze_generator(int indeks, int maze[MAX][MAX], int backtrack_x[CELL], int bactrack_y[CELL], int x, int y, int n, int visited);
void print_maze(int maze[MAX][MAX], int maze_size);
int is_closed(int maze[MAX][MAX], int x, int y);
using namespace std;
int main(void)
{
HANDLE hOut;
hOut = GetStdHandle(STD_OUTPUT_HANDLE);
srand((unsigned)time(NULL));
int size;
int indeks = 0;
brightYellow;
cout << "MAZE GENERATOR" << endl;
cout << "Please enter a number (0 - 30): " << endl;
brightWhite;
cin >> size;
brightWhite;
int maze[MAX][MAX];
int backtrack_x[CELL];
int backtrack_y[CELL];
init_maze(maze);
backtrack_x[indeks] = 1;
backtrack_y[indeks] = 1;
maze_generator(indeks, maze, backtrack_x, backtrack_y, 1, 1, size, 1);
print_maze(maze, size);
getch();
return 0;
}
void init_maze(int maze[MAX][MAX])
{
for(int a = 0; a < MAX; a++)
{
for(int b = 0; b < MAX; b++)
{
if(a % 2 == 0 || b % 2 == 0)
maze[a][b] = 1;
else
maze[a][b] = PATH;
}
}
}
void maze_generator(int indeks, int maze[MAX][MAX], int backtrack_x[CELL], int backtrack_y[CELL], int x, int y, int n, int visited)
{
if(visited < n * n)
{
int neighbour_valid = -1;
int neighbour_x[4];
int neighbour_y[4];
int step[4];
int x_next;
int y_next;
// check walls
if(x - 2 > 0 && is_closed(maze, x - 2, y)) // upside of wall
{
neighbour_valid++;
neighbour_x[neighbour_valid]=x - 2;;
neighbour_y[neighbour_valid]=y;
step[neighbour_valid]=1;
}
if(y - 2 > 0 && is_closed(maze, x, y - 2)) // leftside of wall
{
neighbour_valid++;
neighbour_x[neighbour_valid]=x;
neighbour_y[neighbour_valid]=y - 2;
step[neighbour_valid]=2;
}
if(y + 2 < n * 2 + 1 && is_closed(maze, x, y + 2)) // rightside of wall
{
neighbour_valid++;
neighbour_x[neighbour_valid]=x;
neighbour_y[neighbour_valid]=y + 2;
step[neighbour_valid]=3;
}
if(x + 2 < n * 2 + 1 && is_closed(maze, x + 2, y)) // downside of wall
{
neighbour_valid++;
neighbour_x[neighbour_valid]=x+2;
neighbour_y[neighbour_valid]=y;
step[neighbour_valid]=4;
}
if(neighbour_valid == -1)
{
x_next = backtrack_x[indeks];
y_next = backtrack_y[indeks];
indeks--;
}
if(neighbour_valid!=-1)
{
int randomization = neighbour_valid + 1;
int random = rand()%randomization;
x_next = neighbour_x[random];
y_next = neighbour_y[random];
indeks++;
backtrack_x[indeks] = x_next;
backtrack_y[indeks] = y_next;
int rstep = step[random];
if(rstep == 1)
maze[x_next+1][y_next] = PATH;
else if(rstep == 2)
maze[x_next][y_next + 1] = PATH;
else if(rstep == 3)
maze[x_next][y_next - 1] = PATH;
else if(rstep == 4)
maze[x_next - 1][y_next] = PATH;
visited++;
}
maze_generator(indeks, maze, backtrack_x, backtrack_y, x_next, y_next, n, visited);
}
}
// print the maze walls based on size
void print_maze(int maze[MAX][MAX], int maze_size)
{
for(int a = 0; a < maze_size * 2 + 1; a++)
{
for(int b = 0; b < maze_size * 2 + 1; b++)
{
if(maze[a][b] == WALL)
cout << "#" ; // if wall generate #
else
cout << " " ; // if empty space generate " "
}
cout << endl; // start a new line for the maze
}
}
int is_closed(int maze[MAX][MAX], int x, int y)
{
if(maze[x - 1][y] == WALL
&& maze[x][y - 1] == WALL
&& maze[x][y + 1] == WALL
&& maze[x + 1][y] == WALL
)
return 1;
return 0;
}
|