-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
270 lines (228 loc) · 7.05 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
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
#include <SFML/Graphics.hpp>
#include <stack>
#include <iostream>
#include <random>
#define SIZE 30
#define CELL_WIDTH 20
class Cell{
public:
int x,y;
int pos;
float size = 30.f;
float thickness = 2.f;
bool walls[4] = {true,true,true,true};
bool visited = false;
bool isActive = false;
Cell();
Cell(int,int);
void draw(sf::RenderWindow *window);
};
Cell::Cell(){}
Cell::Cell(int _x,int _y){
x = _x;
y = _y;
}
void Cell::draw(sf::RenderWindow *window){
sf::RectangleShape rect;
if(isActive){
rect.setFillColor(sf::Color(247, 23, 53));
rect.setSize(sf::Vector2f(size, size));
rect.setPosition(x,y);
window->draw(rect);
}
rect.setFillColor(sf::Color(223,243,228));
if(walls[0]){
rect.setSize(sf::Vector2f(size, thickness));
rect.setPosition(x,y);
window->draw(rect);
}
// right
if(walls[1]){
rect.setSize(sf::Vector2f(thickness, size));
rect.setPosition(x+size,y);
window->draw(rect);
}
// bottom
if(walls[2]){
rect.setSize(sf::Vector2f(size+thickness, thickness));
rect.setPosition(x,y+size);
window->draw(rect);
}
// left
if(walls[3]){
rect.setSize(sf::Vector2f(thickness, size));
rect.setPosition(x,y);
window->draw(rect);
}
}
void resetMaze(Cell *maze,int size){
for (int i = 0; i < size*size; i++) {
for (int j = 0; j < 4; j++) {
maze[i].walls[j] = true;
maze[i].visited = false;
maze[i].isActive = false;
}
}
}
void removeWallsBetween(Cell *maze,Cell *current,Cell *chosen,int size) {
// top
if(current->pos-size == chosen->pos){
current->walls[0] = false;
chosen->walls[2] = false;
// right
} else if(current->pos+1 == chosen->pos){
current->walls[1] = false;
chosen->walls[3] = false;
// bottom
} else if(current->pos+size == chosen->pos){
current->walls[2] = false;
chosen->walls[0] = false;
// left
} else if(current->pos-1 == chosen->pos){
current->walls[3] = false;
chosen->walls[1] = false;
}
}
void makeMaze(Cell *maze,int size){
resetMaze(maze,size);
std::stack<Cell> stack;
maze[0].visited = true;
stack.push(maze[0]);
while(!stack.empty()){
Cell current = stack.top();
stack.pop();
int pos = current.pos;
std::vector<int> neighbours;
if((pos) % (size) != 0 && pos > 0){
Cell left = maze[pos-1];
if(!left.visited){
neighbours.push_back(pos-1);
}
}
if((pos+1) % (size) != 0 && pos < size * size){
Cell right = maze[pos+1];
if(!right.visited){
neighbours.push_back(pos+1);
}
}
if((pos+size) < size*size){
Cell bottom = maze[pos+size];
if(!bottom.visited){
neighbours.push_back(pos+size);
}
}
if((pos-size) > 0){
Cell top = maze[pos-size];
if(!top.visited){
neighbours.push_back(pos-size);
}
}
if(neighbours.size() > 0){
// generate a random array index for selecting a neighbour
std::random_device dev;
std::mt19937 rng(dev());
std::uniform_int_distribution<std::mt19937::result_type> dist6(0,neighbours.size()-1);
int randneighbourpos = dist6(rng);
Cell *chosen = &maze[neighbours[randneighbourpos]];
stack.push(current);
removeWallsBetween(maze,&maze[current.pos],chosen,size);
chosen->visited = true;
stack.push(*chosen);
}
}
}
void handleMove(sf::Event event,Cell *maze,int *currentPos,int size){
if(
event.key.code == sf::Keyboard::Left ||
event.key.code == sf::Keyboard::H
){
if(!maze[*currentPos].walls[3] && !maze[*currentPos-1].walls[1]){
*currentPos = *currentPos -1;
maze[*currentPos].isActive = true;
}
} else if(
event.key.code == sf::Keyboard::Right ||
event.key.code == sf::Keyboard::L
){
if(!maze[*currentPos].walls[1] && !maze[*currentPos+1].walls[3]){
*currentPos = *currentPos +1;
maze[*currentPos].isActive = true;
}
} else if(
event.key.code == sf::Keyboard::Up ||
event.key.code == sf::Keyboard::K
){
if((*currentPos-size) < 0){
return;
}
if(!maze[*currentPos].walls[0] && !maze[*currentPos-size].walls[2]){
*currentPos = *currentPos - size;
maze[*currentPos].isActive = true;
}
} else if(
event.key.code == sf::Keyboard::Down ||
event.key.code == sf::Keyboard::J
){
if((*currentPos+size) > size*size){
return;
}
if(!maze[*currentPos].walls[2] && !maze[*currentPos+size].walls[0]){
*currentPos = *currentPos + size;
maze[*currentPos].isActive = true;
}
}
}
int main(int argc, char *argv[]){
int currentPos = 0;
sf::RenderWindow window(sf::VideoMode(CELL_WIDTH*SIZE+60, CELL_WIDTH*SIZE+60), "maze");
Cell *maze = new Cell[SIZE*SIZE];
window.setFramerateLimit(30);
//window.setVerticalSyncEnabled(true);
for (int i = 30,k = 0; i < CELL_WIDTH*SIZE+30; i+=CELL_WIDTH) {
for (int j = 30; j < CELL_WIDTH*SIZE+30; j+=CELL_WIDTH,k++) {
maze[k].y = i;
maze[k].x = j;
maze[k].size = CELL_WIDTH;
maze[k].pos = k;
}
}
makeMaze(maze,SIZE);
maze[currentPos].isActive = true;
sf::RectangleShape currentPosRect;
currentPosRect.setFillColor(sf::Color(166, 207, 213));
currentPosRect.setSize(sf::Vector2f(CELL_WIDTH, CELL_WIDTH));
sf::RectangleShape finishRect;
finishRect.setFillColor(sf::Color(0, 128, 0));
finishRect.setSize(sf::Vector2f(CELL_WIDTH, CELL_WIDTH));
while (window.isOpen()){
sf::Event event;
while (window.pollEvent(event)){
switch (event.type){
case sf::Event::Closed:
window.close();
break;
case sf::Event::KeyPressed:
handleMove(event,maze,¤tPos,SIZE);
break;
default:
break;
}
}
if(currentPos==(SIZE*SIZE-1)){
makeMaze(maze,SIZE);
currentPos = 0;
maze[currentPos].isActive = true;
}
window.clear(sf::Color(13,2,33));
for (int i = 0; i < SIZE * SIZE; i++) {
maze[i].draw(&window);
}
currentPosRect.setPosition(maze[currentPos].x,maze[currentPos].y);
window.draw(currentPosRect);
finishRect.setPosition(maze[SIZE*SIZE-1].x,maze[SIZE*SIZE-1].y);
window.draw(finishRect);
window.display();
}
delete []maze;
return 0;
}