/
ass2b.c
204 lines (177 loc) · 5.27 KB
/
ass2b.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
/* Christopher Mercuri, Student Number: 694657 */
/* COMP20005 Engineering Computation Assignment 2 */
/* C Programming is Fun! */
#include <stdio.h>
#include <string.h>
#define MAXMAZE 100
#define WALL '#'
#define PATH '.'
#define CORR_PATH '+'
#define UNUSED '-'
typedef char maze_row[MAXMAZE+1]; // string array to store a maze row
typedef struct {
maze_row maze[MAXMAZE]; //array of string arrays to store maze
int hassoln; //set to 0, and becomes 1 if solution found
int row_count; //row counter
int col_count; //column counter
} maze_t;
void read_maze(maze_t *maze);
void print_stage1(maze_t *maze);
void assign_reachability(maze_t *maze);
int is_deadend(maze_t *maze, int i, int j);
void valid_path(maze_t *maze, int *i, int *j);
void print_stage2(maze_t *maze);
int
main(int argc, char* argv[]) {
maze_t m;
read_maze(&m);
print_stage1(&m);
assign_reachability(&m);
print_stage2(&m);
return 0;
}
/* Function that sets all maze counters to 0 and then reads in the maze row by
row and stores it while updating the number of rows/columns */
void
read_maze(maze_t *maze) {
/* Set all of the counters to 0 */
maze->row_count = 0;
maze->col_count = 0;
maze->hassoln = 0;
while ((scanf("%s", maze->maze[maze->row_count])) != EOF) {
maze->row_count ++;
}
maze->col_count = strlen(maze->maze[0]);
}
/* Function that prints out the stored array in the format of Stage 1 */
void
print_stage1(maze_t *maze) {
int i, j;
/* Print the stage intro stuff */
printf("Stage 1\n");
printf("=======\n");
printf("maze has %d rows and %d columns\n", maze->row_count,
maze->col_count);
/* Read through the maze character by character and print 2x the
character */
for(i=0; i<(maze->row_count); i++) {
for(j=0; j<(maze->col_count); j++) {
if (maze->maze[i][j] == WALL) {
printf("##");
} else if (maze->maze[i][j] == PATH) {
printf("..");
}
if (j == (maze->col_count-1)) {
printf("\n");
}
}
}
printf("\n");
}
/* Function that reads throught the array and alters the path characters if
they are reachable from the top */
void
assign_reachability(maze_t *maze) {
int i, j, start;
for(start=0; start<(maze->col_count); start++) {
if (maze->maze[0][start] == PATH) {
maze->maze[0][start] = CORR_PATH;
j = start;
i = 1;
while ((i < maze->row_count) && (i >= 0) &&
(j <= maze->col_count) && (j >= 0) &&
(!is_deadend(maze, i, j))) {
maze->maze[i][j] = CORR_PATH;
valid_path(maze, &i, &j);
}
if ((i == (maze->row_count-1)) && (maze->maze[i][j] = CORR_PATH)) {
maze->hassoln = 1;
}
}
}
for(i=0; i<(maze->row_count); i++) {
for(j=0; j<(maze->col_count); j++) {
if (maze->maze[i][j] == PATH) {
if (maze->maze[i-1][j] == CORR_PATH){
maze->maze[i][j] = CORR_PATH;
} else if (maze->maze[i+1][j] == CORR_PATH){
maze->maze[i][j] = CORR_PATH;
} else if (maze->maze[i][j-1] == CORR_PATH){
maze->maze[i][j] = CORR_PATH;
} else if (maze->maze[i][j+1] == CORR_PATH){
maze->maze[i][j] = CORR_PATH;
} else {
maze->maze[i][j] = UNUSED;
}
}
}
}
}
int
is_deadend(maze_t *maze, int i, int j) {
int count = 0;
if ((maze->maze[i-1][j] == PATH) || (maze->maze[i-1][j] == CORR_PATH)){
count++;
}
if ((maze->maze[i+1][j] == PATH) || (maze->maze[i+1][j] == CORR_PATH)){
count++;
}
if ((maze->maze[i][j-1] == PATH) || (maze->maze[i][j-1] == CORR_PATH)){
count++;
}
if ((maze->maze[i][j+1] == PATH) || (maze->maze[i][j+1] == CORR_PATH)){
count++;
}
if (count > 1) {
return 0;
}
return 1;
}
void
valid_path(maze_t *maze, int *i, int *j) {
if (maze->maze[*i-1][*j] == PATH){
*i -= 1;
} else if (maze->maze[*i+1][*j] == PATH){
*i += 1;
} else if (maze->maze[*i][*j-1] == PATH){
*j -= 1;
} else if (maze->maze[*i][*j+1] == PATH){
*j += 1;
}
}
/* Function that determines if there is a solution to the maze and prints it
out as per the format of stage 2 */
void
print_stage2(maze_t *maze) {
int i, j;
/* Print the stage intro stuff */
printf("Stage 2\n");
printf("=======\n");
for(j=0; j<(maze->col_count); j++) {
if (maze->maze[(maze->row_count)-1][j] == CORR_PATH) {
maze->hassoln = 1;
}
}
if (maze->hassoln) {
printf("maze has a solution\n");
} else {
printf("maze does not have a solution\n");
}
/* Read through the maze character by character and print 2x the
character */
for(i=0; i<(maze->row_count); i++) {
for(j=0; j<(maze->col_count); j++) {
if (maze->maze[i][j] == WALL) {
printf("##");
} else if (maze->maze[i][j] == CORR_PATH) {
printf("++");
} else if (maze->maze[i][j] == UNUSED) {
printf("--");
}
if (j == (maze->col_count-1)) {
printf("\n");
}
}
}
printf("\n");
}