-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.c
143 lines (127 loc) · 4.03 KB
/
main.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
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
// Some maze generation code inspired by the mazes for programmers book
#include <SDL.h>
#include <stdlib.h>
#include "maze.h"
#include "render.c"
#include "util.c"
#include "b_tree.c"
#include "sidewinder.c"
#include "random_walk.c"
#include "wilson.c"
#include "hunt_and_kill.c"
#include "backtrack.c"
time_t t;
void clearMap(Map * map) {
for (int x = 0; x < map->width; x++) {
for (int y = 0; y < map->height; y++) {
map->tiles[(y * map->width) + x].unreachable = 0;
map->tiles[(y * map->width) + x].connections = 0;
map->tiles[(y * map->width) + x].distance = 0;
map->tiles[(y * map->width) + x].blocks = 0;
if (x == 0) {
map->tiles[(y * map->width) + x].blocks |= LEFT;
}
if (y == 0) {
map->tiles[(y * map->width) + x].blocks |= UP;
}
if (x + 1 == map->width) {
map->tiles[(y * map->width) + x].blocks |= RIGHT;
}
if (y + 1 == map->height) {
map->tiles[(y * map->width) + x].blocks |= DOWN;
}
}
}
}
void applyMask(Map * map) {
// Mask with shapes
/* int mask[] = { */
/* 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, */
/* 0,1,1,1,0,1,1,0,0,1,1,1,0,1,1,0,1,1,0,0, */
/* 0,0,0,1,0,0,0,1,0,0,0,1,0,1,0,1,0,1,0,0, */
/* 0,1,1,1,0,1,0,1,0,1,1,1,0,1,0,0,0,1,0,0, */
/* 0,1,0,1,0,1,0,1,0,1,0,1,0,1,0,0,0,1,0,0, */
/* 0,1,0,1,0,1,1,0,0,1,0,1,0,1,0,0,0,1,0,0, */
/* 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, */
/* 0,1,1,0,1,1,0,1,1,1,0,1,1,1,1,0,1,1,1,0, */
/* 0,1,0,1,0,1,0,0,0,1,0,0,0,1,0,0,1,0,0,0, */
/* 0,1,0,0,0,1,0,1,1,1,0,0,1,0,0,0,1,1,0,0, */
/* 0,1,0,0,0,1,0,1,0,1,0,1,0,0,0,0,1,0,0,0, */
/* 0,1,0,0,0,1,0,1,0,1,0,1,1,1,1,0,1,1,1,0, */
/* 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, */
/* 0,0,0,1,1,0,1,1,0,0,0,0,0,1,1,0,1,1,0,0, */
/* 0,0,1,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,1,0, */
/* 0,0,1,0,0,0,0,0,1,0,0,0,1,0,0,0,0,0,1,0, */
/* 0,0,0,1,0,0,0,1,0,0,0,0,0,1,0,0,0,1,0,0, */
/* 0,0,0,0,1,0,1,0,0,0,0,0,0,0,1,0,1,0,0,0, */
/* 0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,1,0,0,0,0, */
/* 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 */
/* }; */
int mask[(map->width * map->height)];
for (int i = 0; i < (map->width * map->height); i++) {
mask[i] = 0;
}
int i;
for (int x = 0; x < map->width; x++) {
for (int y = 0; y < map->height; y++) {
i = ((y * map->width) + x);
if (mask[i] == 1) {
map->tiles[i].unreachable = 1;
map->tiles[i].blocks |= COMPLETELY_BLOCKED;
map->tiles[i - 1].blocks |= RIGHT;
map->tiles[i - map->width].blocks |= DOWN;
}
}
}
}
void initMap(Map * map) {
map->width = MAP_SIZE;
map->height = MAP_SIZE;
map->tileCount = map->height * map->width;
map->tiles = malloc(sizeof(Tile) * map->tileCount);
clearMap(map);
}
int main() {
Tile * firstFurthest, *secondFurthest;
srand((unsigned) time(&t));
Map map;
Event event;
initRenderer();
initMap(&map);
for (int y = 0; y < map.height; y++) {
for (int x = 0; x < map.width; x++) {
map.tiles[(map.height * y) + x].x = x;
map.tiles[(map.height * y) + x].y = y;
map.tiles[(map.height * y) + x].unreachable = 0;
map.tiles[(map.height * y) + x].blocks = 0;
map.tiles[(map.height * y) + x].connections = 0;
}
}
applyMask(&map);
/* sidewinder_map(&map); */
while (1) {
renderPuzzle(&map);
event = blockInput();
if (event == QUIT) {
return 0;
} else if (event == REDO) {
clearMap(&map);
/* applyMask(&map); */
hunt_and_kill(&map);
firstFurthest = distanceFrom(&map, 0,0);
for (int y = 0; y < map.height; y++) {
for (int x = 0; x < map.width; x++) {
map.tiles[(map.height * y) + x].distance = 0;
}
}
secondFurthest = distanceFrom(&map, firstFurthest->x, firstFurthest->y);
printf("Longest path is from (%i,%i) -> (%i,%i)\n",
secondFurthest->x, secondFurthest->y,
firstFurthest->x, firstFurthest->y
);
printf("deadends: %i\n", countDeadends(&map));
}
}
closeRenderer();
return 0;
}