// Jax and Juno go on a nighttime walk // Jax and Juno are two adventurous dogs, who love to do night time park // exploring with Sasha. This program should show their efforts // exploring one particular park which is sized 10x10. Start with only Jax // exploring and then we can talk about how to add Juno in also. // // Starter code // // Written by Sasha Vassar September, 2021 #include // The dimensions of the park grid #define N_ROWS 10 #define N_COLS 10 // Struct for dog to hold the character name, and the position of the dog struct dog { char name; int pos_row; int pos_col; }; // Helper Function: Print out the park map as a 2D grid void print_park(int park[N_ROWS][N_COLS], struct dog jax); /* # What is EOF? // scanf returns how many things have been scanned in correctly. // for example: scanf("%d", &variable); // given 3 // this will return 1 scanf("%d", &variable); // given z // this will return 0 (because 'z' isn't a decimal) scanf("%d", &variable); // given Ctrl+D // this will return -1 (because we couldn't scan ANYTHING!) // and since (usually) EOF == -1, EOF means "haven't scanned in anything". */ int main(void) { int park[N_ROWS][N_COLS] = {0}; struct dog jax; //Jax enters the park always from the bottom corner of the grid // TODO: Initialise the coordinates of where Jax is standing when he // enters the park jax.name = 'J'; jax.pos_row = 9; jax.pos_col = 9; print_park(park, jax); // TODO: Scan in a command char direction; // NOTE: direction will be 'l', 'r', 'u' or 'd'. printf("Where would Jax like to go? [l,r,u,d] "); while (scanf(" %c", &direction) == 1) { // New TODO: Let's not let the dogs out (of the park) park[jax.pos_row][jax.pos_col] = 1; if (direction == 'l') { jax.pos_col--; } else if (direction == 'r') { jax.pos_col++; } else if (direction == 'u') { jax.pos_row--; } else if (direction == 'd') { jax.pos_row++; } // Check for "boundary conditions" if (jax.pos_row < 0) { jax.pos_row = 0; } else if (jax.pos_col < 0) { jax.pos_col = 0; // jax's row >= 10 } else if (jax.pos_row >= N_ROWS) { jax.pos_row = N_ROWS - 1; } else if (jax.pos_col >= N_COLS) { jax.pos_col = N_COLS - 1; } print_park(park, jax); printf("Where would Jax like to go? [l,r,u,d] "); } return 0; } // Prints the park map, by printing the integer value stored in // each element of the 2-dimensional park array. // Should also allow to print where each dog is currently standing void print_park(int park[N_ROWS][N_COLS], struct dog jax) { int row = 0; while (row < N_ROWS) { int col = 0; while (col < N_COLS) { //TODO: will need to print out the character // for Jax based on // where the dog is standing on the park map // if Jax is where we currently are at 9, 9; // then print out a J if (jax.pos_row == row && jax.pos_col == col) { printf("%c ", jax.name); } else { printf("%d ", park[row][col]); } col++; } row++; printf("\n"); } }