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>
#include <windows.h>
#include <ctime>
using namespace std;
#define MAX 81
#define CELL MAX*MAX
#define PATH 0
#define WALL 1
void initMaze(int maze[MAX][MAX], int n);
void genMaze(int index, int maze[MAX][MAX], int btX[CELL], int btY[CELL], int x, int y, int n, int visited );
bool isClosed(int maze[MAX][MAX], int x, int y);
void gotoxy(int x,int y);
void printMaze(int maze[MAX][MAX],int n);
int main()
{
system("mode 150");
srand((unsigned)time(NULL));
int maze[MAX][MAX];
int btX[CELL],btY[CELL];
int index = 0,n;
cout << "Maze dimension(0-40): "; cin >> n;
btX[index] = 1;
btY[index] = 1;
initMaze(maze, n);
genMaze(index, maze, btX, btY, 1, 1, n, 1);
cin.ignore(1, ' ');
}
void gotoxy(int x,int y)
{
COORD pos;
pos.X = x;
pos.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void initMaze(int maze[MAX][MAX], int n)
{
for(int a = 0; a < MAX; a++)
for(int b = 0; b < MAX; b++)
{
if(a % 2 == 0 || b % 2 == 0)
{
maze[a][b] = WALL;
if(a <= n*2 && b <= n*2)
{
gotoxy(a, b);
cout << char(219);
}
}
else
maze[a][b] = PATH;
}
}
void genMaze(int index, int maze[MAX][MAX], int btX[CELL], int btY[CELL], int x, int y, int n, int visited )
{
if( visited < n * n)
{
int neighbourV = -1;
int nX[4],nY[4],step[4];
int nextX,nextY;
if( x - 2 > 0 && isClosed(maze, x - 2, y)) //upside
{
neighbourV++;
nX[neighbourV] = x - 2;
nY[neighbourV] = y;
step[neighbourV] = 1;
}
if( y - 2 > 0 && isClosed(maze, x, y - 2)) //leftside
{
neighbourV++;
nX[neighbourV] = x;
nY[neighbourV] = y - 2;
step[neighbourV] = 2;
}
if( y + 2 < n * 2 + 1 && isClosed(maze, x, y + 2) ) //rightside
{
neighbourV++;
nX[neighbourV] = x;
nY[neighbourV] = y + 2;
step[neighbourV] = 3;
}
if( x + 2 < n * 2 + 1 && isClosed(maze, x + 2, y)) //downside
{
neighbourV++;
nX[neighbourV] = x + 2;
nY[neighbourV] = y;
step[neighbourV] = 4;
}
if(neighbourV == -1)
{
nextX = btX[index];
nextY = btY[index];
index--;
}
if(neighbourV != -1)
{
int randomization = neighbourV + 1;
int random = rand()%randomization;
nextX = nX[random];
nextY = nY[random];
index++;
btX[index] = nextX;
btY[index] = nextY;
int rstep = step[random];
if(rstep == 1)
{
maze[nextX + 1][nextY] = PATH;
gotoxy(nextX + 1, nextY);
cout << ' ';
Sleep(30);
}
else
if(rstep == 2)
{
maze[nextX][nextY + 1] = PATH;
gotoxy(nextX, nextY + 1);
cout << ' ';
Sleep(30);
}
else
if(rstep == 3)
{
maze[nextX][nextY - 1] = PATH;
gotoxy(nextX, nextY - 1);
cout << ' ';
Sleep(30);
}
else
if(rstep == 4)
{
maze[nextX - 1][nextY] = PATH;
gotoxy(nextX - 1, nextY);
cout << ' ';
Sleep(30);
}
visited++;
}
genMaze(index, maze, btX, btY, nextX, nextY, n, visited);
}
}
bool isClosed(int maze[MAX][MAX], int x, int y)
{
if(maze[x+1][y] == WALL &&
maze[x-1][y] == WALL &&
maze[x][y+1] == WALL &&
maze[x][y-1] == WALL)
return true;
else
return false;
}
|