-
Notifications
You must be signed in to change notification settings - Fork 0
/
generator.cpp
82 lines (80 loc) · 2.13 KB
/
generator.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
#include "generator.h"
#include "global.h"
#include "random.h"
#include "loc.h"
#include <array>
#include <thread>
#include <chrono>
#include <iostream>
using namespace std;
using namespace global;
constexpr array<array<uint8_t, 4>, 24> getDirectionsCombutations()
{
array<array<uint8_t, 4>, 24> directionsTemp{};
int index{ 0 };
constexpr array<uint8_t, 4> directions{ north, east, south, west };
for (uint8_t first : directions)
{
array<uint8_t, 4> row{ first };
for (uint8_t second : directions)
{
if (second == first) continue;
row[1] = second;
for (uint8_t third : directions)
{
if ((third == first) || (third == second)) continue;
row[2] = third;
for (uint8_t fourth : directions)
{
if ((fourth == first) || (fourth == second) || (fourth == third)) continue;
row[3] = fourth;
directionsTemp[index++] = row;
}
}
}
}
return directionsTemp;
}
constexpr array<array<uint8_t, 4>, 24> directions{ getDirectionsCombutations() };
maze_pointer generator::genMaze(int width, int height)
{
vector<uint8_t>* grid = new vector<uint8_t>(((size_t)width) * height);
vector<loc> path{ {random::next_int(width - 1), random::next_int(height - 1)} };
while (!path.empty())
{
cont_while:
for (uint8_t direction : directions[random::next_int23()])
{
loc next{ path.back() };
uint8_t opposite{ 0 };
switch (direction)
{
case north:
--next.y;
opposite = south;
break;
case east:
++next.x;
opposite = west;
break;
case south:
++next.y;
opposite = north;
break;
case west:
--next.x;
opposite = east;
break;
}
if (next.x >= 0 && next.y >= 0 && next.x < width && next.y < height && (*grid)[next.index(width)] == 0)
{
(*grid)[path.back().index(width)] |= direction;
(*grid)[next.index(width)] |= opposite;
path.push_back(next);
goto cont_while; //Simulating the continue(label) language construct from java. Also skips the condition, as we know it's true.
}
}
path.pop_back();
}
return { shared_ptr<vector<uint8_t>>(grid), width, height };
}