-
Notifications
You must be signed in to change notification settings - Fork 0
/
Maze.cpp
134 lines (116 loc) · 3.68 KB
/
Maze.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
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
#include "Maze.h"
#include<stack>
#include<string>
#include<random>
Maze::Maze(unsigned short _w,unsigned short _h):width{_w},height{_h}
{
for(unsigned short i{} ; i<height ; i++){
myCells.push_back(std::vector<cell*>());
for(unsigned short j{} ; j<width ; j++)
{
myCells[i].push_back(new cell(i,j));
}
}
}
Maze::Maze():Maze(2,2)
{
}
Maze::Maze(Maze& cM):
notVisitedColor{cM.notVisitedColor}
,VisitedColor{cM.VisitedColor}
,startColor{cM.startColor}
,endColor{cM.endColor}
,wallColor{cM.wallColor}
,wallWidth{cM.wallWidth}
,width{cM.width},height{cM.height},cellsSize{cM.cellsSize}
{
for(unsigned short i{}; i<myCells.size(); i++)
{
for(unsigned short j{}; j<myCells[0].size(); j++)
{
this->myCells[i][j]=new cell{*cM.myCells[i][j]};
}
}
}
Maze::~Maze()
{
for(unsigned short i{}; i<myCells.size(); i++)
{
for(unsigned short j{}; j<myCells[0].size(); j++)
{
delete myCells[i][j];
}
}
}
Maze& Maze::operator=(const Maze &cM)
{
notVisitedColor=cM.notVisitedColor;
VisitedColor=cM.VisitedColor;
startColor=cM.startColor;
endColor=cM.endColor;
wallColor=cM.wallColor;
wallWidth=cM.wallWidth;
width=cM.width;height=cM.height;cellsSize=cM.cellsSize;
for(unsigned short i{}; i<myCells.size(); i++)
{
for(unsigned short j{}; j<myCells[0].size(); j++)
{
this->myCells[i][j]=new cell{*cM.myCells[i][j]};
}
}
return *this;
}
const Maze* Maze::creatMaze() const
{
//recursive backtaracker algorithm
std::default_random_engine rEng(time(0));
std::stack<cell*> cellStack;
unsigned int i{0},j{0};
cellStack.push(myCells[i][j]);
while(cellStack.size()!=0)
{
cellStack.top()->myStatus.visited=true;
i=cellStack.top()->x_num;
j=cellStack.top()->y_num;
std::vector<std::pair<cell*,std::string>> otherCells{};
if(this->cellExists(i,j-1))
if(!this->myCells[i][j-1]->myStatus.visited)
otherCells.push_back(std::pair<cell*,std::string>(myCells[i][j-1],"up"));
if(this->cellExists(i-1,j))
if(!this->myCells[i-1][j]->myStatus.visited)
otherCells.push_back(std::pair<cell*,std::string>(myCells[i-1][j],"left"));
if(this->cellExists(i,j+1))
if(!this->myCells[i][j+1]->myStatus.visited)
otherCells.push_back(std::pair<cell*,std::string>(myCells[i][j+1],"down"));
if(this->cellExists(i+1,j))
if(!this->myCells[i+1][j]->myStatus.visited)
otherCells.push_back(std::pair<cell*,std::string>(myCells[i+1][j],"right"));
if(otherCells.empty())
cellStack.pop();
else
{
std::uniform_int_distribution<size_t> randomNum(0,otherCells.size()-1);
auto nextCell=otherCells[randomNum(rEng)];
if(nextCell.second=="up"){
cellStack.top()->myWalls.up=false;
nextCell.first->myWalls.down=false;
}
if(nextCell.second=="left"){
cellStack.top()->myWalls.left=false;
nextCell.first->myWalls.right=false;
}
if(nextCell.second=="down"){
cellStack.top()->myWalls.down=false;
nextCell.first->myWalls.up=false;
}
if(nextCell.second=="right"){
cellStack.top()->myWalls.right=false;
nextCell.first->myWalls.left=false;
}
cellStack.push(nextCell.first);
}
}
myCells[0][0]->myStatus.isStart=true;
myCells[myCells.size()-1][myCells[0].size()-1]->myStatus.isEnd=true;
return this;
}