// Pantea Aria // Robot Explorer – Space Station Adventure // A large program to help with assignment 1 // starter code #include // Provided Constants #define INVALID_INDEX -1 #define MAP_ROWS 8 #define MAP_COLUMNS 8 // User defined constants #define MOVE_UP 'w' #define MOVE_LEFT 'a' #define MOVE_DOWN 's' #define MOVE_RIGHT 'd' // Provided enums and structs enum floor_type { SAFE, RADIATION }; enum item_type { EMPTY, ENERGY }; struct location { enum item_type item; enum floor_type floor; }; // User defined structs struct robot_data { int row; int col; int energy_count; int damage_count; }; // Provided function prototypes void initialise_station(struct location station[MAP_ROWS][MAP_COLUMNS]); void print_station( struct location station[MAP_ROWS][MAP_COLUMNS], int robot_row, int robot_col, int energy_count, int damage_count ); void print_location(struct location location, int floor_layer, int print_robot); void print_floor_type(enum floor_type floor); void print_item_type(enum item_type item); // User defined function prototypes int is_valid_position(int row, int col); struct robot_data station_update( struct location station[MAP_ROWS][MAP_COLUMNS], struct robot_data robot ); void explore_station( struct location station[MAP_ROWS][MAP_COLUMNS], struct robot_data robot ); int main(void) { struct location station[MAP_ROWS][MAP_COLUMNS]; initialise_station(station); print_station(station, INVALID_INDEX, INVALID_INDEX, 0, 0); printf("Enter robot starting location: "); struct robot_data robot; robot.energy_count = 0; robot.damage_count = 0; if (scanf("%d %d", &robot.row, &robot.col) != 2 || is_valid_position(robot.row, robot.col) == 0) { robot.row = 0; robot.col = 0; } robot = station_update(station, robot); print_station( station, robot.row, robot.col, robot.energy_count, robot.damage_count ); printf("\nExplore the station and collect energy cells!\n"); explore_station(station, robot); printf("System shutdown.\n"); return 0; } // Returns 1 if row and col are within map bounds, 0 otherwise int is_valid_position(int row, int col) { int valid = 1; if (row < 0 || col < 0 || row >= MAP_ROWS || col >= MAP_COLUMNS) { valid = 0; } return valid; } // Updates robot stats based on current location struct robot_data station_update( struct location station[MAP_ROWS][MAP_COLUMNS], struct robot_data robot ) { // Collect energy cell if (station[robot.row][robot.col].item == ENERGY) { robot.energy_count++; station[robot.row][robot.col].item = EMPTY; } // Take radiation damage if (station[robot.row][robot.col].floor == RADIATION) { robot.damage_count++; } return robot; } // Handles robot movement loop void explore_station( struct location station[MAP_ROWS][MAP_COLUMNS], struct robot_data robot ) { char move; // Should we add more condition here? while (scanf(" %c", &move) == 1) { if (move == MOVE_UP) { robot.row--; } else if (move == MOVE_DOWN) { robot.row++; } else if (move == MOVE_LEFT) { robot.col--; } else if (move == MOVE_RIGHT) { robot.col++; } robot = station_update(station, robot); print_station( station, robot.row, robot.col, robot.energy_count, robot.damage_count ); } } // STARTER FUNCTIONS BELOW THIS POINT ////////////////////////// // Initialises the station with empty items and safe floors // Hard codes some energy cells and radiation void initialise_station(struct location station[MAP_ROWS][MAP_COLUMNS]) { int row = 0; while (row < MAP_ROWS) { int col = 0; while (col < MAP_COLUMNS) { station[row][col].floor = SAFE; station[row][col].item = EMPTY; col++; } row++; } // Hard-coded setup station[0][1].item = ENERGY; station[1][1].item = ENERGY; station[7][7].item = ENERGY; station[5][1].item = ENERGY; station[0][1].floor = RADIATION; station[7][6].floor = RADIATION; station[0][2].floor = RADIATION; station[0][3].floor = RADIATION; } // Prints the station map and game statistics void print_station( struct location station[MAP_ROWS][MAP_COLUMNS], int robot_row, int robot_col, int energy_count, int damage_count ) { printf("==== Robot Explorer ====\n"); printf("========================\n"); for (int row = 0; row < MAP_ROWS * 2; row++) { for (int col = 0; col < MAP_COLUMNS; col++) { int print_robot = 0; if (robot_row == row / 2 && robot_col == col) { print_robot = 1; } print_location( station[row / 2][col], row % 2, print_robot ); } printf("\n"); } printf("========================\n"); printf("Energy: %2d Damage: %2d\n", energy_count, damage_count); printf("========================\n\n"); } // Prints a single location void print_location( struct location location, int floor_print, int print_robot ) { if (floor_print) { print_floor_type(location.floor); } else { if (print_robot) { printf("(R)"); } else { print_item_type(location.item); } } } // Prints the floor type void print_floor_type(enum floor_type floor) { if (floor == SAFE) { printf(" . "); } else if (floor == RADIATION) { printf(" X "); } else { printf(" ? "); } } // Prints the item type void print_item_type(enum item_type item) { if (item == EMPTY) { printf(" "); } else if (item == ENERGY) { printf(" E "); } else { printf(" ? "); } }