-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMazes.cpp
More file actions
169 lines (125 loc) · 4.32 KB
/
Mazes.cpp
File metadata and controls
169 lines (125 loc) · 4.32 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
#include "defGameEngine.hpp"
#include <stack>
#include <chrono>
#include <thread>
class Example : public def::GameEngine
{
public:
Example()
{
GetWindow()->SetTitle("Example");
}
const def::Vector2i mapSize = { 16, 16 };
def::Vector2i tileSize;
enum Direction : int
{
DIR_NORTH = 1 << 0,
DIR_SOUTH = 1 << 1,
DIR_EAST = 1 << 2,
DIR_WEST = 1 << 3,
DIR_VISITED = 1 << 4
};
int* map = nullptr;
int visited = 0;
std::stack<def::Vector2i> frontier;
protected:
bool OnUserCreate() override
{
def::Vector2i screenSize = GetWindow()->GetScreenSize();
tileSize = screenSize / mapSize;
map = new int[screenSize.x * screenSize.y]{ 0 };
visited = 1;
frontier.push({ 0, 0 });
return true;
}
void GetNeighbours(const def::Vector2i cell, std::vector<std::pair<int, def::Vector2i>>& out)
{
def::Vector2i coord;
auto val = [&](int ox, int oy)
{
coord.x = cell.x + ox;
coord.y = cell.y + oy;
return map[(cell.y + oy) * mapSize.x + cell.x + ox];
};
if (cell.x - 1 >= 0 && val(-1, 0) == 0)
out.push_back({ DIR_WEST, coord });
if (cell.x + 1 < mapSize.x && val(1, 0) == 0)
out.push_back({ DIR_EAST, coord });
if (cell.y - 1 >= 0 && val(0, -1) == 0)
out.push_back({ DIR_NORTH, coord });
if (cell.y + 1 < mapSize.y && val(0, 1) == 0)
out.push_back({ DIR_SOUTH, coord });
}
bool OnUserUpdate(float deltaTime) override
{
// Build a maze
//using namespace std::chrono_literals;
//std::this_thread::sleep_for(30ms);
if (visited < mapSize.x * mapSize.y)
{
const def::Vector2i& cell = frontier.top();
// Each neighbour has its direction relative to the current cell
// and a coordinate
std::vector<std::pair<int, def::Vector2i>> neighs;
GetNeighbours(cell, neighs);
if (neighs.empty())
{
// We've stucked so go backwards
frontier.pop();
}
else
{
// We have some neighbours so let's randomly pick one of them
const auto& next = neighs[rand() % (int)neighs.size()];
auto get = [&](const def::Vector2i& p) -> int&
{
return map[p.y * mapSize.x + p.x];
};
// Update the current cell
get(cell) |= next.first | DIR_VISITED;
int& cellValue = get(next.second);
// Update its neighbour
if (next.first == DIR_NORTH) cellValue |= DIR_SOUTH;
if (next.first == DIR_SOUTH) cellValue |= DIR_NORTH;
if (next.first == DIR_WEST) cellValue |= DIR_EAST;
if (next.first == DIR_EAST) cellValue |= DIR_WEST;
// Push the neighbour to the frontier
frontier.push(next.second);
visited++;
}
}
// Draw the maze
Clear(def::BLUE);
auto draw_line = [&](int x1, int y1, int x2, int y2)
{
DrawLine(x1 * tileSize.x, y1 * tileSize.y, x2 * tileSize.x, y2 * tileSize.y, def::WHITE);
};
def::Vector2i screenSize = GetWindow()->GetScreenSize();
for (int y = 0; y < mapSize.y; y++)
for (int x = 0; x < mapSize.x; x++)
{
int cell = map[y * mapSize.x + x];
if (cell & DIR_VISITED)
{
if ((cell & DIR_NORTH) == 0)
draw_line(x, y, x + 1, y);
if ((cell & DIR_SOUTH) == 0)
draw_line(x, y + 1, x + 1, y + 1);
if ((cell & DIR_EAST) == 0)
draw_line(x + 1, y, x + 1, y + 1);
if ((cell & DIR_WEST) == 0)
draw_line(x, y, x, y + 1);
}
}
// Draw our current position
FillRectangle(frontier.top() * tileSize, tileSize, def::GREEN);
return true;
}
};
int main()
{
Example demo;
if (demo.Construct(256, 240, 4, 4, false, true))
demo.Run();
return 0;
}