-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze.c
273 lines (217 loc) · 5.97 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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
#include <stdio.h>
#include <stdbool.h>
#include <string.h>
#include <stdlib.h>
#include <assert.h>
#define GRID 21
#define MINSIZE 4
#define BUFFSIZE (GRID + 1)
struct maze{
char map[GRID][GRID];
int height;
int width;
int start_y;
int start_x;
};
typedef struct maze maze;
// Test function
void test(void);
// Reads maze from file
void readfile(char* filename, maze* c);
// Converts dimensions to ints
bool readsize(maze* c, char buffer[]);
// Finds start of maze in upper left corner
bool findstart(maze* c);
// Recursively explores for solution
bool explore(char maze[GRID][GRID], int y, int x, int width, int height);
// Prints out the maze
void printmaze(maze* c);
// Exits program with warning
void on_error(const char* s);
int main(int argc, char* argv[])
{
test();
if (argc != 2){
on_error("Incorrect usage. Please input .txt file with maze in it.");
}
maze* c = (maze*)calloc(1, sizeof(maze));
if (c == NULL) {
on_error("Cannot allocate memory.");
}
readfile(argv[1], c);
if(!findstart(c)){
on_error("Starting point must be in the upper left quadrant.");
}
if (!explore(c->map, c->start_y, c->start_x, c->width, c->height)){
printf("The way is shut.\n");
free(c);
return 1;
}
printf("There is a way.\n");
printmaze(c);
free(c);
return 0;
}
void test(void)
{
// Initialise maze for testing
maze* testmaze = (maze*)calloc(1, sizeof(maze));
// 10x10 maze with solution
char test[GRID][GRID] = {
{'#', '#', '#', '#', '#', '#', '#', '#', '#', '#'},
{' ', ' ', '#', ' ', ' ', ' ', ' ', ' ', ' ', '#'},
{'#', ' ', '#', ' ', '#', ' ', '#', '#', ' ', '#'},
{'#', ' ', '#', ' ', '#', '#', '#', '#', ' ', '#'},
{'#', ' ', '#', ' ', ' ', ' ', ' ', '#', ' ', '#'},
{'#', ' ', '#', ' ', '#', '#', '#', '#', ' ', '#'},
{'#', ' ', '#', ' ', ' ', ' ', ' ', '#', ' ', '#'},
{'#', ' ', '#', '#', '#', '#', ' ', '#', ' ', '#'},
{'#', ' ', ' ', ' ', ' ', ' ', ' ', '#', ' ', ' '},
{'#', '#', '#', '#', '#', '#', '#', '#', '#', '#'}
};
for (int i=0; i<10; i++){
strcpy(testmaze->map[i], test[i]);
}
char* testbuffer = "10 10";
assert(readsize(testmaze, testbuffer));
assert(testmaze->width==10);
assert(testmaze->height==10);
assert(findstart(testmaze));
assert(testmaze->start_y==1);
assert(testmaze->start_x==0);
assert(explore(testmaze->map, testmaze->start_y, testmaze->start_x, testmaze->width, testmaze->height));
//4x4 maze with no solution
char test2[GRID][GRID] = {
{'#', '#', '#', '#'},
{' ', ' ', '#', '#'},
{'#', '#', '#', '#'},
{'#', '#', '#', '#'}
};
for (int i=0; i<4; i++){
strcpy(testmaze->map[i], test2[i]);
}
char* testbuffer2 = "4 4";
assert(readsize(testmaze, testbuffer2));
assert(testmaze->width==4);
assert(testmaze->height==4);
assert(findstart(testmaze));
assert(testmaze->start_y==1);
assert(testmaze->start_x==0);
assert(!explore(testmaze->map, testmaze->start_y, testmaze->start_x, testmaze->width, testmaze->height));
//5x5 maze with invalid starting point
char test3[GRID][GRID] = {
{'#', '#', '#', '#', '#'},
{'#', ' ', ' ', ' ', ' '},
{'#', ' ', '#', '#', '#'},
{' ', ' ', '#', '#', '#'},
{'#', '#', '#', '#', '#'}
};
for (int i=0; i<4; i++){
strcpy(testmaze->map[i], test3[i]);
}
char* testbuffer3 = "5 5";
assert(readsize(testmaze, testbuffer3));
assert(testmaze->width==5);
assert(testmaze->height==5);
assert(!findstart(testmaze));
// Invalid dimensions
char* testbuffer4 = "21 21";
assert(!readsize(testmaze, testbuffer4));
char* testbuffer5 = "3 3";
assert(!readsize(testmaze, testbuffer5));
free(testmaze);
}
bool findstart(maze* c)
{
for (int j=0; j<c->height/2; j++){
if (c->map[j][0] == ' '){
c->start_y = j;
c->start_x = 0;
return true;
}
}
for (int i=0; i<c->width/2; i++){
if (c->map[0][i] == ' '){
c->start_y = 0;
c->start_x = i;
return true;
}
}
return false;
}
bool explore(char maze[GRID][GRID], int y, int x, int width, int height)
{
if (maze[y][x] == '#' || maze[y][x] == '.'){
return false;
}
maze[y][x] = '.';
if ((y == height-1 || x == width-1) && maze[y][x] != '#'){
return true;
}
if (y + 1 < height){
if (explore(maze, y+1, x, width, height)){
return true;
}
}
if (x + 1 < width){
if (explore(maze, y, x+1, width, height)){
return true;
}
}
if (y - 1 >= 0){
if (explore(maze, y-1, x, width, height)){
return true;
}
}
if (x - 1 >= 0){
if (explore(maze, y, x-1, width, height)){
return true;
}
}
return false;
}
void readfile(char* filename, maze* c)
{
FILE* fp = fopen(filename, "r");
if(!fp){
on_error("Cannot read from file.");
}
char buffer[BUFFSIZE];
// Reads first line for dimensions
if(!fgets(buffer, BUFFSIZE, fp)){
on_error("Cannot read from file.");
}
if(!readsize(c, buffer)){
on_error("Invalid dimensions. Maze must be at least 4x4 and no larger than 20x20.");
}
int i = 0;
while(fgets(c->map[i], BUFFSIZE, fp)){
i++;
}
fclose(fp);
}
bool readsize(maze* c, char buffer[])
{
char h[BUFFSIZE], w[BUFFSIZE];
sscanf(buffer, "%s %s", h, w);
c->height = atoi(h);
c->width = atoi(w);
if (c->height >= GRID || c->height < MINSIZE || c->width >= GRID || c->width < MINSIZE){
return false;
}
return true;
}
void printmaze(maze* c)
{
for (int j=0; j<c->height; j++){
for (int i=0; i<c->width; i++){
printf("%c", c->map[j][i]);
}
printf("\n");
}
}
void on_error(const char* s)
{
fprintf(stderr, "%s\n", s);
exit(EXIT_FAILURE);
}