-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
431 lines (385 loc) · 9 KB
/
Copy pathmain.cpp
File metadata and controls
431 lines (385 loc) · 9 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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
#include <iostream>
#include <random>
#include <thread>
#include <chrono>
#include <SFML/Graphics.hpp>
struct position
{
int x, y;
};
class cell
{
private:
//Cell Position
position p;
//Toggle for whether to draw the line along each direction
bool drawUp = true, drawLeft = true, drawDown = true, drawRight = true;
//Array containing the upper, left, lower, and right lines
sf::RectangleShape lines[4];
//Variables controlling highlight status
sf::Color highlightColor = sf::Color::Blue;
bool isHighlighted = false;
//Whether or not the cell has been visited by the navigator already
bool hasBeenVisited = false;
public:
cell(int setX, int setY, int cellLength, int lineWidth);
//Configures SFML shapes
void configLine(int di, int cellLength, int lineWidth);
//Returns a copy of the cell created
sf::RectangleShape returnLine (int d);
sf::RectangleShape returnHighlight(int cellLength);
//Returns/sets whether the specified direction is to be drawn
bool returnDrawStatus (int d);
void setDrawStatus (int d, bool status);
void deleteLine (int d);
void setHighlighted(bool status);
void setHighlightColor(sf::Color color);
bool returnHighlightStatus();
bool returnVisited();
void setVisited(bool status);
};
bool cell::returnVisited()
{
return hasBeenVisited;
}
void cell::setVisited(bool status)
{
hasBeenVisited = status;
}
bool cell::returnHighlightStatus()
{
return isHighlighted;
}
void cell::setHighlighted(bool status)
{
isHighlighted = status;
}
void cell::setHighlightColor(sf::Color color)
{
highlightColor = color;
}
void cell::deleteLine (int d)
{
setDrawStatus(d, false);
}
void cell::setDrawStatus (int d, bool status)
{
if (d == 0)
{
drawUp = status;
}
else if (d == 1)
{
drawLeft = status;
}
else if (d == 2)
{
drawDown = status;
}
else if (d == 3)
{
drawRight = status;
}
}
bool cell::returnDrawStatus (int d)
{
if (d == 0)
{
return drawUp;
}
else if (d == 1)
{
return drawLeft;
}
else if (d == 2)
{
return drawDown;
}
else if (d == 3)
{
return drawRight;
}
}
cell::cell (int setX, int setY, int cellLength, int lineWidth)
{
p.x = setX;
p.y = setY;
configLine(0, cellLength, lineWidth);
configLine(1, cellLength, lineWidth);
configLine(2, cellLength, lineWidth);
configLine(3, cellLength, lineWidth);
}
sf::RectangleShape cell::returnLine (int d)
{
if (d == 0)
{
return lines[0];
}
else if (d == 1)
{
return lines[1];
}
else if (d == 2)
{
return lines[2];
}
else if (d == 3)
{
return lines[3];
}
}
sf::RectangleShape cell::returnHighlight(int cellLength)
{
sf::RectangleShape highlight(sf::Vector2f(cellLength, cellLength));
highlight.setPosition(p.x*cellLength, p.y*cellLength);
highlight.setFillColor(highlightColor);
return highlight;
}
void cell::configLine(int d, int cellLength, int lineWidth)
{
if (d == 0)
{
lines[0].setSize(sf::Vector2f(cellLength, lineWidth));
lines[0].setPosition(p.x*cellLength, p.y*cellLength);
lines[0].setFillColor(sf::Color::White);
}
else if (d == 1)
{
lines[1].setSize(sf::Vector2f(cellLength, lineWidth));
lines[1].setPosition(p.x*cellLength, p.y*cellLength);
lines[1].setRotation(90.f);
lines[1].setFillColor(sf::Color::White);
}
else if (d == 2)
{
lines[2].setSize(sf::Vector2f(cellLength, lineWidth));
lines[2].setPosition(p.x*cellLength, (p.y+1)*cellLength);
lines[2].setFillColor(sf::Color::White);
}
else if (d == 3)
{
lines[3].setSize(sf::Vector2f(cellLength, lineWidth));
lines[3].setPosition((p.x+1)*cellLength, p.y*cellLength);
lines[3].setRotation(90.f);
lines[3].setFillColor(sf::Color::White);
}
}
class navigator
{
position p;
std::vector<position> history;
public:
navigator(int setX, int setY);
position returnPos();
void setPosition(position p);
void move(int d);
void pushHistory(position setP);
position popHistory();
int returnHistorySize();
};
int navigator::returnHistorySize()
{
return history.size();
}
void navigator::pushHistory(position p)
{
history.push_back(p);
}
position navigator::popHistory()
{
position lastPosition = history.back();
history.pop_back();
return lastPosition;
}
void navigator::setPosition(position setP)
{
p.x = setP.x;
p.y = setP.y;
}
void navigator::move(int d)
{
if (d == 0)
{
p.y--;
}
else if (d == 1)
{
p.x--;
}
else if (d == 2)
{
p.y++;
}
else if (d == 3)
{
p.x++;
}
}
navigator::navigator(int setX, int setY)
{
p.x = setX;
p.y = setY;
}
position navigator::returnPos()
{
return p;
}
void checkValidMoves(std::vector<std::vector<cell>>cellMap, position currentPosition, bool validMoves[4])
{
validMoves[0] = false;
validMoves[1] = false;
validMoves[2] = false;
validMoves[3] = false;
int numRows = cellMap[0].size();
int numColumns = cellMap.size();
if (currentPosition.y > 0 && !cellMap[currentPosition.x][currentPosition.y-1].returnVisited())
{
validMoves[0] = true;
}
if (currentPosition.x > 0 && !cellMap[currentPosition.x-1][currentPosition.y].returnVisited())
{
validMoves[1] = true;
}
if (currentPosition.y < numRows-1 && !cellMap[currentPosition.x][currentPosition.y+1].returnVisited())
{
validMoves[2] = true;
}
if (currentPosition.x < numColumns-1 && !cellMap[currentPosition.x+1][currentPosition.y].returnVisited())
{
validMoves[3] = true;
}
}
int main()
{
int numColumns, numRows, cellLength, lineWidth, waitTime, windowX, windowY;
std::cout << "Maze width: ";
std::cin >> numColumns;
std::cout << "\nMaze height: ";
std::cin >> numRows;
std::cout << "\nCell length: ";
std::cin >> cellLength;
std::cout << "\nLine width: ";
std::cin >> lineWidth;
std::cout << "\nWait time (ms): ";
std::cin >> waitTime;
windowX = numColumns * cellLength;
windowY = numRows * cellLength;
sf::RenderWindow window(sf::VideoMode(windowX, windowY), "Maze Generator");
std::vector<std::vector<cell> > cellMap;
bool allVisited = false;
std::random_device rd;
std::default_random_engine generator;
generator.seed(rd());
std::uniform_int_distribution<int> distribution(0, 3);
for (int i = 0; i < numColumns; i++)
{
cellMap.push_back( std::vector<cell>() );
for (int j = 0; j < numRows; j++)
{
cell newCell(i, j, cellLength, lineWidth);
cellMap[i].push_back(newCell);
}
}
navigator mapNav(0,0);
//cellMap[mapNav.returnPos().x][mapNav.returnPos().y].setHighlighted(true);
cellMap[mapNav.returnPos().x][mapNav.returnPos().y].setVisited(true);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
//char debug;
//std::cin >> debug;
//Maze-gen algorithm
if (!allVisited)
{
std::this_thread::sleep_for(std::chrono::milliseconds(waitTime));
//Check validity of moves
bool validMoves[4];
checkValidMoves(cellMap, mapNav.returnPos(), validMoves);
std::cout << validMoves[0] << " " << validMoves[1] << " " << validMoves[2] << " " << validMoves[3] << std::endl;
//If there are valid moves
if (validMoves[0] || validMoves[1] || validMoves[2] || validMoves[3])
{
//Choose random cell
bool isMoveValid = false;
int directionChose;
while (!isMoveValid)
{
directionChose = distribution(generator);
std::cout << "Boop: " << directionChose << std::endl;
if (validMoves[directionChose])
{
isMoveValid = true;
}
}
//Push current celll to stack
mapNav.pushHistory(mapNav.returnPos());
//Remove walls
cellMap[mapNav.returnPos().x][mapNav.returnPos().y].deleteLine(directionChose);
if(directionChose == 0)
{
cellMap[mapNav.returnPos().x][mapNav.returnPos().y-1].deleteLine(2);
}
else if (directionChose == 1)
{
cellMap[mapNav.returnPos().x-1][mapNav.returnPos().y].deleteLine(3);
}
else if (directionChose == 2)
{
cellMap[mapNav.returnPos().x][mapNav.returnPos().y+1].deleteLine(0);
}
else if (directionChose == 3)
{
cellMap[mapNav.returnPos().x+1][mapNav.returnPos().y].deleteLine(1);
}
//Make chosen cell the current cell and mark it as visited
mapNav.move(directionChose);
//cellMap[mapNav.returnPos().x][mapNav.returnPos().y].setHighlighted(true);
cellMap[mapNav.returnPos().x][mapNav.returnPos().y].setVisited(true);
}
//Else if the history stack isn't empty
else if(mapNav.returnHistorySize() > 0)
{
mapNav.setPosition(mapNav.popHistory());
//Pop cell and make it curret
}
//Checks if all cells have been visited
allVisited = true;
for (int i = 0; i < numColumns; i++)
{
for (int j = 0; j < numRows; j++)
{
if (!cellMap[i][j].returnVisited())
{
allVisited = false;
}
}
}
}
//Draws highlights and lines for each cell
for (int i = 0; i < numColumns; i++)
{
for (int j = 0; j < numRows; j++)
{
if (cellMap[i][j].returnHighlightStatus())
{
window.draw(cellMap[i][j].returnHighlight(cellLength));
}
for (int k = 0; k < 4; k++)
{
if (cellMap[i][j].returnDrawStatus(k))
{
window.draw(cellMap[i][j].returnLine(k));
}
}
}
}
window.display();
}
return 0;
}