Deliverables

Open the following in separate terminals for marking:

Back to Schedule

Lab Content

Quick Links

Homework

 

 

  • Preparation
  • Programming Exercise
  • Function Descriptions
  • Description of "main"
  • Sample Input/Output

To Prepare

Create a directory for this lab:

  1. Start from your home directorytool_tip

  2. Change into your cs115 directorytool_tip

  3. Make a lab2 directorytool_tip

  4. Change into your lab2 directorytool_tip

Brief Descriptions of the Functions in grid.cpp

initialize:

  1. cycle through the 2D array and set all the values to 0
  2. put the player in the upper left hand corner.tooltip

print_grid:

  1. cycle through the 2D array:
    1. where there is a 1, print an X to represent the player
    2. otherwise, print a 0

move_player:

  1. prompt for and input the direction
  2. prompt for and input the number of space to move
  3. cycle through the 2D array until you find where the player is:
    1. reset the location to 0
    2. set the desired location to 1
    3. 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

  1. initialize the grid
  2. cycle 5 times,
    1. print the grid
    2. move the player
  3. 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 lab2
mkdir lab2
cd cs115
cd