-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbacktrack.c
88 lines (77 loc) · 2.56 KB
/
backtrack.c
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
#include "maze.h"
#include <stdlib.h>
void backtrack(Map * map) {
int visitedCount = 0;
int visited[(map->width * map->height)];
for (int i = 0; i < (map->width * map->height); i++) {
visited[i] = 0;
}
Tile * tile = randomTile(map);
Tile * neighborTile;
int pathIndex = 0;
Tile * path[map->width * map->height];
Direction pathDir[map->width * map->height];
path[pathIndex] = tile;
visited[(path[pathIndex]->y * map->width) + path[pathIndex]->x] = 1;
visitedCount++;
pathIndex++;
while (visitedCount < (map->width * map->height)) {
pathDir[pathIndex] = randomUnvisitedNeighbor(map, tile, visited, &path[pathIndex]);
if (pathDir[pathIndex] == NO_DIRECTION) {
// backtrack
while (pathDir[pathIndex] == NO_DIRECTION && pathIndex > 0) {
pathIndex--;
tile = path[pathIndex];
pathDir[pathIndex] = randomUnvisitedNeighbor(map, tile, visited, &path[pathIndex + 1]);
}
pathIndex++;
neighborTile = path[pathIndex];
visited[(neighborTile->y * map->width) + neighborTile->x] = 1;
visitedCount++;
// TODO: this sucks, since we backtracked, we assign the wrong direction before here
Direction dir = pathDir[pathIndex - 1];
if (dir == NORTH) {
neighborTile->connections |= DOWN;
} else if (dir == SOUTH) {
tile->connections |= DOWN;
} else if (dir == EAST) {
tile->connections |= RIGHT;
} else if (dir == WEST) {
neighborTile->connections |= RIGHT;
}
tile = path[pathIndex];
pathIndex++;
} else {
// render path
startRender();
renderMap(map);
debugRenderCursor(path[pathIndex]->x, path[pathIndex]->y, 0, 100, 0);
for (int x = 0; x < map->width; x++) {
for (int y = 0; y < map->height; y++) {
if (visited[(y * map->width) + x] == 1) {
debugRenderCursor(x, y, 222, 222, 222);
}
}
}
endRender();
delay(1);
visited[(path[pathIndex]->y * map->width) + path[pathIndex]->x] = 1;
visitedCount++;
tile = path[pathIndex - 1];
neighborTile = path[pathIndex];
// Direction is on the neighbor, 0 index never has as direction
Direction dir = pathDir[pathIndex];
if (dir == NORTH) {
neighborTile->connections |= DOWN;
} else if (dir == SOUTH) {
tile->connections |= DOWN;
} else if (dir == EAST) {
tile->connections |= RIGHT;
} else if (dir == WEST) {
neighborTile->connections |= RIGHT;
}
tile = path[pathIndex];
pathIndex++;
}
}
}