In many video games there are non-playing-characters (NPC) who wander around a map, potentially triggering an interaction with the player if the player and the NPC happen to occupy the same space at the same time. In this assignment we will implement a simple wandering player on a 2D grid.
In your game, you will use a 2D array of characters to keep track of the NPC’s location. Use a 10-by-10 array, and initialize every cell to contain a period character (‘.’). The game will progress in the following manner:
In this assignment, you must make use of functions to help organize your code. Recall that to use multidimensional arrays as function parameters, we need to use variable-length arrays as parameters (review page 198 of the textbook).
The choice of which functions to implement are ultimately your decision, but here are some suggestions.
/* Function which prints the current state of the grid to the terminal
preconditions: numrows and numcols are integers, grid is a 2D array with numrows equal to the length of the first dimension of grid and numcols equal to the length of the second dimension of grid
postconditions: the exact contents of grid are printed to the terminal. No variables are changed
*/
void printgrid(int numrows, int numcols, char grid[numrows][numcols])
{
// for you to fill in
}
/* Function which checks whether there is currently a valid move from the current location
preconditions: numrows and numcols are integers, grid is a 2D array with numrows equal to the length of the first dimension of grid and numcols equal to the length of the second dimension of grid
currentrow is an integer between 0 and numrows-1.
currentcol is an integer between 0 and numcols-1.
postconditions: returns true if at least one of the directions of movement {left, right, up, down} on the grid is valid, meaning that at least one direction does not move off the grid and contains a '.' character.
*/
bool validmoveexists(int numrows, int numcols, char grid[numrows][numcols], int currentrow, int currentcol)
{
// for you to fill in
}
It is not a requirement to use these exact functions (or these exact signatures), but you must use at least two functions. There is no upper bound on how many functions you are allowed to use.
In this homework assignment we will need to make use of random numbers in the following ways:
You can review how to use random numbers on Homework 2.
You might find it helpful to make use of Booleans on this assignment. For example, we need to repeatedly choose a random direction to move until a valid direction is found. Following is some pseudo-code to consider
bool stuck = true;
while(stuck)
{
//choose random direction;
if(direction is valid)
{
move to that new location;
stuck = false;
}
}
(Note: Your implementation here can be rather inefficient. You might repeatedly choose the same direction that is not valid - that’s OK.)
If you use booleans, don’t forget to include <stdbool.h>.
You will submit your work for this homework assignment on gradescope.
Please upload your implementation in a file named wanderingplayer.c, along with a testing transcript.
You can create a testing transcript by running:
$ script tests.txt
The command above will record the commands you run and any outputs to a file named tests.txt.
Run the program as many times as you need to thoroughly test your implementation, then run exit to end the transcript.
Include tests.txt with your implementation when you submit your work to gradescope.
In this assignment, there is no user input or variables that will change on different runs. The changes will come from the random-ness of the movements of the NPC. Therefore, you should run your program enough times that you are (fairly) confident it runs as you expect. You should be sure to see your program can make it to the letter ‘Z’ successfully, as well as multiple cases where it ends earlier due to lack of valid moves. You should be confident that your NPC can move in any of the four directions.
It is in your interest to be thorough with your testing, testing your code will help you find bugs that might otherwise have hurt your score in other parts of the assignment.
Your assignment will be graded on a scale of 0–100 points based on the following criteria:
.c file which includes the author (which should just be you), and any resources you used or help you received including tutors, mentors, online and other resources (you do not need to include the instructor, the course website, links posted on this page, or your textbook). This serves as an academic honesty statement. An absence of such a statement signals that you did not receive any help or use any resources beyond our class materials. (0 points)There will be potential deductions for any code quality violations. For now, there are six requirements for quality code:
if–else block that explains whatthe purpose of that code is. The comment should not simply restate the code; it should add information for a human who reads the code and is confused.clang -o wanderingplayer.o wanderingplayer.c