// COMP1511 Week 5 Lecture 1 // Time to do some cooking // Sasha V #include #define MAP_ROWS 8 #define MAP_COLUMNS 8 #define UP 'w' #define DOWN 's' #define LEFT 'a' #define RIGHT 'd' enum entity { CHEF, FOOD, FIRE, FACE_UP, FACE_DOWN, FACE_LEFT, FACE_RIGHT, EMPTY }; enum place_type { STOVE, FRYER, OVEN, REFRIGERATOR, CLEAN }; struct location { enum entity entity; enum place_type place; }; // Provided functions void initialise_map(struct location map[MAP_ROWS][MAP_COLUMNS]); void print_map(struct location map[MAP_ROWS][MAP_COLUMNS]); void print_location(struct location location, int place_print); // Our own functions int check_valid_inputs(int row, int col); void find_fire(struct location map[MAP_ROWS][MAP_COLUMNS], int start_row, int start_col, int fryer_row, int fryer_col, int stove_row, int stove_col); void start_fires(struct location map[MAP_ROWS][MAP_COLUMNS], int num_fires); int main(void) { struct location map[MAP_ROWS][MAP_COLUMNS]; initialise_map(map); // get user input of fridge location int fridge_row, fridge_col; printf("Where is the refrigerator: "); scanf("%d %d", &fridge_row, &fridge_col); // get user input of fryer location int fryer_row, fryer_col; printf("Where is the fryer: "); scanf("%d %d", &fryer_row, &fryer_col); // get user input of stove location int stove_row, stove_col; printf("Where is the stove: "); scanf("%d %d", &stove_row, &stove_col); //printf("STOVE: %d", check_valid_inputs(stove_row, stove_col)); //printf("FRIDGE: %d", check_valid_inputs(fridge_row, fridge_col)); //printf("FRYER: %d", check_valid_inputs(fryer_row, fryer_col)); // If the inputs are not valid, if ((check_valid_inputs(stove_row, stove_col) != 1) || (check_valid_inputs(fridge_row, fridge_col) != 1) || (check_valid_inputs(fryer_row, fryer_col) != 1)) { // tell the user inputs not valid printf("Inputs are not valid.\n"); // end the program //return 1; } /*// Add code to get more user input (as a part of the initial details) to // Build a big oven (2x2), so I can do more gentle cooking (instead of just // frying) char want_big_oven; printf("Do you want to build a big oven? (y/n): "); scanf(" %c", &want_big_oven); if (want_big_oven == 'y') { // ask where I would like my oven printf("Where do you want the big oven? "); int oven_row, oven_col; scanf("%d %d", &oven_row, &oven_col); int row = oven_row; while (row < oven_row + 2) { int col = oven_col; while (col < oven_col + 2) { map[row][col].place = OVEN; col++; } row++; } printf("Map after the big oven has gone into the kitchen:"); print_map(map); } */ // update the map with the fridge, stove and fryer locations map[stove_row][stove_col].entity = FOOD; map[stove_row][stove_col].place = STOVE; map[fridge_row][fridge_col].entity = CHEF; map[fridge_row][fridge_col].place = REFRIGERATOR; map[fryer_row][fryer_col].entity = FIRE; map[fryer_row][fryer_col].place = FRYER; // print out the initial map. printf("Initial Map:\n"); print_map(map); find_fire(map, fridge_row, fridge_col, fryer_row, fryer_col, stove_row, stove_col); print_map(map); return 0; } // explore until chef finds the food burning in the fryer! void find_fire(struct location map[MAP_ROWS][MAP_COLUMNS], int fridge_row, int fridge_col, int fryer_row, int fryer_col,int stove_row, int stove_col) { // Keep getting user input of 'w' (up), 'a' (left), 's' (down),'d' (right), // update and print the updated map until I find that burning fryer! int current_row = fridge_row; int current_col = fridge_col; char direction_input; while ((current_row != fryer_row || current_col != fryer_col) && scanf(" %c", &direction_input) == 1) { int num_fires; if ((current_row == stove_row && current_col == stove_col)) { printf("Oh no! More fire(s) have started in the kitchen!\n"); printf("How many other fires have started? "); scanf("%d", &num_fires); start_fires(map, num_fires); } if (direction_input == UP) { map[current_row][current_col].entity = FACE_UP; // row = row - 1 current_row = current_row - 1; } else if (direction_input == LEFT) { // left //... map[current_row][current_col].entity = FACE_LEFT; current_col = current_col - 1; } else if (direction_input == DOWN) { // down //... map[current_row][current_col].entity = FACE_DOWN; current_row = current_row + 1; } else if (direction_input == RIGHT) { map[current_row][current_col].entity = FACE_RIGHT; current_col = current_col + 1; } // current_row and current_col position that the chef // is meant to be at map[current_row][current_col].entity = CHEF; printf("Updated map:\n"); print_map(map); } } void start_fires(struct location map[MAP_ROWS][MAP_COLUMNS], int num_fires) { for (int i = 0; i < num_fires; i++) { int row = 0; int col = 0; scanf(" %d %d", &row, &col); if (check_valid_inputs(row, col) != 1 || map[row][col].entity != EMPTY) { printf("Invalid fire position!\n"); } else { map[row][col].entity = FIRE; map[row][col].place = FRYER; } } } // return 0 - given inputs not valid inputs // return 1 - given inputs are valid inputs int check_valid_inputs(int row, int col) { if (row >= 0 && row <= MAP_ROWS && col >= 0 && col <= MAP_COLUMNS) { return 1; } else { return 0; } //return row >= 0 && row < MAP_ROWS && col >= 0 && col < MAP_COLUMNS; } // STARTER FUNCTIONS BELOW THIS POINT: // initialises the map with empty areas void initialise_map(struct location map[MAP_ROWS][MAP_COLUMNS]) { int row = 0; while (row < MAP_ROWS) { int col = 0; while (col < MAP_COLUMNS) { map[row][col].place = CLEAN; map[row][col].entity = EMPTY; col++; } row++; } } // print out the map void print_map(struct location map[MAP_ROWS][MAP_COLUMNS]) { int row = 0; while (row < MAP_ROWS * 2) { int col = 0; while (col < MAP_COLUMNS) { print_location(map[row / 2][col], row % 2); col++; } row++; printf("\n"); } } // prints specific location on map (used by print_map) void print_location(struct location location, int place_print) { if (place_print) { if (location.place == STOVE) { printf(" S "); } else if (location.place == FRYER) { printf(" F "); } else if (location.place == OVEN) { printf(" O "); } else if (location.place == REFRIGERATOR) { printf(" R "); } else if (location.place == CLEAN) { printf(" . "); } else { printf(" ? "); } } else { if (location.entity == EMPTY) { printf(" "); } else if (location.entity == CHEF) { printf("(C)"); } else if (location.entity == FIRE) { printf("(!)"); } else if (location.entity == FACE_UP) { printf("(^)"); } else if (location.entity == FACE_DOWN) { printf("(v)"); } else if (location.entity == FACE_LEFT) { printf("(<)"); } else if (location.entity == FACE_RIGHT) { printf("(>)"); } else { printf(" ? "); } } }