// COMP1511 Week 5 Lecture 1 // Time to get our hands (or feet) dirty and write a larger lecture program // Angela Finlayson 24T3 #include // Provided Constants #define INVALID_INDEX -1 #define MAP_ROWS 8 #define MAP_COLUMNS 8 // User define constants go here // Provided enums and structs enum ground_type { GRASS, WATER, MUD }; enum item_type { EMPTY, BONE }; struct location { enum item_type item; enum ground_type ground; }; // User defined enums and structs // Provided function prototypes void initialise_map( struct location map[MAP_ROWS][MAP_COLUMNS] ); void print_map( struct location map[MAP_ROWS][MAP_COLUMNS], int dog_row, int dog_col, int num_bones, int mud_spread ); void print_location( struct location location, int ground_layer, int print_dog ); void print_ground_type(enum ground_type ground); void print_item_type(enum item_type item); // User defined function prototypes int main(void) { struct location map[MAP_ROWS][MAP_COLUMNS]; initialise_map(map); print_map(map, INVALID_INDEX, INVALID_INDEX, 0, 0); printf("Enter dog's location: "); printf("\nLet's find some bones, but watch the mud!!!!\n"); return 0; } // A command loop that allow the player to move the dog around the map // updating the state of the map and the dog // Updates the changes in the map ground_type based on the dog’s movement // and the dogs state (bone and mud counters) // If the dog was in a location with mud on the previous turn he will spread // the mud to the grass // If he was in a location with water on the previous turn he will wash the mud // off and change a mud location back to grass // If the dog lands on a location with a bone // increments the bone count and remove bones from the map. // prints out “Yum!”" // Print the map after each valid move // STARTER FUNCTIONS BELOW THIS POINT ////////////////////////// // initialises the map with empty items and grass ground types // Hard codes some bones, mud and water 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].ground = GRASS; map[row][col].item = EMPTY; col++; } row++; } // Last minute decision to // Hard code these instead of getting user input // so I don't give away too much for the assignment map[0][1].item = BONE; map[1][1].item = BONE; map[7][7].item = BONE; map[0][1].ground = MUD; map[7][6].ground = MUD; map[1][0].ground = WATER; map[2][3].ground = WATER; map[5][5].ground = WATER; } // print out the map and then the game stats // Each row has 2 lines of output, // the first line represents the dog or item // the second line represents the ground void print_map( struct location map[MAP_ROWS][MAP_COLUMNS], int dog_row, int dog_col, int num_bones, int mud_spread ) { printf("==== Mud and Bones ====\n"); printf("=======================\n"); for (int row = 0; row < MAP_ROWS * 2; row++) { for (int col = 0; col < MAP_COLUMNS; col++) { int print_dog = 0; if (dog_row == row/2 && dog_col == col) { print_dog = 1; } print_location(map[row / 2][col], row % 2, print_dog); } printf("\n"); } printf("=======================\n"); printf("Mud: %2d Bones: % d\n", mud_spread, num_bones); printf("=======================\n\n"); } // prints specific location on map (used by print_map) void print_location( struct location location, int ground_print, int print_dog) { if (ground_print) { print_ground_type(location.ground); } else { if (print_dog) { printf("(D)"); } else { print_item_type(location.item); } } } // Takes a ground type enum as input and prints // a corresponding character to visually represent it void print_ground_type(enum ground_type ground) { if (ground == GRASS) { printf(" . "); } else if (ground == WATER) { printf(" W "); } else if (ground == MUD) { printf(" M "); } else { printf(" ? "); } } // Takes an item type enum as input and prints // a corresponding character to visually represent it void print_item_type(enum item_type item) { if (item == EMPTY) { printf(" "); } else if (item == BONE) { printf(" B "); } else { printf(" ? "); } }