-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmaze.c
More file actions
98 lines (87 loc) · 2.15 KB
/
maze.c
File metadata and controls
98 lines (87 loc) · 2.15 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
/* Maze generator in C.
* Joe Wingbermuehle
* 19990805
*/
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
/* Display the maze. */
void ShowMaze(const bool maze[], int width, int height) {
int x, y;
for(y = 0; y < height; y++) {
for(x = 0; x < width; x++) {
switch(maze[y * width + x]) {
case 1: printf("[]"); break;
default: printf(" "); break;
}
}
printf("\n");
}
}
/* Display the maze. */
void dump_maze(const bool maze[], int width, int height, int px, int py) {
int x, y;
for(y = 0; y < height; y++) {
for(x = 0; x < width; x++) {
if (x == px && y == py) {
printf("<>");
} else if (maze[y * width + x]) {
printf("[]");
} else {
printf(" ");
}
}
printf("\n");
}
}
/* Carve the maze starting at x, y. */
void CarveMaze(bool maze[], int width, int height, int x, int y) {
int x1, y1;
int x2, y2;
int dx, dy;
int dir, count;
dir = rand() % 4;
count = 0;
while(count < 4) {
dx = 0; dy = 0;
switch(dir) {
case 0: dx = 1; break;
case 1: dy = 1; break;
case 2: dx = -1; break;
default: dy = -1; break;
}
x1 = x + dx;
y1 = y + dy;
x2 = x1 + dx;
y2 = y1 + dy;
if( x2 > 0 && x2 < width && y2 > 0 && y2 < height
&& maze[y1 * width + x1] == 1 && maze[y2 * width + x2] == 1) {
maze[y1 * width + x1] = false;
maze[y2 * width + x2] = false;
x = x2; y = y2;
dir = rand() % 4;
count = 0;
} else {
dir = (dir + 1) % 4;
count += 1;
}
}
}
/* Generate maze in matrix maze with size width, height. */
void GenerateMaze(bool maze[], int width, int height) {
int x, y;
/* Initialize the maze. */
for(x = 0; x < width * height; x++) {
maze[x] = true;
}
maze[1 * width + 1] = false;
/* Seed the random number generator. */
srand(time(0));
/* Carve the maze. */
for(y = 1; y < height; y += 2) {
for(x = 1; x < width; x += 2) {
CarveMaze(maze, width, height, x, y);
}
}
}