/* 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 #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= 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); } } } }