- Preparation
- Programming Exercise
- Function Descriptions
- Description of "main"
- Sample Input/Output
To Prepare
Create a directory for this lab:
- Start from your home directory

- Change into your
cs115directory
- Make a
lab2directory
- Change into your
lab2directory
Brief Descriptions of the Functions in grid.cpp
initialize:
- cycle through the 2D array and set all the values to 0
- put the player in the upper left hand corner.

print_grid:
- cycle through the 2D array:
- where there is a 1, print an X to represent the player
- otherwise, print a 0
move_player:
- prompt for and input the direction
- prompt for and input the number of space to move
- cycle through the 2D array until you find where the player is:
- reset the location to 0
- set the desired location to 1
- get out of the loops
What assumption have we made in the move_player code?
Details of setting the desired location to 1 for the move_player function
- if you are moving north, then subtract from the current row index
- if you are moving south, then add to the current row index
- if you are moving east, then add to the current column index
- if you are moving west, then subtract from the current column index
Description of main.cpp
- initialize the grid
- cycle 5 times,
- print the grid
- move the player
- print the final grid
All these steps involve calls to functions.
Sample Run
Your current grid is.... X 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 Which direction would you like to move? n/s/e/w? s How many spaces? 4 adjusting south Your current grid is.... 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 X 0 0 0 0 Which direction would you like to move? n/s/e/w? e How many spaces? 3 adjusting east Your current grid is.... 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 X 0 Which direction would you like to move? n/s/e/w? n How many spaces? 2 adjusting north Your current grid is.... 0 0 0 0 0 0 0 0 0 0 0 0 0 X 0 0 0 0 0 0 0 0 0 0 0 Which direction would you like to move? n/s/e/w? w How many spaces? 1 adjusting west Your current grid is.... 0 0 0 0 0 0 0 0 0 0 0 0 X 0 0 0 0 0 0 0 0 0 0 0 0 Which direction would you like to move? n/s/e/w? e How many spaces? 2 adjusting east Your final grid is.... 0 0 0 0 0 0 0 0 0 0 0 0 0 0 X 0 0 0 0 0 0 0 0 0 0
What are the indices for the upper left hand corner? What value will be there?
cd lab2mkdir lab2cd cs115cd