-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMazeGenerator.cpp
161 lines (142 loc) · 4.21 KB
/
MazeGenerator.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
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
//
// Created by Marius on 20.02.2018.
//
#include "MazeGenerator.h"
#include <iostream>
#include <vector>
#include <stdlib.h>
#include <time.h>
MazeGenerator::MazeGenerator(int matrixSize, int windowWidth, int windowHeight, SDL_Renderer* renderer) {
srand (time(NULL));
//alloc 2D Array
this->matrix = new Cell*[matrixSize];
for(int i = 0; i < matrixSize; ++i) {
this->matrix[i] = new Cell[matrixSize];
}
this->scale = windowWidth/matrixSize;
//init 2D Array
for(int x = 0; x < matrixSize; x++) {
for (int y = 0; y < matrixSize; y++) {
int xPos = x*this->scale;
int yPos = y*this->scale;
this->matrix[x][y].setCell(xPos, yPos, scale, renderer);
}
}
this->MatrixSize = matrixSize;
this->renderer = renderer;
}
void MazeGenerator::GenerateMaze(){
this->currentCell = &this->matrix[0][0];
this->currentCell->setVisited(true);
Cell* next;
while(true) {
next = this->getCellNeighbour();
if(next == NULL) {
if(this->CellStack.size() > 0) {
this->currentCell = this->CellStack.top();
this->CellStack.pop();
continue;
}else{
break;
}
}
this->CellStack.push(currentCell);
this->removeWalls(currentCell, next);
currentCell = next;
this->currentCell->setVisited(true);
}
this->DrawMaze({255, 255, 255, 0});
}
void MazeGenerator::DrawMaze(SDL_Color color){
for(int x = 0; x < this->MatrixSize; x++) {
for (int y = 0; y < this->MatrixSize; y++) {
this->matrix[x][y].drawCell(color);
}
}
}
Cell* MazeGenerator::getCellNeighbour(){
int x,y;
std::tie(x, y) = this->currentCell->getIndex();
vector<Cell*> neighbours;
int neighbourCount = 0;
//Top Neighbour
if(y != 0 && !this->matrix[x][y-1].getVisited()){
neighbours.push_back(&this->matrix[x][y-1]);
neighbourCount++;
}
//Bottom Neighbour
if(y != this->MatrixSize-1 && !this->matrix[x][y+1].getVisited()){
neighbours.push_back(&this->matrix[x][y+1]);
neighbourCount++;
}
//Left Neighbour
if(x != 0 && !this->matrix[x-1][y].getVisited()){
neighbours.push_back(&this->matrix[x-1][y]);
neighbourCount++;
}
//Right Neighbour
if(x != this->MatrixSize-1 && !this->matrix[x+1][y].getVisited()){
neighbours.push_back(&this->matrix[x+1][y]);
neighbourCount++;
}
if(neighbourCount == 0)
return NULL;
//generate random index
int randIndex = rand() % neighbourCount;
//return random unvisited neighbour
return neighbours.at(randIndex);
}
void MazeGenerator::removeWalls(Cell* a, Cell* b) {
//get indices
int ax,ay;
std::tie(ax, ay)= a->getIndex();
int bx,by;
std::tie(bx, by)= b->getIndex();
//calculate delta
int deltaX = ax - bx;
int deltaY = ay - by;
//remove x walls
if(deltaX == 1){
a->setWalls("left",false);
b->setWalls("right",false);
}else if(deltaX == -1){
a->setWalls("right",false);
b->setWalls("left",false);
}
//remove y walls
if(deltaY == 1){
a->setWalls("top",false);
b->setWalls("bottom",false);
}else if(deltaY == -1){
a->setWalls("bottom",false);
b->setWalls("top",false);
}
}
void MazeGenerator::pickRandomStartEnd(){
//chooses random startpoint inside second quadrant
this->start = &this->matrix[rand() % this->MatrixSize/2][rand() % this->MatrixSize/2];
//chooses random endpoint inside fourth quadrant
this->end = &this->matrix[rand() % this->MatrixSize/2 + this->MatrixSize/2][rand() % this->MatrixSize/2 + this->MatrixSize/2];
this->start->highlight({255,0,0,255});
this->end->highlight({255,0,0,255});
}
Cell** MazeGenerator::getMatrix(){
return this->matrix;
}
int MazeGenerator::getSize(){
return this->MatrixSize;
}
Cell* MazeGenerator::getStart(){
return this->start;
}
Cell* MazeGenerator::getEnd(){
return this->end;
}
int MazeGenerator::getScale(){
return this->scale;
}
void MazeGenerator::destroy(){
for(int x = 0;x < this->MatrixSize;x++) {
delete[] this->matrix[x];
}
}