-
Notifications
You must be signed in to change notification settings - Fork 0
/
maze.c
119 lines (88 loc) · 2.47 KB
/
maze.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
/*
* Donovan Ellison
* 4-28-2017
* maze.h
*/
#include "maze.h"
void (*go[4])(char ***, struct location *, int, int) = {goNorth, goEast, goSouth, goWest};
int carve(char *** maze, struct location * size, int row, int col, int direction)
{
int randDirection, nextDirection, i;
(*maze)[row][col] = '0';
/* for(i = 0; i < size->row; ++i)
{
printf("%s", (*maze)[i]);
}
printf("\n");*/
randDirection = rand() % 3 + 3;
nextDirection = rand() % 2 + 1;
direction = (direction + randDirection) % 4;
go[direction](maze, size, row, col);
if(randDirection == 5)
++nextDirection;
direction = (direction + nextDirection) % 4;
go[direction](maze, size, row, col);
direction = (direction + 2 * nextDirection + randDirection) % 4;
go[direction](maze, size, row, col);
return 0;
}
void goNorth(char *** maze, struct location * size, int row, int col)
{
int i, j;
int move = 1;
/*If next to edge of array*/
if(row - 1 == 0)
return;
/*If it would create a circular path*/
for(i = 1; i <= 2; ++i)
for(j = -1; j <= 1; ++j)
if((*maze)[row - i][col + j] == '0')
move = 0;
if(move)
carve(maze, size, --row, col, 0);
}
void goEast(char *** maze, struct location * size, int row, int col)
{
int i, j;
int move = 1;
/*If next to edge of array*/
if(col + 2 == size->col)
return;
/*If it would create a circular path*/
for(i = 1; i <= 2; ++i)
for(j = -1; j <= 1; ++j)
if((*maze)[row + j][col + i] == '0')
move = 0;
if(move)
carve(maze, size, row, ++col, 1);
}
void goSouth(char *** maze, struct location * size, int row, int col)
{
int i, j;
int move = 1;
/*If next to edge of array*/
if(row + 2 == size->row)
return;
/*If it would create a circular path*/
for(i = 1; i <= 2; ++i)
for(j = -1; j <= 1; ++j)
if((*maze)[row + i][col + j] == '0')
move = 0;
if(move)
carve(maze, size, ++row, col, 2);
}
void goWest(char *** maze, struct location * size, int row, int col)
{
int i, j;
int move = 1;
/*If next to edge of array*/
if(col - 1 == 0)
return;
/*If it would create a circular path*/
for(i = 1; i <= 2; ++i)
for(j = -1; j <= 1; ++j)
if((*maze)[row + j][col - i] == '0')
move = 0;
if(move)
carve(maze, size, row, --col, 3);
}