-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGeneration.cpp
More file actions
89 lines (78 loc) · 3.33 KB
/
Copy pathGeneration.cpp
File metadata and controls
89 lines (78 loc) · 3.33 KB
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
#include "Globals.hpp"
#include "Generation.hpp"
void wall(int mazeWidth, int mazeHeight, int m_totalVisitedCells, std::vector<std::vector<int>> &m_connectedAdjList, std::pair<float, float> (&cell_coordinates)[1000][1000], std::vector<std::pair<FLOAT_PAIR, int>> &walls)
{
srand(time(NULL));
std::vector <std::vector <int>> m_visited(mazeWidth, std::vector<int> (mazeHeight, 0));
std::stack<std::pair<int, int>> m_stack;
m_stack.push({0,0});
m_visited[0][0] = 1;
m_totalVisitedCells++;
while(!m_stack.empty())
{
std::vector<int> neighbours;
//right neighbour - 0 - {m_stack.top().first + 1, m_stack.top().second}
if (m_stack.top().first + 1 < mazeWidth && m_visited[m_stack.top().first + 1][m_stack.top().second] != 1)
{
neighbours.push_back(0);
}
//up neighbour - 1 {m_stack.top().first, m_stack.top().second - 1}
if (m_stack.top().second - 1 >= 0 && m_visited[m_stack.top().first][m_stack.top().second - 1] != 1)
{
neighbours.push_back(1);
}
//left neighbour - 2 {m_stack.top().first - 1, m_stack.top().second}
if (m_stack.top().first - 1 >= 0 && m_visited[m_stack.top().first - 1][m_stack.top().second] != 1)
{
neighbours.push_back(2);
}
//down neighbour - 3 {m_stack.top().first, m_stack.top().second + 1}
if (m_stack.top().second + 1 < mazeHeight && m_visited[m_stack.top().first][m_stack.top().second + 1] != 1)
{
neighbours.push_back(3);
}
if (!neighbours.empty())
{
int randomCell = neighbours[rand() % neighbours.size()];
int randomCellx;
int randomCelly;
if (randomCell == 0)
{
randomCellx = m_stack.top().first + 1;
randomCelly = m_stack.top().second;
}
if (randomCell == 1)
{
randomCellx = m_stack.top().first;
randomCelly = m_stack.top().second - 1;
}
if (randomCell == 2)
{
randomCellx = m_stack.top().first - 1;
randomCelly = m_stack.top().second;
}
if (randomCell == 3)
{
randomCellx = m_stack.top().first;
randomCelly = m_stack.top().second + 1;
}
/*---------------------------------added by PRITOMASH---------------------------------------*/
//to link data with Solver algorithm
int currentCell = xyToInt(m_stack.top().first, m_stack.top().second, mazeWidth);
int neighbourCell = xyToInt(randomCellx, randomCelly, mazeWidth);
m_connectedAdjList[currentCell].push_back(neighbourCell);
m_connectedAdjList[neighbourCell].push_back(currentCell);
float cx = (cell_coordinates[randomCellx][randomCelly]).first;
float cy = (cell_coordinates[randomCellx][randomCelly]).second;
walls.push_back({std::make_pair(cx, cy), randomCell});
/*---------------------------------end of PRITOMASH edit-----------------------------------*/
m_stack.push({randomCellx, randomCelly});
m_visited[randomCellx][randomCelly] = 1;
m_totalVisitedCells++;
}
else
{
m_stack.pop();
}
}
}