-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
77 lines (71 loc) · 2.88 KB
/
main.cpp
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
#include <iostream>
#include <Cell.hpp>
#include <Grid.hpp>
#include <unistd.h>
using namespace std;
std::vector<Cell> stack;
int main(int numbers,char** args){
char mode = args[1][0];
int level = 0;
bool isFinal = false;
switch(mode){
case '1': level = EASY; break;
case '2': level = MEDIUM; break;
case '3': level = HARD; break;
default: break;
}
try{
Event evt;
RenderWindow window(VideoMode(level*CELLWIDTH,level*CELLWIDTH),"Maze Generator",Style::Close);
RenderTexture texture;
texture.create(level*CELLWIDTH,level*CELLWIDTH);
GridInit(level,texture);
Cell current = grid[0], previous(0,0);
grid[index(current.getX(),current.getY(),level)].isActive = true;
grid[index(current.getX(),current.getY(),level)].isVisited = true;
while(window.isOpen()){
while(window.pollEvent(evt)){
if(evt.type == Event::Closed){
window.close();
cout<<"Window Closed"<<endl;
Image img = texture.getTexture().copyToImage();
img.saveToFile("./Maze.png");
cout<<"Image exported Successfully"<<endl;
return EXIT_SUCCESS;
}
}
if(current.getX()>=0 && current.getY()>=0){
previous = current;
current = RandNeighbour(current,level);
if(current.getX()>=0 && current.getY()>=0){
//Remove wall
RemoveWall(grid[index(previous.getX(),previous.getY(),level)],
grid[index(current.getX(),current.getY(),level)]);
grid[index(previous.getX(),previous.getY(),level)].isActive = false;
grid[index(previous.getX(),previous.getY(),level)].isVisited = true;
grid[index(current.getX(),current.getY(),level)].isActive = true;
grid[index(current.getX(),current.getY(),level)].isVisited = true;
stack.push_back(previous);
}else if(stack.size()!=0){
current = stack[stack.size()-1];
grid[index(previous.getX(),previous.getY(),level)].isActive = false;
grid[index(current.getX(),current.getY(),level)].isActive = true;
stack.pop_back();
}
} else if(!isFinal){
grid[0].isActive = false;
cout<<"Maze generation complete"<<endl;
isFinal = true;
}
usleep(50000);
window.clear();
DrawGrid(texture);
texture.display();
window.draw(Sprite(texture.getTexture()));
window.display();
}
}catch(exception exc){
cerr<<exc.what()<<endl;
return EXIT_FAILURE;
}
}