-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
363 lines (322 loc) · 8.87 KB
/
main.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
//The File Created By Mohammad Hosein Mohseni(402149059) And Mahdi Karimi(402149058).
//Master: Fatemeh Porgholamali
//Valiasr Rafsanjan University
//Computer Engineering 1402
//Term: 4021
#include <stdio.h>
// Function For print Random Maze at the moment.
void PM(int row,int col,char mat[row][col]){
for(int i=0;i<row;i++){
for (int j = 0; j < col ; j++) {
printf("%c ",mat[i][j]);
}
printf("\n");
}
}
// Function for Generating Random Maze.
void Random_Maze_Generator(int isp,int jsp,int row_size,int col_size,char mat[row_size][col_size], FILE *file)
{
int ii, jj, i=isp, j=jsp, rep, k;
char dir, temp;
//Fill Maze with Walls(#)
for(ii=0;ii<row_size;ii++){
for (jj = 0; jj < col_size ; jj++) {
mat[ii][jj]='#';
}
}
//Defining Entrance
mat[i][j]='.';
if(i==0){
i++;
mat[i][j]='.';
}
else if(i==row_size-1){
i--;
mat[i][j]='.';
}
else if(j==0){
j++;
mat[i][j] = '.';
}
else if(j==col_size-1){
j--;
mat[i][j]='.';
}
PM(row_size,col_size,mat);
//Loop for Movement
while(j>0 && j<col_size-1 && i>0 && i<row_size-1){
printf("Enter Reputation(1 2 3 ...) AND Direction(U D R L): \n");
scanf("%c",&temp); // for \n
scanf("%d %c", &rep, &dir);
//Error Control
if(rep<=0){
printf("Error: Wrong Reputation! Try Again: \n");
continue;
}
//Conditions for Movement in specific Direction
if (dir == 'U' || dir == 'u') { //Move Up
if(rep>i){// Error Control
printf("Error: Extra Reputation on Up! Try Again: \n");
continue;
}
for (k=0;k<rep;k++) {
mat[i][j]='.';
i--;
mat[i][j] = '0';
}
PM(row_size, col_size, mat);
}
else if (dir == 'D' || dir == 'd') { //Move Down
if(i+rep>row_size-1){// Error Control
printf("Error: Extra Reputation on Down! Try Again: \n");
continue;
}
for (k=0;k<rep;k++) {
mat[i][j]='.';
i++;
mat[i][j] = '0';
}
PM(row_size, col_size, mat);
}
else if (dir == 'R' || dir == 'r') {//Move Right
if(j+rep>col_size-1){// Error Control
printf("Error: Extra Reputation on Right! Try Again: \n");
continue;
}
for(k=0;k<rep;k++) {
mat[i][j]='.';
j++;
mat[i][j] = '0';
}
PM(row_size, col_size, mat);
}
else if (dir == 'L' || dir == 'l') { //Move Left
if(rep>j){// Error Control
printf("Error: Extra Reputation on Left! Try Again: \n");
continue;
}
for(k=0;k<rep;k++) {
mat[i][j]='.';
j--;
mat[i][j] = '0';
}
PM(row_size, col_size, mat);
}
else{
printf("Error: Wrong Direction Input! Try Again: \n");
continue;
}
}
//Change '0' Cursor to '.'
for (int x = 0; x < row_size; x++) {
for (int y = 0; y < col_size; y++) {
if (mat[x][y] == '0') mat[x][y] = '.';
}
}
// Writing Generated matrix in file.
for(ii=0;ii<row_size;ii++){
for(jj=0;jj<col_size;jj++){
fprintf(file,"%c",mat[ii][jj]);
}
fprintf(file,"\n");
}
}
// Function for reading File and convert it to Matrix.
void File_to_Matrix(FILE* file,int row_size,int column_size,char matrix[row_size][column_size])
{
char CH;
for(int i=0;i<row_size;i++){
for(int j=0;j<column_size;j++){
CH= (char)fgetc(file);
matrix[i][j]=CH;
}
fgetc(file);
}
}
//Function for Finding the Way to Exit.
void Find_Way(int i, int j, int row_size, int column_size, char mat[row_size][column_size])
{
//Entrance
mat[i][j] = 'x';
if(i==0){
i++;
mat[i][j]='x';
}
else if(i==row_size-1){
i--;
mat[i][j]='x';
}
else if(j==0){
j++;
mat[i][j] = 'x';
}
else if(j==column_size-1){
j--;
mat[i][j]='x';
}
//Loop for Finding Way
while(j>0 && j<column_size-1 && i>0 && i<row_size-1){
if(mat[i][j-1]=='x'){ // Face Right
if(mat[i+1][j]=='.'){ //Move Right
i++;
mat[i][j]='x';
}
else if(mat[i][j+1]=='.'){ //Move Straight
j++;
mat[i][j]='x';
}
else if(mat[i-1][j]=='.'){ //Move Left
i--;
mat[i][j]='x';
}
else{ //Reverse Move
mat[i][j]='0';
j--;
}
}
else if(mat[i-1][j]=='x'){ //Face Down
if(mat[i][j-1]=='.'){
j--;
mat[i][j]='x';
}
else if(mat[i+1][j]=='.'){
i++;
mat[i][j]='x';
}
else if(mat[i][j+1]=='.'){
j++;
mat[i][j]='x';
}
else{
mat[i][j]='0';
i--;
}
}
else if(mat[i+1][j]=='x'){ //Face UP
if(mat[i][j+1]=='.'){
j++;
mat[i][j]='x';
}
else if(mat[i-1][j]=='.'){
i--;
mat[i][j]='x';
}
else if(mat[i][j-1]=='.'){
j--;
mat[i][j]='x';
}
else{
mat[i][j]='0';
i++;
}
}
else{ //Face Left
if(mat[i-1][j]=='.'){
i--;
mat[i][j]='x';
}
else if(mat[i][j-1]=='.'){
j--;
mat[i][j]='x';
}
else if(mat[i+1][j]=='.'){
i++;
mat[i][j]='x';
}
else{
mat[i][j]='0';
j++;
}
}
}
}
int main()
{
//Creating Main File of Project
FILE *maze_file;
int n, m, is ,js , option;
//Choosing Between Default and Random Options
printf("Choose:\n1)Default Maze.\n2)Random Handmade Generated Maze.\n");
scanf("%d",&option);
//Default Option
if(option==1) {
//Opening Default File az Main File of the Project
maze_file = fopen("default_maze.txt","r");
//Default Dimensions
n=12;
m=12;
//Default Start Point
is=2;
js=0;
}
//Random Option
else if(option==2){
printf("OK, let's Make it :)\n");
//Creating Random Maze File
FILE *rand_maze;
rand_maze=fopen("rand_maze.txt","w");
//Get Dimensions from User
printf("Enter Dimensions(row x column):\n");
scanf("%d %d", &n, &m);
//Error Control
if(n<=2 || m<=2){
printf("Error: Invalid Dimension!(At least 3x3) ");
return 0;
}
//Get Entrance Coordinates from User
printf("Enter Entrance Coordinates (row,column): \n");
scanf("%d %d", &is, &js);
//Error Control
if((is+js <= 0) || (is+js >= n+m-2) || (is==n-1 && js==0) || (is==0 && js==m-1)){
printf("Error: Invalid Entrance(Corners)! \n");
return 0;
}
if(is!=0 && is != n-1 && js!=0 && js!=m-1){
printf("Error: Invalid Entrance(Middle)! \n");
return 0;
}
//Creating Matrix for Random Maze
char rand_mat[n][m];
Random_Maze_Generator(is,js,n,m,rand_mat,rand_maze);
//Opening Random File as Main file of the Project
maze_file = fopen("rand_maze.txt","r");
fclose(rand_maze);
}
//Error Control
else{
printf("Error: Wrong Input! \n");
return 0;
}
//Creating Matrix and Copy the Main File to it.
char M[n][m];
rewind(maze_file);
File_to_Matrix(maze_file,n,m,M);
fclose(maze_file);
// Finding way to Exit.
Find_Way(is, js, n, m, M);
// Create File for GUI.
FILE *GUI;
GUI = fopen("GUI.txt","w");
for(int i=0;i<n;i++){
for(int j=0;j<m;j++){
fprintf(GUI,"%c",M[i][j]);
}
fprintf(GUI,"\n");
}
fclose(GUI);
//Preparing for print result(change 0 to x).
for (int x = 0; x < n; x++) {
for (int y = 0; y < m; y++) {
if (M[x][y] == '0') M[x][y] = 'x';
}
}
//Print Result.
printf("The Result Maze is : \n");
for(int x=0;x<n;x++) {
for (int y = 0; y < m; y++) {
printf("%c ", M[x][y]);
}
printf("\n");
}
return 0;
}