-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmaze_start.c
282 lines (249 loc) · 6.78 KB
/
maze_start.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
274
275
276
277
278
279
280
281
282
//-----------------------------------------/
// NAME: Saif Mahmud
// VERSION: 02/23/2020
// Purpose: Implementing backtracking algorithm to solve a maze.
//-----------------------------------------/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
//-------------------------------------------------------------------------------------
// CONSTANTS and TYPES
//-------------------------------------------------------------------------------------
#define MAX_DIMENSION 20
// constant definitions for the different cell states
const char WALL = '1';
const char SPACE = '0';
const char VISITED = '.';
const char MOUSE = 'r';
const char EXIT = 'e';
typedef enum BOOL { false, true } Boolean;
struct CELL
{
int row;
int column;
};
typedef struct CELL Cell;
typedef struct CELL_NODE CellNode;
struct CELL_NODE
{
Cell cell;
CellNode *next;
};
//-------------------------------------------------------------------------------------
// VARIABLES
//-------------------------------------------------------------------------------------
CellNode *top = NULL;
// a 2D array used to store the maze
char maze[MAX_DIMENSION][MAX_DIMENSION];
int mazeRows;
int mazeCols;
// holds the location of the mouse and escape hatch
Cell mouse;
Cell escape;
//-------------------------------------------------------------------------------------
// PROTOTYPES
//-------------------------------------------------------------------------------------
// basic cell manipulation
// returns true if the cells are at the same position in our maze
Boolean equalCells(const Cell cell1, const Cell cell2);
// returns a new cell object
Cell makeCell(const int row, const int col);
// returns true if the cell is within our maze
Boolean validCell(const Cell theCell);
// routines for managing our backtracking
// returns true if there are no more cells to try
Boolean noMoreCells();
// returns the next cell to try for a path out of the maze
Cell nextCell();
// introduces a new cell to try
void addCell(const Cell cell);
void printMaze();
void loadMaze();
// returns true if there's a solution to the maze
Boolean solveMaze();
// our invariant checker
void checkState();
//-------------------------------------------------------------------------------------
// FUNCTIONS
//-------------------------------------------------------------------------------------
int main( int argc, char *argv[] )
{
loadMaze();
if ( solveMaze() )
printf( "The mouse is free!!!!\n" );
else
printf( "The mouse is trapped!!!!\n" );
printf( "\nEnd of processing\n" );
return EXIT_SUCCESS;
}
Boolean solveMaze()
{
Boolean hasSolution = true;
Boolean gotSolution = false;
printf("\n");
printMaze();
Cell curr = mouse;
char item;
validCell(mouse);
validCell(escape);
checkState();
while(hasSolution == true && gotSolution == false && equalCells(curr, escape) == false )
{
maze[curr.row][curr.column] = VISITED; //mark curr/start as visited
if(maze[curr.row+1][curr.column] == EXIT || maze[curr.row][curr.column+1] == EXIT ||
maze[curr.row-1][curr.column] == EXIT || maze[curr.row][curr.column-1] == EXIT)
// that means got just in one step ahead.
{
gotSolution = true;
}
else if(gotSolution == false)
{
if(maze[curr.row+1][curr.column] == SPACE ) // going down
{
addCell(makeCell(curr.row+1, curr.column));
}
if(maze[curr.row][curr.column+1] == SPACE ) // going right
{
addCell(makeCell(curr.row, curr.column+1));
}
if(maze[curr.row-1][curr.column] == SPACE ) // going up
{
addCell(makeCell(curr.row-1, curr.column));
}
if(maze[curr.row][curr.column-1] == SPACE ) // going left
{
addCell(makeCell(curr.row, curr.column-1));
}
if(noMoreCells() == true )
{
hasSolution = false;
} else {
curr = nextCell();
validCell(curr);
}
}
printMaze();
}
return hasSolution;
}
Boolean equalCells(const Cell cell1, const Cell cell2)
{
validCell(cell1);
validCell(cell2);
Boolean result = false;
if(cell1.column == cell2.column && cell1.row == cell2.row)
{
result = true;
}
return result;
}
Cell makeCell(const int row, const int col)
{
Cell newCell;
newCell.row = row;
newCell.column = col;
validCell(newCell);
return newCell;
}
Boolean validCell(const Cell theCell)
{
assert(theCell.row <= mazeRows);
assert(theCell.column <= mazeCols);
assert(theCell.row <= MAX_DIMENSION);
assert(theCell.column <= MAX_DIMENSION);
Boolean result = false;
if(theCell.row <= mazeRows && theCell.column <= mazeCols)
{
result = true;
}
return result;
}
Boolean noMoreCells()
{
Boolean result = false;
if(top == NULL)
{
result = true;
}
return result;
}
Cell nextCell()
{
checkState();
Cell out;
out = top->cell;
top = top->next;
validCell(out);
return out;
}
void addCell(const Cell cell)
{
validCell(cell);
CellNode *myNode = (CellNode *) malloc(sizeof(CellNode));
myNode->cell = cell;
myNode->next = top;
top = myNode;
checkState();
}
void checkState()
{
#ifdef NDEBUG
assert(mazeRows > 0);
assert(mazeCols > 0);
assert(mazeRows <= MAX_DIMENSION);
assert(mazeCols <= MAX_DIMENSION);
validCell(mouse);
validCell(escape);
CellNode *curr = top;
while(curr != NULL)
{
validCell(curr->cell);
curr = curr->next;
}
#endif
}
void loadMaze()
{
FILE *f;
char fileName[100];
printf("Enter Maze Filename: \n");
fscanf(stdin, "%s", fileName);
f = fopen(fileName, "r");
char oneLine[MAX_DIMENSION];
char row, col;
fscanf(f, "%s", &row);
mazeRows = strtol(&row, NULL, 10);
fscanf(f, "%s", &col);
mazeCols = strtol(&col, NULL, 10);
assert(mazeRows > 0);
assert(mazeRows <= MAX_DIMENSION);
assert(mazeCols > 0);
assert(mazeCols <= MAX_DIMENSION);
for (int i = 0; i < mazeRows; i++) // fill maze array and set mouse, escape cell
{
for (int j = 0; j < mazeCols; j++)
{
fscanf(f, "%s", &maze[i][j]);
if ( maze[i][j] == MOUSE )
{
mouse = makeCell( i, j );
}
else if ( maze[i][j] == EXIT )
{
escape = makeCell(i, j);
}
}
}
fclose(f);
}
void printMaze() {
for (int i = 0; i < mazeRows; i++)
{
for (int j = 0; j < mazeCols; j++)
{
printf("%c ", maze[i][j]);
}
printf("\n");
}
printf("\n");
}