C-code Maze
You will need these files to make work:
You will need these files to run the program file after compilation.
Here is the file in C: C-code maze
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 | /* Project #3 Project Description: In this project, you will implement a program for Easter egg hunt in an 11x19 maze. Your program should (1) read in from an input file the maze information; (2) show the egg hunter.s current location; and (3) repetitively moves the egg hunter to the direction of his chooses until the egg is found, or the hunter gets lost and asks for help, or the hunter quits. Your program should display the floor plan of the maze and a path to find the egg at the end. A master program, in the format of executable file, and sample input files will be posted on the class web site. */ #include<stdio .h> #define TRUE 1 #define FALSE 0 /*----------------Global Variables---------------------------------------------------------*/ int i, j, k; int board[11][19][4]; int boardnew[11][19][4]; int hunterx, huntery, eggx, eggy; int counter=0; char letter_counter[800]; int visited[11][19]; FILE *input, *output1, *output2; /*----------------FUNCTION DECLARATIONS----------------------------------------------------*/ int check_user_direction(char x); void ask_again(char x); int hunt(int a, int b); int main (int argc, char *argv[]) { /*-------------------Declaration of the variables------------------------------------------*/ int kill; int user_input; /*-------------------Command Line Arguments------------------------------------------------*/ if(argc !=4) // Checks to see if the usage is correct. { printf("Usage: exectuable input_file output_file1 output_file2\n"); // Prints this is the information if not inputed corectly. exit(0); } input = fopen(argv[1],"r"); // Open the input file. if( input == NULL) // Checks whether there is an input file. { printf("File %s cannot open!\n", argv[1]); // Prints this if not. exit(0); } output1 = fopen(argv[2], "w"); // Opens the input file. if( output1 == NULL) // Checks whether there is and output file. { printf("File %s cannot open!\n", argv[2]); // Prints this if not. exit(0); } output2 = fopen(argv[3], "w"); // Opens an output file to print to. if( output2 == NULL) // Checks whether there is and output file. { printf("File %s cannot open!\n", argv[3]); // Prints this if not. } /*------------------Scanning Of the input file--------------------------------------------*/ fscanf(input, "%d %d%*c", &hunterx, &huntery); fscanf(input, "%d %d%*c", &eggx, &eggy); for (j=0; j<11; j++) for (i=0; i<19; i++) { fscanf(input, "%d ",&kill); fscanf(input, "%d ",&kill); fscanf(input, "%d ", &board[j][i][0]); fscanf(input, "%d ", &board[j][i][1]); fscanf(input, "%d ", &board[j][i][2]); fscanf(input, "%d%*c", &board[j][i][3]); if (board[j][i][0] == 1) //This finds what coordinate is equal to 1 and transforms it. board[j][i][0]='|'; else board[j][i][0]=' '; if (board[j][i][1] == 1) board[j][i][1]='-'; else board[j][i][1]=' '; if (board[j][i][2] == 1) board[j][i][2]='|'; else board[j][i][2]=' '; if (board[j][i][3] == 1) board[j][i][3]='-'; else board[j][i][3]=' '; } /*------------------Assign a new 3d array with the hunter and egg locations---------------*/ for (i=0; i<11; i++) for (j=0; j<19; j++) for (k=0; k<4; k++) { boardnew[i][j][k]=board[i][j][k]; } boardnew[hunterx][huntery][0] = 'T'; boardnew[eggx][eggy][0] = 'G'; /*------------------Print the MAZE to the output file 1-----------------------------------*/ i=10; while(i>=0) { for (j=0; j<19; j++) { fprintf(output1, " %c%c%c", board[i][j][1], board[i][j][1], board[i][j][1]); } fprintf(output1, "\n"); for (j=0; j<19; j++) { if (boardnew[i][j][0] == 'T' || boardnew[i][j][0] == 'G') fprintf(output1, "%c %c ", board[i][j][0], boardnew[i][j][0]); else fprintf(output1, "%c ", board[i][j][0]); } fprintf(output1, "%c", board[i][18][2]); fprintf(output1, "\n"); i--; } for (j=18; j>=0; j--) { fprintf(output1, " %c%c%c", board[0][j][3], board[0][j][3], board[0][j][3]); } fprintf(output1, "\n"); /*------------------Print the initial conditions of the MAZE------------------------------*/ system("clear"); printf("You are now in the maze\n"); printf(" %c%c%c \n", board[hunterx][huntery][1], board[hunterx][huntery][1], board[hunterx][huntery][1]); //This prints the initial part of the printf("%c %c %c\n", board[hunterx][huntery][0], boardnew[hunterx][huntery][0], board[hunterx][huntery][2]); //user interface. printf(" %c%c%c \n", board[hunterx][huntery][3], board[hunterx][huntery][3], board[hunterx][huntery][3]); printf("Enter the direction you want to move (W/N/E/S)\n"); printf("'H' to use the computer to find the egg\n"); printf("'Q' to quit\n"); user_input = getchar(); //Get the first character that the user inputs. check_user_direction(user_input); //Then check where to go with that input. fclose(input); fclose(output1); fclose(output2); return 0; } /*----------------FUNCTIONS--------------------------------------------------------------*/ /*----------------Asks user again where to go--------------------------------------------*/ void ask_again(char x) { if (hunterx == eggx && huntery == eggy) { { for (i=0; i<counter ; i++) fprintf(output2, "%c", letter_counter[i]);} printf(" %c%c%c \n", board[hunterx][huntery][1], board[hunterx][huntery][1], board[hunterx][huntery][1]); printf("%c%c %c%c\n", board[hunterx][huntery][0], 'T', 'G', board[hunterx][huntery][2]); printf(" %c%c%c \n", board[hunterx][huntery][3], board[hunterx][huntery][3], board[hunterx][huntery][3]); printf("You found the egg!!!\n"); } else { printf(" %c%c%c \n", board[hunterx][huntery][1], board[hunterx][huntery][1], board[hunterx][huntery][1]); printf("%c %c %c\n", board[hunterx][huntery][0], 'T', board[hunterx][huntery][2]); printf(" %c%c%c \n", board[hunterx][huntery][3], board[hunterx][huntery][3], board[hunterx][huntery][3]); printf("Enter the direction you want to move (W/N/E/S)\n"); printf("'H' to use the computer to find the egg\n"); printf("'Q' to quit\n"); x = getchar(); check_user_direction(x); } } /*----------------Determine where the Egg is when the user inputs 'H'--------------------*/ int hunt(int a, int b) { int found; static char letter[800]; static int cnt=0; if (a == eggx && b == eggy) //prints the current location of the egg if found. { for (i=0; i<cnt ; i++) { fprintf(output2, "%c", letter[i]); } printf(" %c%c%c \n", board[a][b][1], board[a][b][1], board[a][b][1]); printf("%c%c %c%c\n", board[a][b][0], 'T', 'G', board[a][b][2]); printf(" %c%c%c \n", board[a][b][3], board[a][b][3], board[a][b][3]); printf("You found the egg!!!\n"); exit (0); } else { if (visited[a][b] == FALSE) { if (board[a][b][2] != '|' && b >= 0 && b < 19 && visited[a][b+1] == FALSE) //Checks if next E is 0 and goes there. { visited[a][b] = TRUE; //Sets the new location to 1 if previous statement is true. letter[cnt++] = 'E'; //Puts a 'E' in the letter array for printing. found = hunt(a, b+1); //Hunt the next location. } else if(visited[a][b] == TRUE && board[a][b][0] != '|' && b >= 0 && b < 19 && visited[a][b-1] == FALSE) { found = hunt(a, b-1); //If visited is equal to 1 go back the other way. } if (board[a][b][1] != '-' && a >= 0 && a < 11 && visited[a+1][b] == FALSE) //Then its virtually the same code just for other directions. { visited[a][b] = TRUE; letter[cnt++] = 'N'; found = hunt(a+1, b); } else if(visited[a][b] == TRUE && board[a][b][3] != '-' && a >= 0 && a < 11 && visited[a-1][b] == FALSE) { found = hunt(a-1, b); } if (board[a][b][0] != '|' && b >= 0 && b < 19 && visited[a][b-1] == FALSE) { visited[a][b] = TRUE; letter[cnt++] = 'W'; found = hunt(a, b-1); } else if (visited[a][b] == TRUE && board[a][b][2] != '|' && b >= 0 && b < 11 && visited[a][b+1] == FALSE) { found = hunt(a, b+1); } if (board[a][b][3] != '-' && a >= 0 && a < 11 && visited[a-1][b] == FALSE) { visited[a][b] = TRUE; letter[cnt++] = 'S'; found = hunt(a-1, b); } else if (visited[a][b] == TRUE && board[a][b][1] != '-' && a >= 0 && a < 11 && visited[a+1][b] == FALSE) { found = hunt(a+1, b); } } } return FALSE; } /*----------------Check which way the user wants to go and print the correct direction to the output file--------*/ int check_user_direction(char x) { fflush(stdin); { switch (x) { case 'E': { if (board[hunterx][huntery][2] == ' ') //This checks if there is a space and then increases the { huntery++; //for huntery which the x direction. letter_counter[counter++] = x; //Then puts 'E' into the letter array for printing. ask_again(x); //Then asks the user again. } else if (board[hunterx][huntery][2] == '|') //If the current location has a wall then it asks the user again. { printf("There is a Wall!\n"); ask_again(x); } else { printf("Illegal Input!\n"); //If anything else ask the user again. ask_again(x); } } break; case 'N': { if (board[hunterx][huntery][1] == ' ') //Then the same code again for the rest. { hunterx++; letter_counter[counter++] = x; ask_again(x); } else if (board[hunterx][huntery][1] == '-') { printf("There is a Wall!\n"); ask_again(x); } else { printf("Illegal Input!\n"); ask_again(x); } } break; case 'S': { if (board[hunterx][huntery][3] == ' ') { hunterx--; letter_counter[counter++] = x; ask_again(x); } else if (board[hunterx][huntery][3] == '-') { printf("There is a Wall!\n"); ask_again(x); } else { printf("Illegal Input!\n"); ask_again(x); } } break; case 'W': { if (board[hunterx][huntery][0] == ' ') { huntery--; letter_counter[counter++] = x; ask_again(x); } else if (board[hunterx][huntery][0] == '|') { printf("There is a Wall!\n"); ask_again(x); } else { printf("Illegal Input!\n"); ask_again(x); } } case 'Q': //Exit the program if the user enters 'Q' exit (0); break; case 'H': if (hunt(hunterx, huntery) == FALSE) //If the hunt function returns FALSE the egg is not in the maze. { printf("The egg is not in the MAZE!!!!\n"); } break; default: { printf("Illegal Input!\n"); //Anything other than W, N, E, S, Q, H ask again. ask_again(x); } } } } </pre> </counter></stdio> |
GD Star Rating
loading...
loading...
GD Star Rating
loading...
loading...
| Print article | This entry was posted by Tom on January 6, 2009 at 6:01 pm, and is filed under C-Code Written By me. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |


about 1 year ago
Thanks a lot for this, I appreciate the info
loading...
loading...
about 1 year ago
Fantastic post. Keep posting more interesting posts. Been observing your web logs for 2 days now and I should state I am beginning to like your post. I wish to acknowledge how do I support to your web logs?
loading...
loading...