// 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; }; 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); void find_fire(struct location map[MAP_ROWS][MAP_COLUMNS], int fridge_row, int fridge_col, int fryer_row, int fryer_col); int check_validity(int row, int col); int main(void) { struct location map[MAP_ROWS][MAP_COLUMNS]; initialise_map(map); // TODO... int fridge_row; int fridge_col; printf("Where is the refrigerator? "); scanf("%d %d", &fridge_row, &fridge_col); int fryer_row; int fryer_col; printf("Where is the fryer? "); scanf("%d %d", &fryer_row, &fryer_col); int stove_row; int stove_col; printf("Where is the stove? "); scanf("%d %d", &stove_row, &stove_col); // Check for valid inputs: // Let's make a function to check for valid input... if ((check_validity(fridge_row, fridge_col) != 1) || (check_validity(fryer_row, fryer_col) != 1) || (check_validity(stove_row, stove_col) != 1)) { printf("Input not valid!"); return 1; } print_map(map); map[fridge_row][fridge_col].place = REFRIGERATOR; map[fridge_row][fridge_col].entity = CHEF; map[fryer_row][fryer_col].place = FRYER; map[fryer_row][fryer_col].entity = FIRE; map[stove_row][stove_col].place = STOVE; map[stove_row][stove_col].entity = FOOD; print_map(map); find_fire(map, fridge_row, fridge_col, fryer_row, fryer_col); return 0; } // Function to find fire void find_fire(struct location map[MAP_ROWS][MAP_COLUMNS], int fridge_row, int fridge_col, int fryer_row, int fryer_col) { int current_row = fridge_row; int current_col = fridge_col; char direction; while ((current_row != fryer_row || current_col != fryer_col) && (scanf(" %c", &direction) == 1)){ if (direction == UP) { map[current_row][current_col].entity = FACE_UP; current_row = current_row - 1; } else if (direction == LEFT) { map[current_row][current_col].entity = FACE_LEFT; current_col = current_col - 1; } else if (direction == DOWN) { map[current_row][current_col].entity = FACE_DOWN; current_row = current_row + 1; } else if (direction == RIGHT) { map[current_row][current_col].entity = FACE_RIGHT; current_col = current_col + 1; } map[current_row][current_col].entity = CHEF; print_map(map); } } // Function will check for valid input // Input: row and col (two ints) // Output: int (1 or 0) int check_validity(int row, int col) { if (row >= 0 && row < MAP_ROWS && col >= 0 && col < MAP_COLUMNS) { return 1; } else { return 0; } } // 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(" ? "); } } }