Homework 3: Wandering Player

Assigned
  • September 18, 2024
Due
  • September 23, 2024 10:30pm
Collaboration
    Regular policies for collaboration apply to this homework assignment. Make sure you review and understand the policies on the syllabus before you discuss your work on this assignment with anyone else.

Overview

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.

Requirements

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:

  1. Choose a random location on the grid for the NPC to start, and change the character in that cell to ‘A’.
  2. Make the player move in a random direction (left, right, up, or down), and fill that cell in with the next letter, ‘B’.
  3. Continue on in the same way, moving through the grid and placing the next letter of the alphabet.
  4. There are a few rules for the movement of the NPC: the NPC cannot move into a space that has previously been occupied, and the NPC cannot move “off” of the grid. When you choose one of the four random directions to move, you must make sure it’s a valid move based on these rules. If it’s not valid, choose a random direction again.
  5. After each letter is placed, the program should print the 10-by-10 array showing the placement of the letters (along with the periods).
  6. The program should end when one of the two conditions is met: either the NPC has no more valid moves, or we have reached the letter ‘Z’.

Functions

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.

Random Numbers

In this homework assignment we will need to make use of random numbers in the following ways:

  • To choose a starting location (a random row and column in the grid)
  • To choose which direction to move next (left, right, up, or down). This can be done with integers (for example randomly choosing 0 means move left).

You can review how to use random numbers on Homework 2.

Booleans

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>.

What to Submit

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.

Grading

Your assignment will be graded on a scale of 0–100 points based on the following criteria:

  • A statement at the top of your .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)
  • Use of a 2D array to keep track of state of the grid (15 points)
  • Correct generation and use of a “random” starting location, as well as “random” moves by the NPC (15 points)
  • Program uses at least two functions in a manner which improves clarity and organization of code (20 points)
  • Grid is neatly printed after each move (10 points)
  • Correct stopping criteria of the movement of the NPC (20 points)
  • Testing transcript that allows the randomness to cover a substantial number of cases (20 points)

There will be potential deductions for any code quality violations. For now, there are six requirements for quality code:

  1. Indent the body of all loops and conditionals one level deeper than the code just outside the loop. Be consistent with your indentation style.
  2. Use curly braces for every loop or conditional body, regardless of the number of lines.
  3. Include comments that explain what your code does. There should be at least one comment by the start of each loop and ifelse 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.
  4. Give variables descriptive names that help a reader understand what they will be used for.
  5. Do not read from or display the value of any variable without first initializing it.
  6. Your code must compile with no errors and no warnings with the following command
    clang -o wanderingplayer.o wanderingplayer.c