Last active
August 29, 2015 14:23
-
-
Save LusainKim/b81a3d7387a9e70ac783 to your computer and use it in GitHub Desktop.
LusMaze
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
#include <algorithm> | |
#include <iterator> | |
#include <Windows.h> | |
#include <vector> | |
using namespace std; | |
#define MazeSize 16 | |
typedef struct tagMaze{ | |
bool left; | |
bool top; | |
bool right; | |
bool bottom; | |
bool wall; | |
int route; | |
tagMaze(){ | |
route = 0; | |
left = false; | |
top = false; | |
right = false; | |
bottom = false; | |
wall = false; | |
} | |
}MazeTile; | |
MazeTile maze[MazeSize][MazeSize]; | |
void GenerateMaze(POINT& Goal) | |
{ | |
vector<POINT> route; | |
for (int i = 0; i < MazeSize * MazeSize * 0.125f + rand() % int(MazeSize * 1.5); ++i) | |
{ | |
int yadd = 0; | |
int x = rand() % (MazeSize - 2) + 1; | |
if (MazeSize * 0.3f < x && x < MazeSize * 0.8f) | |
yadd = 0; | |
else yadd = 1; | |
int y = rand() % (MazeSize - 2) + yadd; | |
if (maze[x][y].wall || (x == Goal.x && y == Goal.y) || (x == 0 && y == 0)) continue; | |
maze[x][y].wall = true; | |
} | |
POINT start = { 0, 0 }; | |
route.push_back(start); | |
int cnt = 0; | |
while (!(start.x == Goal.x && start.y == Goal.y)) | |
{ | |
cnt++; | |
bool IsAll[4] = { false, false, false, false }; | |
int IsFull; | |
Select: | |
for (int i = IsFull = 0; i < 4; ++i) | |
if (IsAll[i]) IsFull++; | |
if (IsFull == 4 || maze[start.x][start.y].route > rand() % 2 + 1 || rand() % 100 < 25) | |
{ | |
start = route[rand() % (route.size())]; | |
continue; | |
} | |
switch (rand() % 4) | |
{ | |
case 0: | |
if (start.x > 0 && !maze[start.x][start.y].left && !maze[start.x - 1][start.y].wall) | |
{ | |
maze[start.x][start.y].left = true; | |
maze[start.x][start.y].route++; | |
start.x--; | |
route.push_back(start); | |
maze[start.x][start.y].right = true; | |
maze[start.x][start.y].route++; | |
} | |
else | |
{ | |
IsAll[0] = true; | |
goto Select; | |
} | |
break; | |
case 1: | |
if (start.y > 0 && !maze[start.x][start.y].top && !maze[start.x][start.y - 1].wall) | |
{ | |
maze[start.x][start.y].top = true; | |
maze[start.x][start.y].route++; | |
start.y--; | |
route.push_back(start); | |
maze[start.x][start.y].bottom = true; | |
maze[start.x][start.y].route++; | |
} | |
else | |
{ | |
IsAll[1] = true; | |
goto Select; | |
} | |
break; | |
case 2: | |
if (start.x < MazeSize - 1 && !maze[start.x][start.y].right&& !maze[start.x + 1][start.y].wall) | |
{ | |
maze[start.x][start.y].right = true; | |
maze[start.x][start.y].route++; | |
start.x++; | |
route.push_back(start); | |
maze[start.x][start.y].left = true; | |
maze[start.x][start.y].route++; | |
} | |
else | |
{ | |
IsAll[2] = true; | |
goto Select; | |
} | |
break; | |
case 3: | |
if (start.y < MazeSize - 1 && !maze[start.x][start.y].bottom&& !maze[start.x][start.y + 1].wall) | |
{ | |
maze[start.x][start.y].bottom = true; | |
maze[start.x][start.y].route++; | |
start.y++; | |
route.push_back(start); | |
maze[start.x][start.y].top = true; | |
maze[start.x][start.y].route++; | |
} | |
else | |
{ | |
IsAll[3] = true; | |
goto Select; | |
} | |
break; | |
} | |
cout << cnt << endl; | |
// while end | |
} | |
} | |
int main() | |
{ | |
srand(timeGetTime()); | |
// POINT Goal = { rand() % (MazeSize - 1), rand() % (MazeSize - 1) }; | |
POINT Goal = { MazeSize - 1, MazeSize * 3 / 4 - rand() % (MazeSize / 2) }; | |
maze[Goal.x][Goal.y].right = true; | |
maze[0][0].top = true; | |
GenerateMaze(Goal); | |
cout << "complete!" << endl; | |
for (int y = 0; y < MazeSize; ++y) | |
{ | |
for (int sidey = 0; sidey < 3;++sidey) | |
{ | |
for (int x = 0; x < MazeSize; ++x) | |
{ | |
switch (sidey) | |
{ | |
case 0: | |
cout << "□"; | |
(maze[x][y].top) ? cout << " " : cout << "□"; | |
cout << "□"; | |
break; | |
case 1: | |
(maze[x][y].left) ? cout << " " : cout << "□"; | |
if (x == Goal.x && y == Goal.y) | |
cout << "※"; | |
else | |
(maze[x][y].left || maze[x][y].top || maze[x][y].right || maze[x][y].bottom) ? cout << " " : cout << "□"; | |
(maze[x][y].right) ? cout << " " : cout << "□"; | |
break; | |
case 2: | |
cout << "□"; | |
(maze[x][y].bottom) ? cout << " " : cout << "□"; | |
cout << "□"; | |
break; | |
} | |
} | |
cout << endl; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment