// dancing_bird_two_step.c // // Written by YOUR-NAME (YOUR-ZID) // on TODAYS-DATE // // Tutor demo: basic movement, plus the two step special move. #include #define SIZE 5 enum tile_type { EMPTY, DANCED_ON, BIRD }; struct tile { enum tile_type type; int energy_value; }; struct position { int row; int col; }; struct dance_state { int row_direction; int col_direction; }; struct position move_bird(struct tile dance_floor[SIZE][SIZE], struct position bird, int row_change, int col_change); struct position special_move(struct tile dance_floor[SIZE][SIZE], struct position bird, struct dance_state *state); void setup_dance_floor(struct tile dance_floor[SIZE][SIZE]); void print_dance_floor(struct tile dance_floor[SIZE][SIZE]); // ----------------------------------------------------------------------------- int main(void) { struct tile dance_floor[SIZE][SIZE]; setup_dance_floor(dance_floor); struct position bird; bird.row = 0; bird.col = 0; dance_floor[bird.row][bird.col].type = BIRD; // The bird starts out facing left. struct dance_state state; state.row_direction = 0; state.col_direction = -1; printf("Let's dance!\n"); print_dance_floor(dance_floor); char command; while (scanf(" %c", &command) == 1) { if (command == 'w') { bird = move_bird(dance_floor, bird, -1, 0); } else if (command == 's') { bird = move_bird(dance_floor, bird, 1, 0); } else if (command == 'a') { bird = move_bird(dance_floor, bird, 0, -1); } else if (command == 'd') { bird = move_bird(dance_floor, bird, 0, 1); } else if (command == 'x') { bird = special_move(dance_floor, bird, &state); } print_dance_floor(dance_floor); } return 0; } // ----------------------------------------------------------------------------- // YOUR FUNCTIONS // ----------------------------------------------------------------------------- // Moves the bird by the given change in row and column, and returns its // new position. If the move would take the bird off the dance floor, // nothing changes and the bird's current position is returned. struct position move_bird(struct tile dance_floor[SIZE][SIZE], struct position bird, int row_change, int col_change) { // TODO: Complete this function return bird; } // Special move that makes the bird dance struct position special_move(struct tile dance_floor[SIZE][SIZE], struct position bird, struct dance_state *state) { // TODO: Complete this function return bird; } // ----------------------------------------------------------------------------- // PROVIDED FUNCTIONS // ----------------------------------------------------------------------------- //////////////// DO NOT CHANGE ANY OF THE CODE BELOW HERE ////////////////////// // Fill the dance floor with empty tiles and their energy values void setup_dance_floor(struct tile dance_floor[SIZE][SIZE]) { int energy[SIZE][SIZE] = { {3, 1, 4, 1, 5}, {9, 2, 6, 5, 3}, {5, 8, 9, 7, 9}, {3, 2, 3, 8, 4}, {6, 2, 6, 4, 3} }; for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { dance_floor[i][j].type = EMPTY; dance_floor[i][j].energy_value = energy[i][j]; } } } // Print the dance floor void print_dance_floor(struct tile dance_floor[SIZE][SIZE]) { printf("\n"); for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { printf("+----"); } printf("+\n"); for (int j = 0; j < SIZE; j++) { char symbol = '.'; if (dance_floor[i][j].type == DANCED_ON) { symbol = '*'; } else if (dance_floor[i][j].type == BIRD) { symbol = 'v'; } printf("| %c%d ", symbol, dance_floor[i][j].energy_value); } printf("|\n"); } for (int j = 0; j < SIZE; j++) { printf("+----"); } printf("+\n"); }